NOIP2009Hankson 的趣味题

本文介绍了一种通过暴力枚举解决洛谷1072问题的方法。该问题涉及数学运算,如最大公约数及整除条件。文章给出了详细的C++实现代码,包括如何验证x是否符合条件的具体步骤。

题目来源:https://www.luogu.org/problem/show?pid=1072

 

这题暴力枚举就可以过。

 

由(x,a0)=a1,[x,b0]=b1

 

推出a1|x,x|b1,(x/a1,a0/a1)=1,(b1/x,b1/b0)=1。

 

枚举x。

 

代码:

 

 

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

int main() {
    int _;
    scanf("%d", &_);
    for (int k = 1; k <= _; k++) {
        int a0, a1, b0, b1;
        scanf("%d%d%d%d", &a0, &a1, &b0, &b1);
        if (a0 % a1 != 0 || b1 % b0 != 0) {
            printf("0\n");
            continue;
        }
        int tot = 0;
        for (int x = 1; x * x <= b1; x++) {
            if (b1 % x == 0) {
                if ((x % a1 == 0 && gcd(x / a1, a0 / a1) == 1) && (gcd(b1 / x, b1 / b0) == 1))tot++;
                if (x * x == b1)continue;
                int t = b1 / x;
                if (t % a1 == 0 && (gcd(t / a1, a0 / a1) == 1) && (gcd(b1 / t, b1 / b0) == 1))tot++;
            }
        }
        printf("%d\n", tot);
    }
    return 0;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值