HDU 1014 Uniform Generator【GCD,水】

时间:2022-05-07
本文章向大家介绍HDU 1014 Uniform Generator【GCD,水】,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Uniform Generator

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29336    Accepted Submission(s): 11694

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 5

15 20

63923 99999

Sample Output

         3         5    Good Choice

        15        20    Bad Choice

     63923     99999    Good Choice

Source

South Central USA 1996

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1014

分析:(⊙o⊙)…我其实没有看懂题目意思,看着样例直接猜过去的,毕竟很容易看出gcd的关系,看出了这点的话,应该直接就知道怎么写吧,PE了三发,这个格式问题真的是,诶,比较智障罢了,不知道空格空多少个位置! 下面解释一下为什么GCD是正解呢!

因为当GCD(step, mod) == 1的时候,那么第一次得到序列:x0, x0 + step, x0 + step…… 那么mod之后,必然下一次重复出现比x0大的数必然是x0+1,为什么呢?

因为(x0 + n*step) % mod; 且不需要考虑x0 % mod的值为多少,因为我们想知道第一次比x0大的数是多少,那么就看n*step%mod会是多少了,因为GCD(step, mod) == 1,那么n*step%mod必然是等于1,故此第一次重复出现比x0大的数必然是x0+1,那么第二次出现比x0大的数必然是x0+2,以此类推,就可得到必然会出现所有0到mod-1的数,然后才会重复出现x0.

当GCD(step, mod) != 1的时候,可以推出肯定跨过某些数了,这里不推了。

然后可以扩展这个结论,比如如果使用函数 x(n) = (x(n-1) * a + b)%mod;增加了乘法因子a,和步长b了;

那么如果是Good Choice,就必然需要GCD(a, mod) == 1,而且GCD(b, mod) == 1;

这里就偷懒不证明这个扩展结论了,而且证明这个结论需要用到线性模(Congruence)和乘法逆元的知识了。

下面给出AC代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int gcd(int a,int b)
 4 {
 5     return b==0?a:gcd(b,a%b);
 6 }
 7 int main()
 8 {
 9     int step,mod;
10     while(scanf("%d%d",&step,&mod)!=EOF)
11     {
12         if(gcd(step,mod)==1)
13             printf("%10d%10d    Good Choicenn",step,mod);
14         else
15             printf("%10d%10d    Bad Choicenn",step,mod);
16     }
17     return 0;
18 }