Uniform Generator

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29336    Accepted Submission(s): 11694

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
 
Source
分析:(⊙o⊙)…我其实没有看懂题目意思,看着样例直接猜过去的,毕竟很容易看出gcd的关系,看出了这点的话,应该直接就知道怎么写吧,PE了三发,这个格式问题真的是,诶,比较智障罢了,不知道空格空多少个位置!
下面解释一下为什么GCD是正解呢!

因为当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)和乘法逆元的知识了。

下面给出AC代码:

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

HDU 1014 Uniform Generator【GCD,水】的更多相关文章

  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(题解)

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

  4. HDU 1014 Uniform Generator 题解

    找到规律之后本题就是水题了.只是找规律也不太easy的.证明这个规律成立更加不easy. 本题就是求step和mod假设GCD(最大公约数位1)那么就是Good Choice,否则为Bad Choic ...

  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. HDOJ 1014 Uniform Generator(公约数问题)

    Problem Description Computer simulations often require random numbers. One way to generate pseudo-ra ...

随机推荐

  1. 一个两年java程序猿的2017个人总结

    前言 又到了一年中最后的日子了,相信有不少公司要求员工写年度总结了,我也不例外.不过个人感觉在公司的写个年度总结来说,过于模板化了.其实很多没有必要.总之,本篇的个人总结,是按照个人的想法写的.简而言 ...

  2. [置顶] xamarin android使用gps定位获取经纬度

    看了文章你会得出以下几个结论 1.android定位主要有四种方式GPS,Network(wifi定位.基站定位),AGPS定位 2.绝大部分android国产手机使用network进行定位是没有作用 ...

  3. GAME——转圈游戏

    我们在生命的路上常常绝望 大概是因为弯路走了太多 脚上的泡被磨起又磨破 像我们所有的幻想与梦 起起落落. 所以说 我这道题考场上面和题解想得一模一样啊啊啊啊啊啊啊啊啊啊!!!!!! 但是就是打复杂了啊 ...

  4. 本机向windows服务器传输文件的三种方法

    闲来无事,在腾讯云上申请了一个免费的服务器,想将自己写的网页发布到服务器上,服务器的申请很简单,百度搜索 腾讯云 ,然后新人第一次注册能申请到免费一个月的云主机,虽然配置不怎么高,但是还是能用的,这是 ...

  5. 搭建redis cluster

    1  下载 redis安装包 tar zxvf redis-3.0.2.tar.gz cd redis-3.0.2/ make make install 2安装ruby sudo apt-get in ...

  6. SqlServer Lock_Escalation

    在今天的文章里,我想谈下SQL Server里锁升级(Lock Escalations).锁升级是SQL Server使用的优化技术,用来控制在SQL Server锁管理里把持锁的数量.我们首先用SQ ...

  7. Spring MVC报错:The request sent by the client was syntactically incorrect ()

    原因: 1.参数名称不一致 2.参数类型不一致

  8. PHP 微信公众号-创建菜单-配置

    1.服务号 2.基本配置 注意: URL: 确保能访问到你对应的文件 Token:随意设置,但是要与文件里的一致 3.网页授权 注意:填写网站域名 4.更具实际需求创建菜单

  9. Android Studio 查看手机CPU信息

    在Android开发中,我们想要获取手机是什么CPU架构,可以通过下面方式: 1.进入adb 终端 adb shell 2.进入proc目录 cd /proc/ 3.查看cpu信息 cat cpuin ...

  10. PE文件详解(六)

    这篇文章转载自小甲鱼的PE文件详解系列原文传送门 之前简单提了一下节表和数据目录表,那么他们有什么区别? 其实这些东西都是人为规定的,一个数据在文件中或者在内存中的位置基本是固定的,通过数据目录表进行 ...