uva350(数论)

来源:互联网 发布:什么是淘宝同步单 编辑:程序博客网 时间:2024/06/03 16:57

题意:求循环的长度。。

思路:一开始想用结构体来做,没得到一个L就flag一下表示访问过,不过输入数据就卡住了不得不得不放弃。

后来用了下面这种方法感觉是一样的就是测试数据过不了,有时间在改改试试。。感觉代码1,代码2思路是一样的。不知道错在哪里了。。。

代码1:未过测试数据。。哦原来是数组开小了。。代码1:还有个问题就是But be careful: the cyclemight not begin with theseed!数组不一定是从第一个数组开始的所以导致第三组测试数据输出501。。。

 

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[1010];
int main()
{
 int Z, I, M,L;
 while(scanf("%d%d%d%d",&Z,&I,&M,&L)!=EOF)
 {
  if(Z==0&&I==0&&M==0&&L==0)
   break;
  int count=0;
  memset(a,0,sizeof(a));
  while(1)
  {
   if(a[L]==0)
   {
    a[L]=1;
    count++;

   }
   L=(Z*L+I)%M;
   if(a[L]!=0)
    break;
  }
  printf("%d\n",count);
 }
 return 0;
}

 

//代码2:AC
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=10100;
int a[maxn];//数组开大点
int main()
{
 int Z, I, M,L;
 int flag=1;
 while(scanf("%d%d%d%d",&Z,&I,&M,&L)!=EOF)
 {
  if(Z==0&&I==0&&M==0&&L==0)
   break;
  memset(a,0,sizeof(a));
  int count=0;
  do
  {
   L=(Z*L+I)%M;
   a[L]++;
   count++;
  }while(a[L]==1);
  printf("Case %d:%d\n",flag++,count-1);
 }
 return 0;
}

原创粉丝点击