• HDU 1014 Uniform Generator


    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
    seed从0开始每次加step并%mod
    问 是否所有seed都在0-mod-1之中
     
    这题一开始没有想得高深算法 直接模拟就过了
     
    后来发现只要step和mod互素 那么就是good choice
     
     1 #include<cstdio>
     2 #include<iostream>
     3 #define MAXN 100010
     4 
     5 using namespace std;
     6 
     7 int step,mod;
     8 
     9 bool vis[MAXN],flag=false;
    10 
    11 int main() {
    12     while(~scanf("%d %d",&step,&mod)) {
    13         int seed=0,ans=0,p=0;
    14         flag=false;
    15         fill(vis,vis+MAXN+1,false);
    16         do {
    17             if(p>=MAXN) break;
    18             p++;
    19             seed=(seed+step)%mod;
    20             if(!vis[seed]) {
    21                 vis[seed]=true;
    22                 ans++;
    23             }
    24         }while(ans<mod);
    25         for(int i=1;i<mod;i++)
    26           if(!vis[i]) {
    27               flag=true;
    28               printf("%10d%10d    Bad Choice
    ",step,mod);
    29               printf("
    ");
    30               break;
    31           } 
    32         if(!flag) printf("%10d%10d    Good Choice
    ",step,mod),printf("
    ");
    33     }
    34     return 0;
    35 } 
    模拟
     1 #include<cstdio>
     2 #include<iostream>
     3 
     4 using namespace std;
     5 
     6 int step,mod;
     7 
     8 inline int gcd(int a,int b) {
     9     if(b==0) return a;
    10     else return gcd(b,a%b);
    11 }
    12 
    13 int main() {
    14     while(~scanf("%d %d",&step,&mod)) {
    15         if(gcd(step,mod)==1) 
    16           printf("%10d%10d    Good Choice
    ",step,mod),printf("
    ");
    17         else printf("%10d%10d    Bad Choice
    ",step,mod),printf("
    ");
    18     }
    19     return 0;
    20 }
    gcd


    作者:乌鸦坐飞机
    出处:http://www.cnblogs.com/whistle13326/
    新的风暴已经出现 怎么能够停止不前 穿越时空 竭尽全力 我会来到你身边 微笑面对危险 梦想成真不会遥远 鼓起勇气 坚定向前 奇迹一定会出现

     
  • 相关阅读:
    java之this关键字
    单位转换类UnitUtil2
    重量WeightFormatUtil辅助类
    语音提示辅助类MySoundAlertUtil
    Android 编程下 Touch 事件的分发和消费机制
    switch case语句的用法
    Struts2之环境配置
    CSS属性绘制图形(一)
    jquery $(document).ready() 与window.onload的区别
    Android开发之ActionBar
  • 原文地址:https://www.cnblogs.com/whistle13326/p/7156637.html
Copyright © 2020-2023  润新知