这道题题目如下:
seed(x+1) = [seed(x) + STEP] % MOD
where '%' is the modulus operator.
Such a function will generate pseudo-random numbers (seed) between 0 and MOD-1. One problem with functions of this form is that they will always generate the same pattern over and over. In order to minimize this effect, selecting the STEP and MOD values carefully can result in a uniform distribution of all values between (and including) 0 and MOD-1.
For example, if STEP = 3 and MOD = 5, the function will generate the series of pseudo-random numbers 0, 3, 1, 4, 2 in a repeating cycle. In this example, all of the numbers between and including 0 and MOD-1 will be generated every MOD iterations of the function. Note that by the nature of the function to generate the same seed(x+1) every time seed(x) occurs means that if a function will generate all the numbers between 0 and MOD-1, it will generate pseudo-random numbers uniformly with every MOD iterations.
If STEP = 15 and MOD = 20, the function generates the series 0, 15, 10, 5 (or any other repeating series if the initial seed is other than 0). This is a poor selection of STEP and MOD because no initial seed will generate all of the numbers from 0 and MOD-1.
Your program will determine if choices of STEP and MOD will generate a uniform distribution of pseudo-random numbers.
3 5 15 20 63923 99999
首先我写了一个程序,模拟了题目所说的过程
1014.cpp 如下:
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int vcc (int step,int mod)
{
int seed=0,seed1=0;
int count=0;
vector<int> text;
for(int j=0;j<=mod-1;j++)
{
text.push_back(seed);
seed1=(seed+step)%mod;
/*for(int i=0;i!=text.size();i++)
{
if(text[i]==seed1)
{
count++;
//break;
}
}*/
seed=seed1;
}
for(int i=0;i!=text.size();i++)
{
for(int k=i+1;k!=text.size();k++)
{
if(text[k]==text[i])
{
count++;
break;
}
}
if(count)
{
break;
}
}
return count;
}
int main()
{
int text1,text2;
scanf("%d%d",&text1,&text2);
//cin>>text1>>text2;
//cout<<vcc(text1,text2)<<endl;
printf("%10d%10d%20s",text1,text2,(vcc(text1,text2)?"Bad Choice":"Good Choice"));
return 0;
}
虽然模拟出来结果,不过时间复杂度过高,当我输如 63923 99999 这个case时,时间已经超过半分钟,不可思议!!!!原因是我把所有0~99999中的所有数据放入到vector中,然后在两次遍历,时间就爆表了。。。。。
之后,我从网上看到一些解法,就是不用把整个数据全记录下来,把“seed(x+1) = [seed(x) + step] % mod” 这个条件作为循环条件:于是我又写了第二版:
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int step,mod;
bool isf[100000];
//cin>>step>>mod;
while (scanf("%d%d",&step,&mod)!=EOF)
{
bool tag=true;
for(int i=0;i<mod;i++)
{
isf[i]=false;
}
int seed=0;
do
{
isf[seed]=true;
seed=(seed+step)%mod;
}while(seed!=0);
for(int j=0;j<mod;j++)
{
if(!isf[j])
{
tag=false;
break;
}
}
//printf("%10d%10d%14s\n",step,mod,(tag?"Good Choice":"Bad Choice"));
if(tag)
{
printf("%10d%10d Good Choice\n\n",step,mod);
}
else
{
printf("%10d%10d Bad Choice\n\n",step,mod);
}
cout << endl;
}
return 0;
}
随后,我又发现,这便是求两个整数是否互质的问题。。。。。。
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int step,mod;
while(cin>>step>>mod)
{
bool tag=true;
for(int i=2;i<=(step>=mod?mod:step);i++)
{
if(mod%i==0&&step%i==0)
{
tag=false;
break;
}
}
printf("%10d%10d %s\n",step,mod,(tag?"Good Choice":"Bad Choice"));
cout<<endl;
}
return 0;
}
这就是我的思考之路,智商捉鸡。。。。