Hdu1014: Uniform Generator

Source: Internet
Author: User
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 shocould 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 shocould 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 shocould print the message "bad choice ". after each output test set, your program shocould 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

 

# Include <stdio. h> # include <string. h> int hash [1000000]; int seed [1000000]; int main () {int step, Mod, I; while (~ Scanf ("% d", & step, & mod) {memset (hash, 0, sizeof (hash); seed [0] = 0; hash [0] = 1; int flag = 0; for (I = 1; I <1000000; I ++) {seed [I] = (seed [I-1] + step) % MOD; hash [seed [I] = 1;/* else {flag = 0; break;} */} for (I = 0; I <MOD; I ++) {If (! Hash [I]) break;} if (I> = mod) Flag = 1; if (FLAG) printf ("% 10d % 10d good choice \ n", step, moD); else printf ("% 10d % 10d bad choice \ n", step, MoD);} 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.