1014 Uniform Generator
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 28661 Accepted Submission(s): 11402
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.
15 20
63923 99999
15 20 Bad Choice
63923 99999 Good Choice
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define N 100000
int main()
{
int step,mod;
while(scanf("%d%d",&step,&mod)!=EOF)
{
int a[N],m=;
a[]=;
for(int i=;i<mod;i++)
{
a[i]=(a[i-]+step)%mod;
}
sort(a,a+mod);
for(int i=;i<mod;i++)
{
if(a[i]!=i)
{
m=;
break;
}
}
if(m==)
{
printf("%10d%10d",step,mod);
printf(" Bad Choice\n");
}
else
{
printf("%10d%10d",step,mod);
printf(" Good Choice\n");
}
printf("\n");
}
return ;
}
或者(判断两个数是否互质)
#include<stdio.h>
int hz(int t,int m)
{
for(int i=;i<=t&&i<=m;i++)
if(t%i==&&m%i==)
return ;
return ;
}
int main()
{
int t,m;
while(scanf("%d%d",&t,&m)!=EOF)
{
if(hz(t,m))
printf("%10d%10d Good Choice\n\n",t,m);
else
printf("%10d%10d Bad Choice\n\n",t,m);
}
}
1014 Uniform Generator的更多相关文章
- HDU 1014 Uniform Generator(模拟和公式)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1014 Uniform Generator Time Limit: 2000/1000 MS (Java ...
- HDU 1014 Uniform Generator【GCD,水】
Uniform Generator Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1014:Uniform Generator
Uniform Generator Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1014 Uniform Generator(题解)
Uniform Generator Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1014.Uniform Generator 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1014 题目意思:给出 STEP 和 MOD,然后根据这个公式:seed(x+1) = [seed(x) ...
- HDOJ 1014 Uniform Generator(公约数问题)
Problem Description Computer simulations often require random numbers. One way to generate pseudo-ra ...
- HDU 1014 Uniform Generator 欧几里得
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1014 解题思路: 1. 把题目意思读懂后,明白会输入两个数,然后根据题中的公式产生一系列伪随机数,看这 ...
- 1014 Uniform Generator ACM
http://acm.hdu.edu.cn/showproblem.php?pid=1014 题目的英文实在是太多了 ,搞不懂. 最后才知道是用公式seed(x+1) = [seed(x) + STE ...
- HDU 1014 Uniform Generator 题解
找到规律之后本题就是水题了.只是找规律也不太easy的.证明这个规律成立更加不easy. 本题就是求step和mod假设GCD(最大公约数位1)那么就是Good Choice,否则为Bad Choic ...
随机推荐
- macaca环境搭建(web 和 android)
一.安装配置JDK 1.1下载JDK地址http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.h ...
- eclipse项目导入到android studio中文乱码处理
由于eclipse项目是gbk编码,Android studio默认用的是utf-8. 就会导致代码中的汉字,注释全部显示为乱码. 解决方法:在module的bulid.gradle中加入: comp ...
- web works importScripts
html: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...
- XJOIWinterCampPrecontest1-P2队列
2 排队2.1 题目有n 个人打水,第i 个人打水需要ai 的时间.有K 个水龙头,你可以随意安排他们打水的顺序以及使用哪一个水龙头,但是每一时刻每一个水龙头只能有一个人使用且一个人一旦开始打水就不能 ...
- 老李推荐:第8章6节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-启动Monkey 4
在获得比对设备序列号后,findAttachedDevice就会跟提供的序列号进行比对,如果吻合就返回给调用者” 代码8-6-3 AdbBackend - waitForConnection”了.而A ...
- Redis分布式锁
Redis分布式锁 分布式锁是许多环境中非常有用的原语,其中不同的进程必须以相互排斥的方式与共享资源一起运行. 有许多图书馆和博客文章描述了如何使用Redis实现DLM(分布式锁管理器),但是每个库都 ...
- jdk1.8新特性,还不知道的朋友还不看看,1.9都快出来了
一.接口的默认方法 Java 8允许我们给接口添加一个非抽象的方法实现,只需要使用 default关键字即可,这个特征又叫做扩展方法,示例如下:代码如下:interface Formula { ...
- 对quartz定时任务的初步认识
已经好久没有写技术博文了,今天就谈一谈我前两天自学的quartz定时任务吧,我对quartz定时任务的理解,就是可以设定一个时间,然后呢,在这个时间到的时候,去执行业务逻辑,这是我的简单理解,接下来看 ...
- Java将一个目录下的所有数据复制到另一个目录下
/* 将"C:\\JavaProducts\\Source"下的所有数据复制到"C:\\Target"下 */ import java.io.*; public ...
- 简单c语言子集词法分析器
概述 词法分析是编译的第一个环节,其输入是高级语言程序,输出是单词串.词法分析器的主要任务是将高级语言程序作为字符串输入,然后依据词法规则将字符串组合成单词,并输出单词串. 为了方便之后的编译环节,通 ...