HDU 1014 Uniform Generator

来源:互联网 发布:网络传播概论第四版 编辑:程序博客网 时间:2024/06/04 00:45
Problem Description
Computer simulations often require random numbers. One way to generate pseudo-random numbers is via a function of the form

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. 
 

 

Input
Each line of input will contain a pair of integers for STEP and MOD in that order (1 <= STEP, MOD <= 100000).
 

 

Output
For each line of input, your program should print the STEP value right- justified in columns 1 through 10, the MOD value right-justified in columns 11 through 20 and either "Good Choice" or "Bad Choice" left-justified starting in column 25. The "Good Choice" message should be printed when the selection of STEP and MOD will generate all the numbers between and including 0 and MOD-1 when MOD numbers are generated. Otherwise, your program should print the message "Bad Choice". After each output test set, your program should print exactly one blank line.
 

 

Sample Input
3 515 2063923 99999
 

 

Sample Output
3 5 Good Choice 15 20 Bad Choice 63923 99999 Good Choice
 

 

Source
South Central USA 1996
 
题目大意就是 给你两个数 step mod
seed从0开始每次加step并%mod
问 是否所有seed都在0-mod-1之中
 
这题一开始没有想得高深算法 直接模拟就过了
 
后来发现只要step和mod互素 那么就是good choice
 
 1 #include<cstdio> 2 #include<iostream> 3 #define MAXN 100010 4  5 using namespace std; 6  7 int step,mod; 8  9 bool vis[MAXN],flag=false;10 11 int main() {12     while(~scanf("%d %d",&step,&mod)) {13         int seed=0,ans=0,p=0;14         flag=false;15         fill(vis,vis+MAXN+1,false);16         do {17             if(p>=MAXN) break;18             p++;19             seed=(seed+step)%mod;20             if(!vis[seed]) {21                 vis[seed]=true;22                 ans++;23             }24         }while(ans<mod);25         for(int i=1;i<mod;i++)26           if(!vis[i]) {27               flag=true;28               printf("%10d%10d    Bad Choice\n",step,mod);29               printf("\n");30               break;31           } 32         if(!flag) printf("%10d%10d    Good Choice\n",step,mod),printf("\n");33     }34     return 0;35 } 
模拟
 1 #include<cstdio> 2 #include<iostream> 3  4 using namespace std; 5  6 int step,mod; 7  8 inline int gcd(int a,int b) { 9     if(b==0) return a;10     else return gcd(b,a%b);11 }12 13 int main() {14     while(~scanf("%d %d",&step,&mod)) {15         if(gcd(step,mod)==1) 16           printf("%10d%10d    Good Choice\n",step,mod),printf("\n");17         else printf("%10d%10d    Bad Choice\n",step,mod),printf("\n");18     }19     return 0;20 }
gcd

 

原创粉丝点击