HDOJ 1014

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

 1 #include <iostream>
 2 #include <cstring>
 3 #include <set>
 4 #include <cstdio>
 5
 6 using namespace std;
 7
 8
 9 int main(){
10     int step,mod,v=0,n;
11     set<int> s;
12     set<int>::iterator it;
13     while(cin>>step>>mod){
14         printf("%10d%10d",step,mod);
15         n = mod-1;
16         s.insert(v);
17         while(n--){
18             v = (v+step)%mod;
19             it = s.find(v);
20             if(it!=s.end()){
21                 break;
22             }
23             s.insert(v);
24         }
25         if(s.size()== mod)printf("    Good Choice\n\n");
26         else printf("    Bad Choice\n\n");
27         s.clear();
28     }
29 }
时间: 2024-10-21 14:19:01

HDOJ 1014的相关文章

HDOJ 1014 模拟

题目大意:给出两个数 ,s,m,按照题目中的要求计算生成的随机数列,判断是否生成了0~m-1这m个数列,若是则是好的选择,否则是坏的选择. 算法思想; 用一个标记flag[MAXN]来记录前0~m-1是否生成,同时用num记录数列的长度,循环判断当前的数是否先前生成过,若没有则标记,否及判断num是否等于m,若等则是一个好的选择,不等则不是一个好选择.输出时s,m均占10个字节有队齐,字符串从25列开始输出,左对齐.每个用例后空一行. 代码如下: #include <iostream> #in

Hdoj 1014 Uniform Generator

题目:点击打开链接 分析:题目要求均匀产生随机数,有两种方法:1 对产生的数进行标记 2 要产生随机数STEP和MOD的最大公约数不能超过1 代码一: #include<stdio.h> #include<string.h> int a1[100010]; int main() { int STEP,MOD; while(~scanf("%d%d",&STEP,&MOD)) { int i; int k=0; memset(a1,0,sizeof

HDOJ 题目分类

HDOJ 题目分类 /* * 一:简单题 */ 1000:    入门用:1001:    用高斯求和公式要防溢出1004:1012:1013:    对9取余好了1017:1021:1027:    用STL中的next_permutation()1029:1032:1037:1039:1040:1056:1064:1065:1076:    闰年 1084:1085:1089,1090,1091,1092,1093,1094, 1095, 1096:全是A+B1108:1157:1196:1

【HDOJ】4328 Cut the cake

将原问题转化为求完全由1组成的最大子矩阵.挺经典的通过dp将n^3转化为n^2. 1 /* 4328 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector>

POJ Xiangqi 4001 &amp;&amp; HDOJ 4121 Xiangqi

题目链接(POJ):http://poj.org/problem?id=4001 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=4121 Xiangqi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1108   Accepted: 299 Description Xiangqi is one of the most popular two-player boa

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

HDOJ 4901 The Romantic Hero

DP....扫两遍组合起来 The Romantic Hero Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 547    Accepted Submission(s): 217 Problem Description There is an old country and the king fell in love with a

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]