HDU 1014 Uniform Generator

Source: Internet
Author: User
Tags greatest common divisor

Uniform Generator
http://acm.hdu.edu.cn/showproblem.php?pid=1014
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 21831 Accepted Submission (s): 8556


Problem descriptioncomputer simulations often require random numbers. One-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 would generate pseudo-random numbers (seed) between 0 and MOD-1. One problem with functions of this form was that they would 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 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 this by the nature of the function to generate the same seed (x+1) every time seed (x) occurs means that if a function Would generate all the numbers between 0 and MOD-1, it'll generate pseudo-random numbers uniformly with every MOD Iterati Ons.

If STEP = + MOD =, the function generates the series 0, 5 (or any 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 would generate a uniform distribution of pseudo-random numbers.

Inputeach line of input would contain a pair of integers for STEP and MOD on that order (1 <= STEP, MOD <= 100000).

Outputfor each line of input, your program should print the STEP value right-justified in columns 1 through, the MOD V Alue right-justified in columns one through and either "good Choice" or "bad Choice" left-justified starting in column 2 5. The "good Choice" message should is printed when the selection of STEP and MOD would generate all the numbers between an D including 0 and MOD-1 when MOD numbers is generated. Otherwise, your program should print the message "Bad Choice". After the output test set, your program should print exactly one blank line.

Sample Input
3 515 2063923 99999

Sample Output
         3         5    good Choice    Choice     63923     99999    Good Choice

Test instructions: Give two numbers of step and mod to determine whether any number between 0 and mod-1 can be generated from a given function and output as required.

Problem-solving ideas: Through the analysis of the simulation can be obtained when the input two step,mod greatest common divisor 1 o'clock can meet the test instructions, that is, to determine whether two is a prime number, but the output format, which is a big pit, preferably with C output.

Complexity Analysis: Time complexity: O (0)
Space complexity: O (0)


#include <iostream> #include <cstdio> #include <iomanip> using namespace std; int main () {     int step,mod,r,a,b;     while (Cin>>step>>mod && MoD)     {         a=step>mod?step:mod;         b=step<mod?step:mod;         while (b)         {             r=a%b;             a=b;             b=r;         }         if (a==1)  printf ("%10d%10d    good choice\n\n", step, MoD);          else      printf ("%10d%10d bad    choice\n\n", step, MoD);     }     return 0; }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1014 Uniform Generator

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.