思路:gcd(a,b)=k<=>gcd(a/k,b/k)=1,令x=a/k,y=b/k,则问题变为问x<=a/d,y<=b/d有多少(x,y)满足gcd(x,y)=1。
这个问题可以用容斥原理求解,令全集为所有(x,y),性质为p[i]|gcd(x,y),p[i]为质数,那么答案就是所有p[i]|gcd(x,y)的并集的补集,显然全集为x*y,计算补集时就是满足的(x,y)个数,容易发现这一项在最终的式子中的系数就是
而满足k|gcd(x,y)的(x,y)数量为(x/k)*(y/k)个。
于是通过预处理莫比乌斯函数前缀和以及进行数论分块,可以算出答案
代码:
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
//#define int LL
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#pragma warning(disable :4996)
const double eps = 1e-8;
const LL mod = 1000000009;
const LL MOD = 998244353;
const int maxn = 50010;
LL N, A, B, D;
LL v[maxn], prime[maxn], mu[maxn];
LL mus(LL n)//初始化
{
mu[0] = 0, mu[1] = 1;
memset(v, 0, sizeof(v));
int m = 0;
for (LL i = 2; i <= n; i++)
{
if (!v[i])
{
v[i] = i;
prime[++m] = i;//素数
mu[i] = -1;
}
for (LL j = 1; j <= m; j++)
{
if (prime[j] > v[i] || prime[j] > n / i)
break;
v[prime[j] * i] = prime[j];
if (i % prime[j] == 0)
mu[i * prime[j]] = 0;
else
mu[i * prime[j]] = -mu[i];
}
}
for (int i = 1; i <= n; i++)//改为记录前缀和
mu[i] += mu[i - 1];
return m;
}
void solve()
{
LL ans = 0;
LL X = A / D, Y = B / D;
LL K = min(X, Y);
for (LL l = 1, r; l <= K; l = r + 1)
{
r = min(X / (X / l), Y / (Y / l));
ans += (mu[r] - mu[l - 1]) * (X / r) * (Y / r);
}
printf("%lld\n", ans);
}
int main()
{
scanf("%lld", &N);
mus(50001);
while (N--)
{
scanf("%lld%lld%lld", &A, &B, &D);
solve();
}
return 0;
}