找到规律之后本题就是水题了。只是找规律也不太easy的。证明这个规律成立更加不easy。

本题就是求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)和乘法逆元的知识了。

题目:

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
  1. 3 5
  2. 15 20
  3. 63923 99999
 
Sample Output
  1. 3 5 Good Choice
  2. 15 20 Bad Choice
  3. 63923 99999 Good Choice

本题的代码是非常easy的:
  1. #include <stdio.h>
  2.  
  3. inline int GCD(int a, int b)
  4. {
  5. return b == 0? a : GCD(b, a % b);
  6. }
  7.  
  8. int main()
  9. {
  10. int step, mod;
  11. while (scanf("%d %d", &step, &mod) != EOF)
  12. {
  13. printf("%10d%10d ", step,mod);
  14. if(GCD(step, mod) == 1) printf("Good Choice\n\n");
  15. else printf("Bad Choice\n\n");
  16. }
  17. return 0;
  18. }

HDU 1014 Uniform Generator 题解的更多相关文章

  1. HDU 1014 Uniform Generator(模拟和公式)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1014 Uniform Generator Time Limit: 2000/1000 MS (Java ...

  2. HDU 1014 Uniform Generator(题解)

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

  3. HDU 1014 Uniform Generator【GCD,水】

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

  4. HDU 1014:Uniform Generator

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

  5. hdu 1014.Uniform Generator 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1014 题目意思:给出 STEP 和 MOD,然后根据这个公式:seed(x+1) = [seed(x) ...

  6. HDU 1014 Uniform Generator 欧几里得

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1014 解题思路: 1. 把题目意思读懂后,明白会输入两个数,然后根据题中的公式产生一系列伪随机数,看这 ...

  7. hdu 1014 Uniform Generator 数论

    摘取于http://blog.csdn.net/kenden23/article/details/37519883: 找到规律之后本题就是水题了,不过找规律也不太容易的,证明这个规律成立更加不容易. ...

  8. HDU 1014 Uniform Generator(最大公约数,周期循环)

    #include<iostream> #include <cstdio> #include <cstring> using namespace std; int m ...

  9. 1014 Uniform Generator ACM

    http://acm.hdu.edu.cn/showproblem.php?pid=1014 题目的英文实在是太多了 ,搞不懂. 最后才知道是用公式seed(x+1) = [seed(x) + STE ...

随机推荐

  1. centos6.x一键15项系统优化(转自努力哥)

    #!/bin/sh ################################################ #Author:nulige # qqinfo: # Date: -- #vers ...

  2. HTML+JavaScript制作表白特效,表白不成功,小编现场吃雪

    今年的雪特别美,长沙自从08年后的最大的一场学了,今天小编给大家制作一个表白特效,希望大家喜欢,如果你是程序员希望对你有帮助,追到你喜欢的女孩,哈哈~追不到对象,小编现场吃学给你大家看 下图是爱心飘落 ...

  3. 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)F 猴子排序的期望【Java/高精度/组合数学+概率论】

    链接:https://www.nowcoder.com/acm/contest/116/F 来源:牛客网 题目描述 我们知道有一种神奇的排序方法叫做猴子排序,就是把待排序的数字写在卡片上,然后让猴子把 ...

  4. Python的支持工具[0] -> 环境包管理工具[1] -> Anaconda

    Anaconda包管理工具 / Anaconda Package Management Tools Anaconda is the world’s most popular Python data s ...

  5. H. Fake News (medium)

    H. Fake News (medium) 题意 以前是给出 S T 串,问在 S 中有多少个子串为 T 的个数,子串可以不连续,保持位置相对一致. 现在给出 n ,要你构造 S T 串. 分析 这种 ...

  6. UTF-8 setup for workspace

    In Eclipse, go to Preferences>General>Workspace and select UTF-8 as the Text File Encoding. Th ...

  7. [BZOJ 1806] Miners 矿工配餐

    Link: BZOJ 1806 传送门 Solution: 为了使状态包含每个节点前所有必须的信息: 设$dp[i][a1][a2][b1][b2]$为配送到第$i$个,一厂前两个为$a1,a2$,二 ...

  8. The newly created daemon process has a different context than expected

    Error: The newly created daemon process has a different context than expected. It won't be possible ...

  9. [置顶] kubernetes资源类型--deployment

    Deployment(中文意思为部署.调度)提供了一种更加简单的更新RC和Pod的机制,K8S版本1.2实现的.通过在Deployment中描述所期望的集群状态,Deployment Controll ...

  10. ubuntu 安装 regex模块时 fatal error: Python.h: No such file or directory

    原因是 python-dev包没有安装 根据Py2还是py3 sudo apt-get install python-dev 或者 sudo apt-get install python3-dev 安 ...