传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1014

Uniform Generator

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

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
 
题目意思:
就是给定两个数step, mod然后利用给定的公式求随机数,保证从0到mod减1范围内都均匀出现。
每一个数都出现一次。这样就均等分布了。就打印Good否则就Bad 
 
分析:
有两种做法:
1.直接模拟
枚举产生随机数,然后排序比对
全部匹配就是好的,有一个不是就是坏的
code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m,i;
int s[];
while(cin>>n>>m)
{
s[]=;
for(i=;i<m;i++)
s[i]=(s[i-]+n)%m;
sort(s,s+m);
for(i=;i<m;i++)
if(s[i]!=i)
break;
printf("%10d%10d",n,m);
if(i==m)
cout<<" Good Choice"<<endl<<endl;
else
cout<<" Bad Choice"<<endl<<endl;
}
return ;
}

2.公式法

这是网上比较神奇的做法(判断一下两个数是不是互质)

就是比较step和mod的最大公约数是不是1

大佬题解:

本题就是求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;

code:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int gcd(int a,int b)//最大公约数
{
if (b==)
return a;
return gcd(b, a%b);
}
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
if(gcd(n,m)==)
printf("%10d%10d Good Choice\n\n",n,m);
else
printf("%10d%10d Bad Choice\n\n",n,m);
}
return ;
}
 

HDU 1014 Uniform Generator(模拟和公式)的更多相关文章

  1. HDU 1014 Uniform Generator【GCD,水】

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

  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 解题报告

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

  5. HDU 1014 Uniform Generator 欧几里得

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

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

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

  7. HDU 1014 Uniform Generator 题解

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

  8. hdu 1014 Uniform Generator 数论

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

  9. 1014 Uniform Generator ACM

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

随机推荐

  1. ReentrantReadWriteLock简介

    对象的方法中一旦加入synchronized修饰,则任何时刻只能有一个线程访问synchronized修饰的方法.假设有个数据对象拥有写方法与读方法,多线程环境中要想保证数据的安全,需对该对象的读写方 ...

  2. redis的持久化方式

    redis有两种持久化方式,第一种是基于快照的持久化方式,第二种是基于文件追加的持久化方式 一.基于快照的持久化 1.修改redis.conf配置文件,开启基于快照的持久化方式 2.修改持久化文件存放 ...

  3. unity3d之技能栏冷却

    绑定在按钮上的脚本 using UnityEngine; using System.Collections; using UnityEngine.UI; public class CdCover : ...

  4. 《Java并发编程实战》读书笔记(一)----- 简介

    简史 早期的计算机中不包含操作系统,从头至尾都只执行一个程序,并且这个程序能访问计算机所有资源.随着计算机发展,操作系统的出现,使得计算机可以同时运行多个程序,并且每程序都在单独的进程内运行.为什么要 ...

  5. 第11章 Media Queries 与Responsive 设计

    Media Queries--媒体类型(一) 随着科学技术不断的向前发展,网页的浏览终端越来越多样化,用户可以通过:宽屏电视.台式电脑.笔记本电脑.平板电脑和智能手机来访问你的网站.尽管你无法保证一个 ...

  6. c# 利用反射 从json字符串 动态创建类的实例 并动态为实例成员赋值

    转自 http://hi.baidu.com/wjinbd/item/c54d43d998beb33be3108fdd 1 创建自己要用的类 class stu { string _name; int ...

  7. 【Supervised Learning】支持向量机SVM (to explain Support Vector Machines (SVM) like I am a 5 year old )

    Support Vector Machines 引言 内核方法是模式分析中非常有用的算法,其中最著名的一个是支持向量机SVM 工程师在于合理使用你所拥有的toolkit 相关代码 sklearn-SV ...

  8. windows线程时间打印

    https://blog.csdn.net/xingcen/article/details/70084029

  9. Python Json获取天气预报

    #!/usr/bin/python # -*- coding: UTF-8 -*- import json import smtplib import urllib from email.mime.t ...

  10. LDA相关论文汇总

    转:http://blog.csdn.net/pirage/article/details/9467547 LDA理论 David M. Blei, Andrew Y. Ng, and Michael ...