HDU1014 Uniform Generator

简介:
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1014

这道题就是题目有点难懂,其实难度很小,就是产生伪随机数时给出的步长step和上限mod,判断下这两个值是否可以让产生的随机数均匀分布,所谓的均匀分布就是在mod步里能产生出0到mod-1这mod个数。

#include <iostream>
using namespace std;

const long int MAX_MOD = 100000;
bool gened[MAX_MOD] = {false};//数是否产生

void doRandomTest(int step,int mod)
{
    int seed = 0,tmp,i;//初始种子值为零
    for(i=0;i<mod;++i)
    {//刚开始数都未产生
        gened[i] = false;
    }
    gened[0] = true;//第一个产生的是零
    do
    {
        tmp = (seed+step)%mod;
        gened[tmp] = true;//第tmp个数已经产生了
        seed = tmp;//更新种子数
    }while(seed!=0);
    for(i=0;i<mod;++i)
    {
        if(gened[i]==false)//有数没有产生出来
            break;
    }
    if(i==mod)
    {
        printf("%10d%10d    Good Choice\n",step,mod);
    }
    else
        printf("%10d%10d    Bad Choice\n",step,mod);
    cout<<endl;
}

int main(int argc,char* argv[])
{
    int step,mod,i;
    while(cin>>step>>mod)
    {
        doRandomTest(step,mod);
    }
    return 0;
}


本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/12/29/1019397.html,如需转载请自行联系原作者
目录
相关文章
|
机器学习/深度学习 算法
poj 1597 Uniform Generator【生成指定范围内所有随机数】
本文参考资料:http://hi.baidu.com/bnjyjncwbdbjnzr/item/1f997cfdd225d5d143c36a58 题意:一个生成随机数的函数,      Seed[x+1] = ( seed[x] + STEP ) % MOD 输入step和mod,问能否生成0~MOD-1之间所有的数,是Good Choice,否则Bad Choice 题意其实就是:给出S和M,求0*S%M,1*S%M,2*S%M......(M-1)*S%M能否组成一个集合包含0.1.。
1004 0
HDOJ 1014 Uniform Generator(公约数问题)
HDOJ 1014 Uniform Generator(公约数问题)
105 0
|
机器学习/深度学习
HDU 1014 Uniform Generator【GCD,水】
Uniform Generator Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29336    Accepted Submissio...
1186 0
UVa1583 - Digit Generator
UVa1583 - Digit Generator
57 0
|
Python
Python random 随机函数(random、uniform、randint、choice、choices、randrange、shuffle、sample)
Python random 随机函数(random、uniform、randint、choice、choices、randrange、shuffle、sample)
163 0
|
人工智能
poj 3187 Backward Digit Sums【next_permutation】
点击打开题目 Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4498   Accepted: 2586 Descript...
900 0
|
算法
poj 1330 Nearest Common Ancestors
点击打开链接poj 1330 思路:LCA+离线Tarjan算法 分析: 1 LCA用于找到一棵树或者一个图中找到两个节点的最近的祖先问题。
1026 0

热门文章

最新文章