HDU1014
来源:互联网 发布:windows下wep破解 编辑:程序博客网 时间:2024/05/28 09:32
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
第一呢:初始种子不是关键(一开始我给考虑进去了)
第二呢:如果你找不到规律那么用set的判重性比数组好多了(注意清空set)
第三呢:如果找到规律那就忒厉害了~~
转自:红黑联盟
/*找到规律之后本题就是水题了,不过找规律也不太容易的,证明这个规律成立更加不容易。
本题就是求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的值为多少,因为我们想知道第一次比x0大的数是多少,那么就看n*step%mod会是多少了,因为GCD(step, mod) == 1,那么n*step%mod必然是等于1,故此第一次重复出现比x0大的数必然是x0+1,那么第二次出现比x0大的数必然是x0+2,以此类推,就可得到必然会出现所有0到mod-1的数,然后才会重复出现x0.
当GCD(step, mod) != 1的时候,可以推出肯定跨过某些数了,这里不推了。
然后可以扩展这个结论,比如如果使用函数 x(n) = (x(n-1) * a + b)%mod;增加了乘法因子a,和步长b了;
那么如果是Good Choice,就必然需要GCD(a, mod) == 1,而且GCD(b, mod) == 1;
这里就偷懒不证明这个扩展结论了,而且证明这个结论需要用到线性模(Congruence)和乘法逆元的知识了。*/
#include<iostream>#include<algorithm>#include<set>#include<cstdio>using namespace std;int main(){int step,mod,seed;int x,nx;set<int> mys;while(cin>>step>>mod){int flag = 0;//for(int seed = 0;seed < mod;seed++)//{int t = 0;x = 0;while(mys.insert(x).second == true){t++;x = (x + step) % mod;}mys.clear();if(t == mod){flag = 1;//break;}//}printf("%10d%10d ",step,mod) ;if(flag == 1){cout<<"Good Choice"<<"\n"<<endl;}else{cout<<"Bad Choice"<<"\n"<<endl; }}return 0;}
#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>using namespace std;int Gcd(int a,int b){ return b==0?a:Gcd(b,a%b);}int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { if(Gcd(n,m)==1) printf("%10d%10d Good Choice\n",n,m); else printf("%10d%10d Bad Choice\n",n,m); printf("\n"); } return 0;}
- hdu1014
- hdu1014
- hdu1014
- hdu1014
- hdu1014
- HDU1014
- HDU1014
- HDU1014
- HDU1014
- hdu1014(随机数)
- HDU1014 Uniform Generator
- HDU1014 ( Uniform Generator )
- hdu1014 Uniform Generator
- HDU1014:Uniform Generator
- hdu1014 Uniform Generator
- hdu1014 Uniform Generator
- hdu1014 Uniform Generator--哈希表水题
- hdu1014 Uniform Generator
- Android理解:显式和隐式Intent
- Oracle 使用字符串作为查询集合时,rownum的使用
- 基于深度学习框架的火灾识别报警平台搭建----OpenCV3.1.0读取dav视频数据出错
- stl map使用详解
- Windows核心编程之多进程概述
- HDU1014
- oracle监听配置
- 传美云商系统开发后台文档
- 移动端车牌识别扫一扫
- pandas常用命令
- BDTC2017北京大数据技术大会参会心得
- 第一行代码 3.4.2 创建自定义控件 章节中初上手出项的下载完成后闪退问题和自定义控件无反应问题
- ajax获取json数据展示
- springboot集成log4j