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 Choice15        20    Bad Choice63923     99999    Good Choice

解题思路:
循环节为 mod/gcd(step,mod);
代码:

#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
int main(){int a,b;while(~scanf("%d%d",&a,&b)){printf("%10d%10d    ",a,b);if(gcd(a,b)==1)puts("Good Choice");else puts("Bad Choice");putchar('\n');}return 0;
}

HDU-1014 线性同余法相关推荐

  1. 【随机数生成算法系列】线性同余法和梅森旋转法

    一般我们用到的随机算法都是伪随机算法,什么叫伪随机算法呢?伪随机算法意思是假如知道第一个随机种子和随机算法的话就可以推算出下一个随机数.通常我们程序里都是通过当前时间作为随机函数的第一个随机种子,然后 ...

  2. 随机算法求pi、线性同余法求random、拉斯维加斯算法python

    一.随机算法求pi # 计算圆周率 import pdb import random def CalcPai(n):# 计算π值k = 0for i in range(0,n):x = random. ...

  3. hdu 3949(线性基模版) 异或和中第k小的数

    传送门 给定 n(n \le 10000)n(n≤10000) 个数 a_1, a_2, \ldots, a_na​1​​,a​2​​,-,a​n​​,以及 Q(Q\le 10000)Q(Q≤1000 ...

  4. RNG分析:线性同余法 LCG

    古老的LCG(linear congruential generator)代表了最好最朴素的伪随机数产生器算法.主要原因是容易理解,容易实现,而且速度快. LCG 算法数学上基于公式: X(n+1)  ...

  5. 线性同余法产生随机数C语言,线性同余生成随机数的一点思考

    今天下午 pk 和我讨论了一个问题,他看到在另一个项目组的 lua 代码里有一段使用线性同余产生随机数的代码,但是那个项目组的同事告诉他这个函数生成的随机数是分布不均的.于是他想到了我前两天给他讲的关 ...

  6. hdu 1014 +hdu 1019 (求最小公倍数或者排序)

    题目: Problem Description Computer simulations often require random numbers. One way to generate pseud ...

  7. hdu 1014 Uniform Generator 数论

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

  8. 同余 在计算机中的应用 算法,线性乘同余法在购车摇号中的应用

    [1] 邓玉良, 毛志刚, 叶以正. 基于Logistic映射的智能IC卡随机数发生器[J] . 计算机研究与发展, 1999, 36(4):509-512. (Deng Yuliang, Mao Z ...

  9. 一种基于线性反馈位移寄存器的随机数生成方法

    以下介绍一种基于线性反馈位移寄存器的随机数生成方法,这种算法是某品牌MCU主控的随机数模块生成算法,主控上是电路直接实现的,产生随机数的速度很快,用C代码描述如下: #define WORD unsi ...

最新文章

  1. 实时机器学习是什么,面临哪些挑战?
  2. Linux进程间通信四 Posix 消息队列简介与示例
  3. ubuntu卸载vsftpd出错
  4. 行为类模式--策略模式
  5. linux集群命令关闭其中一台,自己整理的一点Linux命令集
  6. 终于,我读懂了所有Java集合——map篇
  7. 42 CO配置-控制-产品成本控制-成本对象控制-实际成本核算/物料分类帐-激活实际成本核算
  8. leetcode题解41-缺失的第一个正数原来如此简单
  9. 新SQL Server 2016示例数据库
  10. java sin函数图像_java中怎样绘制正弦函数图象
  11. PEP8——Python代码规范
  12. 经典机器学习系列(十三)【结构化学习】
  13. java web 车辆管理系统_javaweb车辆信息管理系统
  14. Pillow图像处理
  15. Linux下ffmpeg批量转换图片
  16. 可汗学院公开课——统计学学习:47-61
  17. 翻译:软件测试的未来五个趋势
  18. html中onfocus作用,HTML onfocus用法及代码示例
  19. 【工具使用系列】PostScript工具 GhostScript,GSview,Epstool,RedMon
  20. word中,解决插入形状后固定位置不变。

热门文章

  1. SpringMVC框架从入门到精通
  2. 计算机专业毕设评阅人评语,毕业论文指导教师评语与评阅人评语写法
  3. 如何查找重复文件并快速删除,电脑查重复文件的方法
  4. 2018 软件学院 AK 杯 题解
  5. linux中修改vlan的ip地址,RHEL在VLAN Trunk模式下的IP地址配置
  6. [渝粤教育] 西南科技大学 材料力学 在线考试复习资料(1)
  7. cesium 使用entities、primitives添加的模型并且改变模型颜色
  8. java.lang.OutOfMemoryError: PermGen space
  9. sql语句:简写拼音查询
  10. GBase XDM Cluster 产品介绍