UVa 408 - Uniform Generator

来源:互联网 发布:淘宝卖家退款给买家 编辑:程序博客网 时间:2024/06/08 09:01

传送门UVa 408 - Uniform Generator

据说是用到了数论中的完全剩余系. 刚好今天下午去图书馆借了本<<初等数论>>, 待我先去研究一下... 明天再来补充.

做法是求给出的两个数是否互质, 如果是, 就是Good Choice.

话说为什么我看到GCD第一联想就是我党...

详情见代码 I

---------------------------------------------------------------------------------------------------------------

刚试了一下, 这题也可以简单粗暴地过的, 只要判断cnt = mod之前有没有值为0的step即可. 默默地滚去看书了...

详情见代码 II

--------------------------------------------4.27补充---------------------------------------------

我太高估自己了.....看了一个晚上根本看不懂

----------------------------------------------------------------------------------------------------

代码 I

#include <cstdio>#include <cmath>using namespace std;int GCD(int a, int b){    if (b == 0)        return a;    else        return GCD(b, a % b);}int main(){    //freopen("input.txt", "r", stdin);    int a, b;    while (~scanf("%d%d", &a, &b))    {        int temp = GCD(a, b);        printf("%10d%10d", a, b);        if (temp == 1)            printf("    Good Choice\n");        else            printf("    Bad Choice\n");        printf("\n");    }    return 0;}

代码 II


#include <cstdio>#include <cmath>using namespace std;int main(){    //freopen("input.txt", "r", stdin);    int cnt, seed, step, mod;    while (~scanf("%d%d", &step, &mod))    {        cnt = seed = 0;        printf("%10d%10d", step, mod);        while (true)        {            seed = (seed + step) % mod;            cnt++;            if (seed == 0)            {                if (cnt == mod)                    printf("    Good Choice\n");                else                    printf("    Bad Choice\n");                printf("\n");                break;            }        }    }    return 0;}




0 0
原创粉丝点击