一道十分经典的数论题,在考场上也可以用暴力的算法来解决,从而得到\(50pts\)的较为可观的分数,而如果想要AC的话,我们观察原题给的数据范围\(a,b,c,d\)(为了好表示,分别代表a1,a2,b1,b2)。
这样我们可以根据比较容易推出的定理来优化
\[gcd(a,b)==c~=>~gcd(a/c,b/c)==1\]
\(Code\)
#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll __gcd(ll n, ll m)
{
if (m == 0) return n;
return __gcd(m, n % m);
}
ll __lcm(ll n, ll m) {return (n * m / __gcd(n, m));}
ll T; ll a, b, c, d;
inline ll solve(ll a, ll b, ll c, ll d)
{
ll ans = 0;
ll n = (a / b), m = (d / c);
for (int i = 1; i <= (int) sqrt(d); i++)
{
if (d % i != 0) continue;
if (i % b == 0 && __gcd(i / b, n) == 1 && __gcd(m, d / i) == 1) ans++;
if (d / i == i) continue;
ll ha = d / i;
if (ha % b == 0 && __gcd(ha / b, n) == 1 && __gcd(m, d / ha) == 1) ans++;
}
return ans;
}
int main()
{
scanf("%lld", &T);
while (T--)
{
scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
printf("%lld\n", solve(a, b, c, d));
}
}