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

 UniformGenerator 

Computer simulations often require random numbers. One way togenerate pseudo-random numbers is via a function of the form

displaymath29

where `` tex2html_wrap_inline31 " 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 ( tex2html_wrap_inline77 ).

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题意:displaymath29通过这个公式判断能否产生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 int  seed[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;
}
原创粉丝点击