# bzoj 2005 能量采集 莫比乌斯反演

ni=1mj=1gcd(i,j)$\sum_{i=1}^{n}\sum_{j=1}^{m}{gcd(i,j)}$
=ni=1mj=1d|gcd(i,j)ϕ(d)$\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{d|gcd(i,j)}{\phi(d)}$
=min(n,m)d=1ϕ(d)×nd×md$\sum_{d=1}^{min(n,m)}{\phi(d)\times \lfloor \frac{n}{d} \rfloor \times \lfloor \frac{m}{d} \rfloor}$

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<ctime>
#define N 100500
using namespace std;
int prime[N],tot,n,m;
long long phi[N],ans;
bool bo[N];
void init(){
phi[1]=1;
for(int i=2;i<=n;i++){
if(!bo[i]){
prime[++tot]=i;
phi[i]=i-1;
}
for(int j=1;j<=tot&&i*prime[j]<=n;j++){
bo[i*prime[j]]=1;
if(i%prime[j]==0){
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
else phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
for(int i=1;i<=n;i++)phi[i]+=phi[i-1];
}
int main(){
scanf("%d%d",&n,&m);
if(n>m)swap(n,m);
init();
for(int i=1,j;i<=n;i=j+1){
j=min(n/(n/i),m/(m/i));
ans+=(long long)(phi[j]-phi[i-1])*(n/i)*(m/i);
}
ans*=2; ans-=(long long)n*m;
printf("%lld\n",ans);
return 0;
}

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客