hdoj 1014 Uniform Generator
来源:互联网 发布:java里for函数用法 编辑:程序博客网 时间:2024/05/12 02:17
Uniform Generator
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19198 Accepted Submission(s): 7523
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.
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 515 2063923 99999
Sample Output
3 5 Good Choice 15 20 Bad Choice 63923 99999 Good Choice
题意大概是:找循环体,并且是mod个数一循环,15 20是因为它们是4个一循环所以不行。
基本思路:
#include<stdio.h>#define max 100000+10int seed[max];void f(int step,int mod){ int i; seed[0]=0; for(i=1;i<=mod;i++) { seed[i]=(seed[i-1]+step)%mod; }}int main(){ int step,mod; int i,j; while(scanf("%d%d",&step,&mod)!=EOF) { f(step,mod); printf("%10d%10d ",step,mod); for(i=1;i<=mod;i++) { if(seed[i]==0) { break; } } if(i==mod) printf("Good Choice\n\n"); else printf("Bad Choice\n\n"); } return 0;}
巧法:判断两数的gcd 若等于1,即Good,否则Bad。
#include<stdio.h>int gcd(int a,int b){ int i,j,t; while(a) { t=a; a=b%a; b=t; } return t;}int main(){ int step,mod; int i,j; while(scanf("%d%d",&step,&mod)!=EOF) { printf("%10d%10d ",step,mod); if(gcd(step,mod)==1) printf("Good Choice\n\n"); else printf("Bad Choice\n\n"); } return 0;}
0 0
- HDOJ 1014 Uniform Generator
- 【HDOJ】1014 -> Uniform Generator
- hdoj 1014 Uniform Generator
- HDOJ-1014 Uniform Generator
- HDOJ 1014 Uniform Generator
- hdoj 1014 Uniform Generator
- HDOJ 1014 Uniform Generator
- Hdoj 1014 Uniform Generator
- 【HDOJ】【1014】Uniform Generator
- hdoj 1014Uniform Generator
- HDOJ 1014 Uniform Generator
- hdoj 1014 Uniform Generator
- hdoj.1014 Uniform Generator 20140809
- HDOJ 1014 Uniform Generator(公约数问题)
- hdoj 1014 uniform generator(水题)
- HDOJ Uniform Generator
- ACM--公约数为1--HDOJ 1014--Uniform Generator--水
- 1014 Uniform Generator
- C# 示例程序开发小结
- 线性表顺序存储
- 。。。
- 字符串替换 简单题
- .NET框架设计—常被忽视的C#设计技巧
- hdoj 1014 Uniform Generator
- Linux 删除大量的文件和移动大量的文件(Argument list too long)
- android软键盘相关
- Android ListView总结(多选框ListViiew,动态加载,多线程更新ListView中的进度条)
- 深入浅出的讲解傅里叶变换
- fsfafa
- JSP动作组件
- VM安装Centos7 报错无法执行64位操作
- [LeetCode]Fraction to Recurring Decimal