HDU 1014.Uniform Generator【模拟及优化】【8月16】

本文探讨了伪随机数生成器的工作原理及如何通过选择合适的参数(步长和模数)来确保生成的随机数在指定范围内均匀分布。通过实例分析,展示了如何通过输入参数判断其是否能实现这一目标。

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
给定step、mod     根据seed[i]=(seed[i-1]+step)%mod产生随机数使0~mod-1全部出现。如果0~mod-1全部出现,则绝对是在seed[mod-1]生成后出现,不然肯定进入了循环。那么产生前mod项,排序遍历就可以了。要注意输出格式。代码如下:

#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
    int step,mod;
    while(scanf("%d%d",&step,&mod)!=EOF){
        int seed[100010]={0};
        for(int i=1;i<mod;i++)
            seed[i]=(seed[i-1]+step)%mod;
        bool flag=true;
        sort(seed,seed+mod);
        for(int i=0;i<mod;i++)
        if(seed[i]!=i){
            flag=false;
            break;
        }
        if(flag) printf("%10d%10d    Good Choice\n\n",step,mod);
        else printf("%10d%10d    Bad Choice\n\n",step, mod);
    }
    return 0;
}
以上代码不管出现不出现循环,都做完了前mod项。时间上会长一些,可以对已产生数做标记,如果又产生了这个数,则陷入循环。时间上会短一些。代码如下:

#include<cstdio>
int main(){
    int step,mod;
    while(scanf("%d%d",&step,&mod)!=EOF){
        int seed[100010]={0},fla[100010]={0};
        bool flag=true;
        for(int i=1;i<mod;i++){
            seed[i]=(seed[i-1]+step)%mod;
            if(fla[seed[i]]){//产生循环
                flag=false;
                break;
            }
            fla[seed[i]]=1;//标记
        }
        if(flag){//无循环
            sort(seed,seed+mod);
            for(int i=0;i<mod;i++)
            if(seed[i]!=i){
                flag=false;
                break;
            }
            if(flag) printf("%10d%10d    Good Choice\n\n",step,mod);
            else printf("%10d%10d    Bad Choice\n\n",step, mod);
        }
        else printf("%10d%10d    Bad Choice\n\n",step, mod);
    }
    return 0;
}
优化前:46MS  优化后:31MS

(或许这不应该叫做优化)

为什么不明显呢?首先,本身100000对于计算机不是特别大的数,其次,优化后100000循环里多了操作,时间并没有降下太多。这样做对于产生循环的操作是优化,而不产生循环的操作就是无用功。对于更大的数或许就有作用了。或许这样做也不能叫优化~
怎么办?

然后我又想,生成随机数,直到产生循环为止。如果0~mod-1都产生了,则最多做mod次。如果0~mod-1没有产生就进入循环,则少做了很多次。代码如下:

#include<cstdio>
int main(){
    int step,mod;
    while(scanf("%d%d",&step,&mod)!=EOF){
        int seed=0,f[100010]={0};
        bool flag=true;
        while(!f[seed]){//直到产生循环
            f[seed]=1;
            seed=(seed+step)%mod;
        }
        for(int i=0;i<mod;i++)
        if(!f[i]){//不是0~mod-1都产生了
            flag=false;
            break;
        }
        if(flag) printf("%10d%10d    Good Choice\n\n",step,mod);
        else printf("%10d%10d    Bad Choice\n\n",step, mod);
    }
    return 0;
}

第一次:46MS

第二次:31MS

本次优化(可以叫做优化了吧):15MS

但是还是有问题,上面还要一个循环判断0~mod-1是否都产生。仔细分析,如果0~mod-1都产生了,则第mod+1次就产生循环。只需判断第几次出现循环就好了。第mod+1次出现循环就Good Choice;否则就Bad Choice。代码如下:

#include<cstdio>
int main(){
    int step,mod;
    while(scanf("%d%d",&step,&mod)!=EOF){
        int seed=0,f[100010]={0},cnt=0;
        while(!f[seed]){//直到产生循环
            f[seed]=1;
            seed=(seed+step)%mod;
            cnt++;
        }
        if(cnt==mod) printf("%10d%10d    Good Choice\n\n",step,mod);
        else printf("%10d%10d    Bad Choice\n\n",step, mod);
    }
    return 0;
}

第一次:46MS

第二次:31MS

第三次优化:15MS

第四次优化:0MS(HDU测试的)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值