UVA - 408 Uniform Generator

来源:互联网 发布:javascript 是否是数组 编辑:程序博客网 时间:2024/06/09 20:51

题目大意:给出 step 和 mod,根据 seed(x+1)=[seed(x)+step]%10mod,问产生的随机数是否均匀分布于 0 到 mod-1。

解题思路:产生随机数存入数组,排序比较。
可以通过判断 gcd(step, mod) 是否等于 1,然而并不能够理解。

#include<iostream> #include<cstdio>#include<string.h>#include<stdlib.h>#include<cmath>using namespace std;int cmp(const void*a,const void*b) {    return *(int*)a -*(int*)b;}int ans[10000010];int main() {    int step, mod;    ans[0]  = 0;    while(scanf("%d%d", &step, &mod) != EOF) {        for (int i = 1; i < mod; i++)            ans[i] = (ans[i-1] + step) % mod;        qsort(ans, mod, sizeof(int), cmp);        int tag = 1;        for (int i = 1; i < mod; i++)            if (ans[i] != i) { tag = 0; break;}        if (tag) printf("%10d%10d    Good Choice\n\n", step, mod);        else printf("%10d%10d    Bad Choice\n\n", step, mod);    }    return 0; }
0 0