洛谷P1072 Hankson 的趣味題(數學)

題意

題目鏈接

Sol

充滿套路的數學題。。

如果你學過莫比烏斯反演的話不難得到兩個等式

\[gcd(\frac{x}{a_1}, \frac{a_0}{a_1}) = 1\]

\[gcd(\frac{b_1}{b_0}, \frac{b_1}{x}) = 1\]

然後枚舉\(b_1\)的約數就做完了。。

// luogu-judger-enable-o2
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN = 1e6;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int T, a0, a1, b0, b1, ans;
int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}
void check(int x) {
    if(x % a1) return ;
    ans += (gcd(x / a1, a0 / a1) == 1 && gcd(b1 / b0, b1 / x) == 1);
}
int main() {
    T = read();
    while(T--) {
        a0 = read(), a1 = read(), b0 = read(), b1 = read(); ans = 0;
        for(int x = 1; x * x <= b1; x++) {
            if(b1 % x == 0) {
                check(x);
                if(b1 != x) check(b1 / x);
            }
        }
        cout << ans << endl;
    }
    return 0; 
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章