莫比乌斯反演
#include <iostream>
#include <cstdio>
#define LL long long
using namespace std;
LL read()
{
LL x=0,f=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=x*10+c-'0';
c=getchar();
}
return f*x;
}
const int N=1e5;
LL mu[N],sum[N],prime[N],num,n,m,t,d,ans,next;
bool pri[N];
LL gcd(LL a,LL b)
{
if(!b) return a;
return gcd(b,a%b);
}
int main()
{
n=50000; mu[1]=1; pri[1]=1;
for(int i=2;i<=n;i++)
{
if(!pri[i])
{
prime[++num]=i;
mu[i]=-1;
}
for(int j=1;j<=num&&i*prime[j]<=n;j++)
{
pri[i*prime[j]]=1;
mu[i*prime[j]]=-mu[i];
if(i%prime[j]==0)
{
mu[i*prime[j]]=0;
break;
}
}
}
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+mu[i];
t=read();
while(t--)
{
n=read(); m=read(); d=read(); ans=0;
if(n>m) swap(n,m);
n/=d; m/=d;
for(LL i=1;i<=n;i=next)
{
next=min(n/(n/i),m/(m/i))+1;
ans+=(sum[next-1]-sum[i-1])*(n/i)*(m/i);
}
cout<<ans<<"\n";
}
return 0;
}