UVa 408 Uniform generator: gcd & Proof

Source: Internet
Author: User
Tags gcd generator printf time limit

408-uniform Generator

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_ problem&problem=349

Computer simulations often require random numbers. One way to generate pseudo-random numbers are via a function of the form

Where ' is the modulus operator.

Such a function would generate pseudo-random numbers (seed) between 0 and MOD-1. One problem with functions of the ' this ' form is ' that they'll always generate the ' same '. 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 would generate the series of pseudo-random numbers 0, 3, 1, 4, 2 in a re Peating cycle. In this example, all of the numbers between and including 0 and MOD-1 would be generated every MOD iterations of the Functi On. Note "By" The nature of the "function to generate" sameseed (x+1) every time seed (x) occurs means that if a function w Ill generate all the numbers between 0 andMOD-1, it'll generate pseudo-random the numbers with uniformly MOD every S.

If step = The function generates the series 0, 5 (or any of the other repeating series if the initial SE Ed is other than 0). This is a poor selection of step and MOD because no initial seed would generate all of the numbers from 0 and MOD-1.

Your program would determine if choices of step and MOD'll generate a uniform distribution of pseudo-random numbers.

Input

Each line of input would contain a pair of integers for step and MOD in-order ( ).

Output

For each line of input, your program should print the step value right-justified in columns 1 through, the MOD value R Ight-justified in columns through and either ' good Choice ' or ' bad Choice ' left-justified starting in column 25. The ' good Choice ' message should is printed the selection of the step andmod'll generate all the numbers between and I Ncluding 0 and MOD-1 when MOD numbers are generated. Otherwise, your should print the message ' Bad Choice '. After each output test set, the your program should print exactly one blank line.

Sample Input

3 5
63923 99999

Sample Output

3 5 Good Choice

Bad Choice

63923 99999 Good Choice

The first can be directly calculated by brute force:

/*0.082s*/
    
#include <cstdio>  
    
int main ()  
{  
    int step, mod, X, Count;  
    while (~SCANF ("%d%d", &step, &mod))  
    {  
        x = 0, count = 0;  
        Do
        {  
            x = (x + step)% MoD;  
            ++count;  
        }  
        while (x && count!= MoD);  
        printf ("%10d%10d    %s\n\n", step, mod, count = mod && x = 0? "Good Choice": "Bad Choice");  
    }  
    return 0;  
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Using the above code, you can find that when mod=20, only 1,3,7,9,11,13,17,19,... is a good choice, and these numbers are 20 coprime.

So we guess: when gcd (Step,mod)!=1, this is not a good choice.

Proof: Set GCD (Step,mod) =k, then step=ka,mod=kb, no matter how the calculation

Seed (x+1) ≡seed (x) (mod k)

So there must be some numbers that can't be generated.

Optimized code:

/*0.022s*/
    
#include <cstdio>  
    
int gcd (int a, int b)  
{return  
    b gcd (b, a% B): A;  
}  
    
int main ()  
{  
    int step, mod;  
    while (~SCANF ("%d%d", &step, &mod)  
        printf ("%10d%10d    %s\n\n", step, mod, gcd (step, MoD) = 1? "Good Choice": "Bad Choice");  
    return 0;  
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.