题意:
求i=1∑nj=1∑m[gcd(i,j)==d]
这个题目很明显就是一个莫比乌斯反演的板子题
我们令f(x)=i=1∑nj=1∑m[gcd(i,j)=x]
根据莫比乌斯反演的基本定理
g(x)=x∣d∑f(d)=i=1∑nj=1∑m[x∣gcd(i,j)]
∵[x∣gcd(i,j)]
∴x∣i且x∣j
g(x)=i=1∑nj=1∑m[x∣gcd(i,j)]=i=1∑⌊xn⌋j=1∑⌊xm⌋=⌊xn⌋⌊xm⌋
f(x)=x∣d∑g(d)μ(xd)=x∣d∑μ(xd)⌊dn⌋⌊dm⌋
x∣d∑μ(xd)⌊dn⌋⌊dm⌋=i=1∑⌊xn⌋μ(i)⌊ixn⌋⌊ixm⌋=i=1∑⌊xn⌋μ(i)i⌊xn⌋i⌊xm⌋
到了这一步,只需要运用数论分块进行处理,把复杂度降到
O(n
)
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define re register
#define gc getchar()
#define ll long long
inline int read()
{
re int x(0); re char ch(gc);
while(ch>'9'||ch<'0') ch=gc;
while(ch>='0'&&ch<='9') x=(x*10)+(ch^48),ch=gc;
return x;
}
const int N=50050;
int mu[N],pri[N],sum[N],vis[N],cnt;
void get_mu(int n)
{
mu[1]=1;
for(int i=2;i<=n;++i)
{
if(!vis[i])
mu[i]=-1,pri[++cnt]=i;
for(int j=1;j<=cnt&&i*pri[j]<=n;++j)
{
vis[i*pri[j]]=1;
if(i%pri[j]==0) break;
else mu[i*pri[j]]=-mu[i];
}
}
for(int i=1;i<=n;++i) sum[i]=sum[i-1]+mu[i];
}
void work()
{
int a=read(),b=read(),d=read();
int n=min(a,b),ans=0;
for(int l=1,r;l<=n;l=r+1)
{
r=min(a/(a/l),b/(b/l));
ans+=(a/(l*d))*(b/(l*d))*(sum[r]-sum[l-1]);
}
cout<<ans<<endl;
}
int main()
{
int T=read();
get_mu(50050);
while(T--) work();
return 0;
}