找到规律之后本题就是水题了。只是找规律也不太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
3 5
15 20
63923 99999
 
Sample Output
3 5 Good Choice 15 20 Bad Choice 63923 99999 Good Choice

本题的代码是非常easy的:
#include <stdio.h>

inline int GCD(int a, int b)
{
return b == 0? a : GCD(b, a % b);
} int main()
{
int step, mod;
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;
}

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. springBoot springCloud

    微服务功能的主要体现: 1)服务的注册与发现 Eureka ,Consul ,Zookeeper 2)服务的负载均衡 Ribbon 3)服务的容错 Hystrix 4)服务的网关 微服务中常用的网关组 ...

  2. 实现如下语法的功能:var a = add(2)(3)(4); //9

    从汤姆大叔的博客里看到了6个基础题目:本篇是第6题 - 实现如下语法的功能:var a = add(2)(3)(4); //9 解题关键:add()函数需要返回一个加法函数,而不是一个普通的值,即定义 ...

  3. Http与RPC通信协议的比较

    OSI网络结构的七层模型 各层的具体描述如下: 第七层:应用层     定义了用于在网络中进行通信和数据传输的接口 - 用户程式:提供标准服务,比如虚拟终端.文件以及任务的传输 和处理:  第六层:表 ...

  4. Python Unittest与数据驱动

    python中有一个装饰器类DDT,通过它我们可以复用代码,达到数据驱动测试的目的,该类的官方介绍可以参考 http://ddt.readthedocs.io/en/latest/index.html ...

  5. Python的程序结构[2] -> 类/Class[5] -> 内建类 bytes 和 bytearray

    内建类 bytes 和 bytearray / Built-in Type bytes and bytearray 关于内建类 Python的内建类 bytes 主要有以下几点: class byte ...

  6. 【贪心】Mixing Milk

    题目描述 The Merry Milk Makers company buys milk from farmers, packages it into attractive 1- and 2-Unit ...

  7. Xamarin.Android真机测试提示[INSTALL_FAILED_UPDATE_INCOMPATIBLE]

    Xamarin.Android真机测试提示[INSTALL_FAILED_UPDATE_INCOMPATIBLE]   使用真机测试的时候,出现以下错误提示:   Deployment failed ...

  8. Oracle触发器简单入门记录

    写在前面: 最近,老项目新增了日报优化的需求,丽姐让我用触发器去实现当数据插入或者更新的时候,实现对日报表数据更新操作.嗯嗯嗯呢,之前学习数据库的时候,有碰到过触发器,但都是一跳而过,也没怎么去真正的 ...

  9. android中的开机自启动

    android中的开机自启动 android中的开机自启动可分为两步: 1.写一个BroadcastReceiver: public class BootReceiver extends Broadc ...

  10. Working with the NSOperationQueue Class

    Multi-tasking prevents apps from freezing. In most programming languages, achieving this is a bit tr ...