Uniform Generator

Uniform Generator

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

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

#include<iostream>
#include <iomanip>
using namespace std;

int main()
{
int step=0;
int mod=0;
while(cin>>step>>mod)
{
int *modvalue=new int[mod+2];
modvalue[0]=0;
for(int i=1;i<mod+2;i++)
{
modvalue[i]=(modvalue[i-1]+step)%mod;
}
int loop=0;
for(int i=1;i<mod+2;i++)
{
if(modvalue[i]==0)
{
loop++;
break;
}
else
{
loop++;
}

}
if(loop==mod)
{
cout<<setw(10)<<setiosflags(ios::right)<<step;
cout<<setw(10)<<setiosflags(ios::right)<<mod;
//printf("%10d%10d", step, mod);
cout<<" Good Choice"<<endl<<endl;
}
else
{
// printf("%10d%10d", step, mod);
cout<<setw(10)<<setiosflags(ios::right)<<step;
cout<<setw(10)<<setiosflags(ios::right)<<mod;
cout<<" Bad Choice"<<endl<<endl;
}
}
return 0;
}

Sample Output

3 5 Good Choice

15 20 Bad Choice

63923 99999 Good Choice

这题主要是输出格式设置,以下是我的完整代码:

时间: 2024-10-11 04:32:08

Uniform Generator的相关文章

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 题解

找到规律之后本题就是水题了,不过找规律也不太容易的,证明这个规律成立更加不容易. 本题就是求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的值为

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

(杭电 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(统一随机数生成)

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

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". 好久以前留下来的问题了,以前觉得题目意思又长,以为是很难的题目......今天看<短码之美>

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