# Solution

$\large \sum_{i=1}^{a}\sum_{j=1}^{b}[gcd(i,j)=x]$

$\large \sum_{i=1}^{a/x}\sum_{j=1}^{b/x}[gcd(i,j)=1]$

$\large \sum_{i=1}^{a/x}\sum_{j=1}^{b/x}\sum_{d|gcd(i,j)}\mu(d)$

$\large \sum_{i=1}^{a/x}\sum_{j=1}^{b/x}\sum_{d=1}^{a/x}\mu(d)*[d|gcd(i,j)]$

$\large \sum_{d=1}^{a/x}\mu(d)\sum_{i=1}^{a/x}\sum_{j=1}^{b/x}[d|gcd(i,j)]$

$\large \sum_{d=1}^{a/x}\mu(d)\frac{a}{x*d}\frac{b}{x*d}$

OK,到此为止，我们所有东西都可以算了。

# Code

//Luogu P3455 [POI2007]ZAP-Queries
//Jan,22ed,2019
//莫比乌斯反演
#include<iostream>
#include<cstdio>
using namespace std;
{
long long x=0,f=1; char c=getchar();
while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
while(isdigit(c)){x=x*10+c-'0';c=getchar();}
return x*f;
}
const int N=50000+100;
const int M=50000;
int cnt_p,prime[N],mu[N];
bool noPrime[N];
void GetPrime(int n)
{
noPrime[1]=true,mu[1]=1;
for(int i=2;i<=n;i++)
{
if(noPrime[i]==false)
prime[++cnt_p]=i,mu[i]=-1;
for(int j=1;j<=cnt_p and i*prime[j]<=n;j++)
{
noPrime[i*prime[j]]=true;
if(i%prime[j]==0)
{
mu[i*prime[j]]=0;
break;
}
mu[i*prime[j]]=mu[i]*mu[prime[j]];
}
}
}
long long pre_mu[N];
int main()
{
GetPrime(M);
for(int i=1;i<=M;i++)
pre_mu[i]=pre_mu[i-1]+mu[i];

for(;T>0;T--)
{

long long ans=0;
if(a>b) swap(a,b);
a/=x,b/=x;
for(int l=1,r;l<=a;l=r+1)
{
r=min(a/(a/l),b/(b/l));
ans+=(pre_mu[r]-pre_mu[l-1])*(a/l)*(b/l);
}

printf("%lld\n",ans);
}
return 0;
}



posted @ 2019-01-22 15:18  GoldenPotato  阅读(290)  评论(0编辑  收藏  举报