uva350(数论)
2013-05-30 10:58
267 查看
题意:求循环的长度。。
思路:一开始想用结构体来做,没得到一个L就flag一下表示访问过,不过输入数据就卡住了不得不得不放弃。
后来用了下面这种方法感觉是一样的就是测试数据过不了,有时间在改改试试。。感觉代码1,代码2思路是一样的。不知道错在哪里了。。。
代码1:未过测试数据。。哦原来是数组开小了。。代码1:还有个问题就是But be careful: the cycle
might not begin with the
seed!数组不一定是从第一个数组开始的所以导致第三组测试数据输出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;
}
思路:一开始想用结构体来做,没得到一个L就flag一下表示访问过,不过输入数据就卡住了不得不得不放弃。
后来用了下面这种方法感觉是一样的就是测试数据过不了,有时间在改改试试。。感觉代码1,代码2思路是一样的。不知道错在哪里了。。。
代码1:未过测试数据。。哦原来是数组开小了。。代码1:还有个问题就是But be careful: the cycle
might not begin with the
seed!数组不一定是从第一个数组开始的所以导致第三组测试数据输出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;
}
相关文章推荐
- UVA 350 (暑假-数论 -E - Pseudo-Random Numbers)
- 【数论】Irrelevant Elements, ACM/ICPC NEERC 2004, UVa1635 【组合数学】
- UVa 160 Factors ans Factorials(数论)
- ACM 数论 UVALive 6170 Esspe-Peasee 解二元一次方程 扩张欧几里得算法
- UVA-10061 How many zero's and how many digits ? (数论)
- UVA305 - Joseph(数论 + 打表)
- UVA 题目1521 GCD Guessing Game(数论+贪心)
- uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)
- UVa 350 - Pseudo-Random Numbers
- uva 10780 - Again Prime? No Time.(数论)
- UVA 1434 - YAPTCHA(数论)
- uva11538(数论)
- uva 1529 - Clock(数论)
- UVA 10791 Minimum Sum LCM (数论)
- UVA11388 11889 10943 10780 10892 11752 11076 11609 11489 10791 11461/LA 2889 2911 2756 数论基础题
- UVA 417 - Word Index(数论)
- UVA 10862 - Connect the Cable Wires(数论 递推 高精度)
- UVA - 350 Pseudo-Random Numbers set的使用
- UVa 11388 - GCD LCM (简单数论)
- UVA 12716 GCD XOR(数论+枚举+打表)