现在想想就是个显然题嘛,由于刚学莫比乌斯反演,不记得把(F)直接化简,推了几分钟式子就直接看题解了(emmm)
(f(d)=sum_{i=1}^msum_{j=1}^m[gcd(i,j)=d])
(T)次查询,给出(n,m,d)
(F(d)=sum_{d|n}f(n))
则有:
(f(d)=sum_{d|n}mu(frac{n}{d})F(n))
考虑枚举(x=frac{n}{d})
(f(d)=sum_{x=1}^{min(frac{n}{d},frac{m}{d})}mu(x)F(xd))
(f(d)=sum_{x=1}^{min(frac{n}{d},frac{m}{d})}mu(x)leftlfloorfrac{n}{xd} ight floor leftlfloorfrac{m}{xd} ight floor)
分块使得(leftlfloorfrac{n}{xd} ight floor leftlfloorfrac{m}{xd} ight floor)相等
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
using namespace std;
typedef long long LL;
const LL maxn=50000+10;
inline int 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<<3)+(x<<1)+c-'0',c=getchar();
}
return x*f;
}
int T;
int prime[maxn],mu[maxn],sum[maxn];
bool visit[maxn];
inline void F_phi(LL N){
mu[1]=1;
int tot=0;
for(int i=2;i<=N;++i){
if(!visit[i]){
mu[i]=-1,
prime[++tot]=i;
}
for(int j=1;j<=tot&&i*prime[j]<=N;++j){
visit[i*prime[j]]=true;
if(i%prime[j]==0)
break;
else
mu[i*prime[j]]=-mu[i];
}
}
for(int i=1;i<=N;++i)
sum[i]=sum[i-1]+1ll*mu[i];
}
int main(){
T=Read();
F_phi(50000);
while(T--){
int n=Read(),m=Read(),d=Read();
if(n>m)
swap(n,m);
LL ans(0);
int N=n/d;
for(int l=1,r;l<=N;l=r+1){
r=min((n/d)/(n/l/d),(m/d)/(m/l/d));
ans=ans+1ll*(sum[r]-sum[l-1])*1ll*((n/l/d)*1ll*(m/l/d));
}
printf("%lld
",ans);
}
return 0;
}/*
1
50000 50000 8
*/