HDU—— 1014 Uniform Generator

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014086857/article/details/41146155

题意:对于公式:seed(x+1) = [seed(x) + STEP] % MOD,给定STEP和MOD,如果将STEP和MOD带入公式后所产生的循环中,对于一个循环中能产生0~MOD-1当中的所有数,此时输出Good Choice,否则输出Bad Choice,其中seed[0] = 0。

解题思路:如果公式能在MOD步当中产生0~MOD-1当中的所有数,则此时就可以输出Good Choice,否则输出Bad Choice,因为若想保证在迭代过程中不产生其他无关数字,那么在MOD步当中就应该产生出所有0~MOD-1的数字。详见代码。

Code:

#include <iostream>
#include <cstdio>
using namespace std;
#define maxn 100000

int seed[maxn];
void function(int step,int mod)
{
    int i=0;
    do{
        i++;
        seed[i] = (seed[i-1] + step)%mod;
    }while(seed[i]);
    if(i != mod )
        printf("%10d%10d    Bad Choice\n\n", step, mod);
    else
        printf("%10d%10d    Good Choice\n\n", step, mod);
}

int main()
{
    int step,mod;
    while(scanf("%d%d",&step,&mod)==2)
    {
        seed[0]=0;
        function(step,mod);
    }
    return 0;
}

Uniform Generator

06-11

Problem DescriptionnComputer simulations often require random numbers. One way to generate pseudo-random numbers is via a function of the formnnseed(x+1) = [seed(x) + STEP] % MODnnwhere '%' is the modulus operator. nnSuch 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. nnFor 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. nnIf 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. nnYour program will determine if choices of STEP and MOD will generate a uniform distribution of pseudo-random numbers. n nnInputnEach line of input will contain a pair of integers for STEP and MOD in that order (1 <= STEP, MOD <= 100000).n nnOutputnFor 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.n nnSample Inputn3 5n15 20n63923 99999n nnSample Outputn 3 5 Good Choicenn 15 20 Bad Choicenn 63923 99999 Good Choicen n

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试