1uva408(数论)
来源:互联网 发布:中标麒麟装软件 编辑:程序博客网 时间:2024/06/03 19:12
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=349
408 - Uniform Generator
Time limit: 3.000 seconds
Computer simulations often require random numbers. One way togenerate pseudo-random numbers is via a function of the form
where `` " is the modulus operator.
Such a function will generate pseudo-random numbers(seed) between 0 and MOD-1. One problem withfunctions of this form is that they will always generate the samepattern over and over. In order to minimize this effect, selectingthe STEP and MOD values carefully can result in auniform distribution of all values between (and including) 0 andMOD-1.
For example, if STEP = 3 and MOD = 5, the functionwill generate the series of pseudo-random numbers 0, 3, 1, 4, 2 ina repeating cycle. In this example, all of the numbers between andincluding 0 and MOD-1 will be generated every MODiterations of the function. Note that by the nature of the functionto generate the same seed(x+1) every timeseed(x) occurs means that if a function will generateall the numbers between 0 and MOD-1, it will generatepseudo-random numbers uniformly with every MODiterations.
If STEP = 15 and MOD = 20, the function generatesthe series 0, 15, 10, 5 (or any other repeating series if theinitial seed is other than 0). This is a poor selection ofSTEP and MOD because no initial seed will generateall of the numbers from 0 and MOD-1.
Your program will determine if choices of STEP andMOD will generate a uniform distribution of pseudo-randomnumbers.
Input
Each line of input will contain a pair of integers forSTEP and MOD in that order ( ).
Output
For each line of input, your program should print theSTEP value right- justified in columns 1 through 10, theMOD value right-justified in columns 11 through 20 andeither ``Good Choice" or ``Bad Choice"left-justified starting in column 25. The ``Good Choice"message should be printed when the selection of STEP andMOD will generate all the numbers between and including 0and MOD-1 when MOD numbers are generated. Otherwise, yourprogram should print the message ``Bad Choice". After eachoutput test set, your program should print exactly one blankline.
SampleInput
3 515 2063923 99999
SampleOutput
3 5 Good Choice 15 20 Bad Choice 63923 99999 Good Choice题意:通过这个公式判断能否产生0到mod-1的随机数。。
一开始做的时候tle,别忘了数据是1千万啊而我用了差不多是两个循环。。
这题网上是用判断公约数是不是1.不明白为什么这么做。。。
代码1:tle
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 10000001
long long int seed[maxn];
long long int vis[maxn];
int main()
{
int step,mod;
while(scanf("%lld%lld",&step,&mod)!=EOF)
{
memset(seed,0,sizeof(seed));
memset(vis,0,sizeof(vis));
int i=0,count=1,j;
int flag=0;
while(1)
{
seed[i+1]=(seed[i]+step)%mod;
for(j=0;j<mod;j++)
{
if(seed[i+1]==j&&vis[seed[i+1]]==0)
{
count++;
vis[seed[i+1]]=1;
}
if(vis[seed[i+1]])
break;
}
if(count==mod)
{
flag=1;
break;
}
}
if(flag)
printf("lldlld Good Choice\n",step,mod);
else
printf("lldlld Bad Choice\n",step,mod);
}
return 0;
}
代码2:AC.不过还是有要注意的地方在注释里有解释
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define maxn 10000001
long long intseed[maxn];
int main()
{
int step,mod;
while(scanf("%d%d",&step,&mod)!=EOF)
{
int i;
for(i=0;i<mod;i++)
seed[i]=0;
//memset(seed,0,sizeof(seed));//用memset()的话就tle..
int x=step;
int count=0;
while(seed[(x+step)%mod]==0)
{
x=(x+step)%mod;
count++;
seed[x]=1;
}
if(count==mod)
printf("dd Good Choice\n\n",step,mod);
else
printf("dd Bad Choice\n\n",step,mod);
}
return 0;
}
- 1uva408(数论)
- UVA408 (暑假-数论 -D - Uniform Generator)
- UVA408
- UVa408 Uniform Generator
- UVA408 Uniform Generator
- uva408 Uniform Generator
- [转]数论知识(1)
- 数论模板1
- 数论总结1
- 数论证明题(1)
- 1uva10392(数论)
- 1uva10879(数论)
- 数论总结1
- 初等数论1
- 数论总结(1)
- 数论(1)
- 低等数论1
- 数论总结1
- 机遇偏爱有准备的头脑
- poj3259(Bellman_ford)
- 1poj2524(套模板并查集)
- 未uva550(数论)
- ExtAspNet快速入门 - 原生Ajax与PageManager控件
- 1uva408(数论)
- uva10916
- 1cf136div2
- 1poj1598(字符串)
- 1poj2240(bellman_ford||floyd)
- 我的博客今天0岁231天了,我领取了…
- poj2240(bellman_ford||floyd)
- poj2182(线段树计算元素位置)
- poj2828(线段树点的查询)