杭电1014
Uniform Generator
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21450 Accepted Submission(s): 8398
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
分析:
题目比较长,大致意思为,给定一个递推公式seed(x+1) = [seed(x) + STEP] % MOD,给出STEP和MOD,问通过该公式计算出的所有结果能否包含从0到MOD-1的所有数。
因为牵扯到取余运算,而且得出下一个数所需的seed(x)和STEP都是确定已知的,所以可以判定会产生循环,且周期必定小于或等于MOD,但是此题要求产生的数包含0到MOD-1所有的数,故周期必定为MOD才符合。
所以解题思路如下:
对于给出的STEP和MOD,按照公式求出seed(0)到seed(mod-1),保存在数组里,进行升序排列,如果是 Good Choice,那么排序结果肯定是0,1,2,3.........mod-1 ,可以直接用数组下标与该下标所指数据进行对比,若存在不相同的数,便为 Bad Choice
网上更多人选择的做法是判断STEP与MOD是否互质,若互质便是 Good Choice。简单粗暴,时间复杂度相当低,具体数学推导没有找到,所以我采用前一种做法。
具体代码如下:
#include<iostream>
#include<iomanip>
#include<algorithm>
using namespace std;
int data[100005];
void seed(int step, int mod)
{
data[0] = 0;
for (int i = 1; i <= mod; i++)
{
data[i] = (data[i - 1] + step) % mod;
}
}
int cmp(const void *a, const void *b)
{
return *(int*)a - *(int*)b;
}
int main()
{
int step, mod;
while (cin >> step >> mod)
{
seed(step, mod);
bool flag = true;
qsort(data, mod, sizeof(int),cmp);
//sort(data, data + mod);
for (int i = 0; i < mod; i++)
{
if (data[i] != i)
flag = false;
}
if (flag == true)
cout << setw(10) << step << setw(10) << mod << " Good Choice\n" << endl;
else
cout << setw(10) << step << setw(10) << mod << " Bad Choice\n" << endl;
}
return 0;
}