HDU1014 Uniform Generator (数论)

HDU1014 Uniform Generator (数论)

原题地址:

题意

给出STEP和MOD的值,问能否通过给出的式子得到1到mod-1的全部数。

思路

其实只需要判断两个数是否互质就可以了,判断互质只需要使用辗转相除法判断最大公约数是否为1即可。

辗转相除法:

用较小数除较大数,再用出现的余数(第一余数)去除除数,再用出现的余数(第二余数)去除第一余数,如此反复,直到最后余数是0为止。如果是求两个数的最大公约数,那么最后的除数就是这两个数的最大公约数。

PS:这题的输出格式有毒。。。

AC代码

#include <bits/stdc++.h>

using namespace std;

/**
 *  Created with IntelliJ Clion.
 *  @author  wanyu
 *  @Date: 2018-02-25
 *  @Time: 23:10
 *  To change this template use File | Settings | File Templates.
 * 
 */


int a, b;

bool prime() {//辗转相除法
    bool flag;
    int dividend = max(a, b);
    int divisor = min(a, b);
    while (true) {
        int temp = dividend % divisor;//看能否整除
        if (temp == 0) {//如果能够整除看除数是否为1
            flag = divisor == 1;
            break;
        }
        dividend = divisor;
        divisor = temp;

    }
    return flag;
}

int HDU1014() {
    while (~scanf("%d %d", &a, &b)) {
        bool flag = prime();
        if (flag) {
            printf("%10d%10d    Good Choice\n\n", a, b);
        } else {
            printf("%10d%10d    Bad Choice\n\n", a, b);
        }

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值