杭电 1014-- Uniform Generator

Uniform Generator

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21219    Accepted Submission(s):
8308

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

//考虑大数;注意格式;

 1 #include<stdio.h>
 2 #define MAX 100010;
 3
 4 int gcd(int a, int b)    //最大公约数;
 5 {
 6     if(!b) return a;
 7     else
 8     return gcd(b,a%b);
 9 }
10
11 int main()
12 {
13     int m,n;
14     while(~scanf("%d %d",&m,&n))
15     {
16         int len=gcd(m,n);
17         if(len==1)
18         printf("%10d%10d    Good Choice\n\n");
19         else
20         printf("%10d%10d    Bad Chioce\n\n");
21
22     }
23 } 

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int m,n;
 5     while(~scanf("%d %d",&m,&n))
 6     {
 7         printf("%10d%10d",m,n);
 8         int total=0,i;
 9         for(i=0;;i++)
10         {
11             total=(total+m)%n;
12             if(total==0)
13             break;
14         }
15         if(i==n-1)
16         printf("    Good Choice\n");
17         else
18         printf("    Bad Choice\n");
19         printf("\n");
20
21     }
22     return 0;
23 }
时间: 2024-10-17 18:22:11

杭电 1014-- Uniform Generator的相关文章

HDU 1014 Uniform Generator 题解

找到规律之后本题就是水题了,不过找规律也不太容易的,证明这个规律成立更加不容易. 本题就是求step和mod如果GCD(最大公约数位1)那么就是Good Choice,否则为Bad Choice 为什么这个结论成立呢? 因为当GCD(step, mod) == 1的时候,那么第一次得到序列:x0, x0 + step, x0 + step-- 那么mod之后,必然下一次重复出现比x0大的数必然是x0+1,为什么呢? 因为(x0 + n*step) % mod: 且不需要考虑x0 % mod的值为

(杭电 1014)Uniform Generator

Uniform Generator Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 35190 Accepted Submission(s): 14002 Problem Description Computer simulations often require random numbers. One way to generate pseu

HDU 1014 Uniform Generator

Uniform Generator Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22610    Accepted Submission(s): 8889 Problem Description Computer simulations often require random numbers. One way to generate

HDU 1014 Uniform Generator【GCD函数】

Uniform Generator Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 20180    Accepted Submission(s): 7902 Problem Description Computer simulations often require random numbers. One way to generat

HDU 1014.Uniform Generator【模拟及优化】【8月16】

Uniform Generator 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 wil

HDU 1014 Uniform Generator(题解)

Uniform Generator Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 32990    Accepted Submission(s): 13081 Problem Description Computer simulations often require random numbers. One way to generat

hdu 1014.Uniform Generator 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1014 题目意思:给出 STEP 和 MOD,然后根据这个公式:seed(x+1) = [seed(x) + STEP] % MOD,问是否在一个周期里可以产生 0 - mod-1 的数.可以的话输出 "Good Choice", 否则输出 "Bad Choice". 好久以前留下来的问题了,以前觉得题目意思又长,以为是很难的题目......今天看<短码之美>

ACM HDU 1014 Uniform Generator

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1014 解题思路: 1. 把题目意思读懂后,明白会输入两个数,然后根据题中的公式产生一系列伪随机数,看这些数是不是能够包含0~MOD-1.如果产生不了就输出“Good Choice”,否则输出“Bad Choice”. 2. 全部假使x从0开始,设STEP为a,MOD为b.如果说a,b存在倍数关系,即假设最小存在2倍关系,那么就会有b=2a.x初始为0,第一步之后为a,第二步之后为0,之后a.0交替出

(HDU)1014 --Uniform Generator(统一随机数生成)

这个题目不难,关键是看懂英文:(判断两个数是否互质,而且注意输出的格式) 描述 计算机模拟通常需要随机数.生成伪随机数的一种方式是通过一定形式的函数: seed(x + 1)= [seed(x)+ STEP]%MOD 其中'%'是模运算符. 这样的函数将生成在0和MOD-1之间的伪随机数(种子).这种形式的作用的一个问题就是,它们将总是重复地生成相同的模式. 为了最小化这种影响,仔细选择STEP和MOD值,可以使得在0和MOD-1(包括这两者)之间的所有值的均匀分布. 例如,如果STEP = 3

hdu 1014 Uniform Generator(水 枚举 gcd)

题意:有一个数列 seed[x+1]=(seed(x)+step)%mod 给出 step 和 mod 如果求出的是以 1...mod-1 为循环节的数列 则为 good choice 否则 则是 bad choice 思路:1.用标记法 如果 形成循环节时 每个数都被标记到 则good choice 2.当两个数互素时 则 good choice 2.代码 #include<cstdio> #include<cstring> #include<iostream> #i