# Solution

$\large \sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j)\in prime]$

$\large \sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{k\in prime}^{n}[gcd(i,j)= k]$

$\large \sum_{k\in prime}^{n}\sum_{i=1}^{n/k}\sum_{j=1}^{m/k}[gcd(i,j)= 1]$ (在这里除号指向下取整)

$\large \sum_{k\in prime}^{n}\sum_{i=1}^{n/k}\sum_{j=1}^{m/k}\sum_{d|gcd(i,j)}\mu(d)$

$\large \sum_{k\in prime}^{n}\sum_{i=1}^{n/k}\sum_{j=1}^{m/k}\sum_{d=1}^{n/k}μ(d)[d|gcd(i,j)]$ （因为前面$i,j$中最小的是$n/k$,所以说我们这里$d$的最大值也为$n/k$

$\large \sum_{k\in prime}^{n}\sum_{d=1}^{n/k}\sum_{i=1}^{n/k}\sum_{j=1}^{m/k}μ(d)[d|gcd(i,j)]$

$\large \sum_{k\in prime}^{n}\sum_{d=1}^{n/k}μ(d)\sum_{i=1}^{n/k}\sum_{j=1}^{m/k}[d|gcd(i,j)]$

$\large \sum_{k\in prime}^{n}\sum_{d=1}^{n/k}μ(d)\frac{n}{k*d}\frac{m}{k*d}$

$\large \sum_{k\in prime}^{n}\sum_{d=1}^{n/k}μ(\frac{T}{k})\frac{n}{T}\frac{m}{T}$

$\large \sum_{T=1}^{n}\frac{n}{T}\frac{m}{T}\sum_{(k\in prime,k|T)}μ(\frac{T}{k})$

# Code

//Luogu P2257 YY的GCD
//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=10000000+1000;
const int M=10000000;
int mu[N],prime[N],cnt_p;
bool noPrime[N];
void GetPrime(int n)
{
mu[1]=1;
noPrime[1]=true;
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 f[N],pre_f[N];
int main()
{
int t=clock();
GetPrime(M);
for(int i=1;i<=cnt_p;i++)
for(int j=1;prime[i]*j<=M;j++)
f[prime[i]*j]+=mu[j];
for(int i=1;i<=M;i++)
pre_f[i]=pre_f[i-1]+f[i];

for(;T>0;T--)
{
if(n>m) swap(n,m);

int l=1,r=1;
long long ans=0;
for(;l<=n;l=r+1)
{
r=min(n/(n/l),m/(m/l));
ans+=(pre_f[r]-pre_f[l-1])*(n/l)*(m/l);
}

printf("%lld\n",ans);
}
cerr<<clock()-t;
return 0;
}



posted @ 2019-01-22 11:27  GoldenPotato  阅读(677)  评论(0编辑  收藏  举报