NOIP2009 Hankson 的趣味题

题目链接

此题其实就是求满足 (a, x) = b / [c, x] = d的x的个数。

[c, x] = d   =>  x | d

所以x为d的约数

那么此题的思路就很明了了:枚举d的每个约数,求满足条件的数的个数。时间复杂度:$O(nsqrt{d}log{a})$,可是实际操作时远远到不了这个值,可以过。

记得要开long long

代码

#include <iostream>
using namespace std;
//快读 
long long read() {
    long long ret = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar(); 
    }
    while (isdigit(ch)) {
        ret = ret * 10 + ch - '0';
        ch = getchar();
    }
    return ret * f;
}
//快写
void write(long long x) {
    if (x >= 10) write(x / 10);
    putchar(x % 10 + '0');
}
long long t, a, b, c, d, ans;
//求最大公约数(欧几里得算法)
long long gcd(long long x, long long y) {
    return y == 0 ? x : gcd(y, x % y);
}
//求最小公倍数
long long lcm(long long x, long long y) {
    return x * y / gcd(x, y);
}
int main() {
    t = read();
    while (t--) {
        ans = 0;
        a = read(), b = read(), c = read(), d = read();
        for (long long i = 1; i * i <= d; i++) {//从1到sqrt(d)
            if (d % i == 0) {
                if (gcd(a, i) == b && lcm(c, i) == d) ans++;
                if (d / i != i) {
                    if (gcd(a, d / i) == b && lcm(c, d / i) == d) ans++;
                }
            }
        }
        write(ans);
        putchar('
');
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zcr-blog/p/11618531.html