ACM--公约数为1--HDOJ 1014--Uniform Generator--水

HDOJ题目地址:传送门

Uniform Generator

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 25465    Accepted Submission(s): 10043

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,如果为1就输出good,否则输出bad

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<memory.h>
using namespace std;
int gcd(int a,int b){
   int r=0;
   while(b!=0){
       r= a%b;
       a=b;
       b=r;
   }
   return a;
}
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        if(gcd(n,m)==1){
            printf("%10d%10d    Good Choice\n\n",n,m);
        }else{
            printf("%10d%10d    Bad Choice\n\n",n,m);
        }
    }
}
时间: 2024-11-09 00:50:30

ACM--公约数为1--HDOJ 1014--Uniform Generator--水的相关文章

Hdoj 1014 Uniform Generator

题目:点击打开链接 分析:题目要求均匀产生随机数,有两种方法:1 对产生的数进行标记 2 要产生随机数STEP和MOD的最大公约数不能超过1 代码一: #include<stdio.h> #include<string.h> int a1[100010]; int main() { int STEP,MOD; while(~scanf("%d%d",&STEP,&MOD)) { int i; int k=0; memset(a1,0,sizeof

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

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

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 ACM 1014 Uniform Generator

分析:题意是一个生成随机数的函数,Seed[x+1] = ( seed[x] + STEP ) % MOD,seed是生成出来的随机数,seed[0]是哪个数并不重要,后面证明.STEP就是每次往前一个所加的值,再模上MOD得到下一个随机数. 判断这个随机生成函数的好坏的依据是如果能够产生0~MOD-1内的所有数,就是一个好的,否则坏(因此该題也可以用模拟,用HASH表). 根据同余特性,便可以假设在k步之后,生成的seed[k] = seed[0],所以有Seed[k] = ( seed[0]

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

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