Computer simulations often require random numbers. One way to generate pseudo-random numbers is via a function of the form
Where ''" is the modulus operator.
Such a function will generate pseudo-random numbers (Seed) Between 0 andMoD-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, selectingStepAndMoDValues
Carefully can result in a uniform distribution of all values between (and including) 0 andMoD-1.
For example, ifStep= 3 andMoD= 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 andMoD-1 will be generated everyMoDIterations
Of the function. Note that by the nature of the function to generate the sameSeed(X+ 1) every timeSeed(X) Occurs means that if a function will generate all the numbers between 0 andMoD-1, it will generate pseudo-random
Numbers uniformly with everyMoDIterations.
IfStep= 15 andMoD= 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 selectionStepAndMoDBecause no initial seed will generate
All of the numbers from 0 andMoD-1.
Your program will determine if choicesStepAndMoDWill generate a uniform distribution of pseudo-random numbers.
Input
Each line of input will contain a pair of integersStepAndMoDIn that order ().
Output
For each line of input, your program shocould printStepValue right-justified in columns 1 through 10,MoDValue right-justified in columns 11 through 20 and either''Good choice"Or''Bad choice"Left-justified starting
In column 25.''Good choice"Message shoshould be printed when the selectionStepAndMoDWill generate all the numbers between and including 0 andMoD-1 when mod numbers are generated. Otherwise, your program shocould
Print the message''Bad choice". After each output test set, your program shocould 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
Question: Give N, Mod, step = (n + step) % mod, and ask if the step can be 0 ~ Number of all values in mod-1.
Solution: the question can be equivalent to determining whether the maximum common divisor of N and mod is 1.
The reason is that I have been thinking for a long time. Now I want to prove it to you:
First, the question can be approximate to (K * n) % mod, K is 0 ~ G-1 (G is not 0 and G * n % mod = 0), because if K is greater than G, then the remainder forms a loop.
1: when the maximum common approx. of N and mod is 1, the minimum common approx. Is n * mod, that is, G = Mod, that is to say, g k values correspond to different Mod (0'mod-1) steps (cycle cannot be formed in the middle; otherwise, there is a conflict, so K corresponds to step one by one ).
2: when the maximum number of N and mod is not 1, set K to the maximum number of them, n = A * k, MOD = B * K, N % mod remainder set a = A % B remainder Set B * K.
From the conclusion of 1, we can know that the B set is 0 ~ B-1. For example, 1 * k, 2 * K. If K is 2, 2 and 4 are obtained, and 1 and 3 are not included in.
#include<stdio.h>int judge(int n, int m){for (int i = 2; i <= n && i <= m; i++){if (n % i == 0 && m % i == 0)return 0;}return 1;}int main(){int n, m;while (scanf("%d%d", &n, &m) !=EOF){printf("%10d%10d", n, m);if (judge(n, m))printf(" Good Choice\n\n");elseprintf(" Bad Choice\n\n");}return 0;}