洛谷P1072 Hankson 的趣味题

题目
x和\(a_0\)的最大公约数是\(a_1\)
x和\(b_0\)的最小公倍数是\(b_1\)
\(gcd(x,a_0)=a_1\)
\(gcd(x/a_1,a_0/a_1)=1\) \(\tag{1}\)
\(lcm(x_1,b_0)=b_1\)
\(\frac{x*b_0}{gcd(x,b_0)}=b_1\)
\(gcd(x,b_0)=\frac{x*b_0}{b_1}\)
\(gcd(x/\frac{x * b_0}{b_1},b_0/\frac{x*b_0}{b_1})=1\)
化简可得
\(gcd(\frac{b_1}{b_0},\frac{b_1}{x})=1\) \(\tag{2}\)
观察\((1),(2)\)可得\(x\)既是\(a_1\)的倍数又是\(b_1\)的约数
枚举\(b_1\)的约数判断是否满足他是\(a_1\)的倍数并且满足\((1),(2)\)即可
注:那个(1),(2)在最右边..........

Code:

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int P, ans, a0, a1, b0, b1;

int read() {
    int s = 0, w = 1;
    char ch = getchar();
    while(!isdigit(ch)) {if(ch == '-') w = -1; ch = getchar();}
    while(isdigit(ch)) {s =s  * 10 + ch - '0'; ch = getchar();}
    return s * w;
}
int gcd(int x, int y) {
    return y == 0 ? x : gcd(y, x % y);
}
int main() {
    P = read();
    while(P--) {
        a0 = read(), a1 = read(), b0 = read(), b1 = read();
        ans = 0;
        int wz = sqrt(b1);
        for(int i = 1; i <= wz; i++) {
            if(!(b1 % i)) {
                if(!(i % a1) && gcd(i / a1, a0 / a1) == 1 && gcd(b1 / b0, b1 / i) == 1) ans++;
                int j = b1 / i;
                if (i != j) if(!(j % a1) && gcd(j / a1, a0 / a1) == 1 && gcd(b1 / b0, b1 / j) == 1) ans++;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}
上一篇:二级指针(二维数组)


下一篇:洛谷P1072 Hankson的趣味题