# BZOJ 2005: [Noi2010]能量采集 莫比乌斯反演

$2\times\sum_{i=1}^{n}\sum_{j=1}^{m}gcd(i,j)-2\times\sum_{i=1}^{n}\sum_{j=1}^{m}1$

$\Rightarrow 2\times\sum_{i=1}^{n}\sum_{j=1}^{m}gcd(i,j)-n\times m$

$\Rightarrow \sum_{d=1}^{n}d\sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j)==d]$

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

$\Rightarrow \sum_{i=1}^{\frac{n}{d}} \sum_{j=1}^{\frac{m}{d}}[gcd(i,j)==1]$

$\Rightarrow \sum_{i=1}^{\frac{n}{d}} \sum_{j=1}^{\frac{m}{d}}\sum_ {b|i,b|j}\mu(b)$

$\Rightarrow \sum_{b=1}^{\frac{n}{d}}\mu(b)\sum_{b|i}\sum_{b|j}$

$\Rightarrow re=\sum_{d=1}^{n}d\times calc(\frac{n}{d},\frac{m}{d})$

#include<bits/stdc++.h>
#define ll long long
#define maxn 100104
#define M 100002
using namespace std;
inline void setIO(string s)
{
string in=s+".in";
freopen(in.c_str(),"r",stdin);
}
int cnt;
bool vis[maxn];
int mu[maxn], prime[maxn];
ll sumv[maxn],sum2[maxn];
inline ll calc(ll n,ll m)
{
ll re=0;
int i,j;
for(i=1;i<=n;i=j+1)
{
j=min(n/(n/i), m/(m/i));
re+=(sumv[j]-sumv[i-1])*(n/i)*(m/i);
}
return re;
}
inline ll solve(ll n,ll m)
{
ll re=0;
int i,j;
for(i=1;i<=n;i=j+1)
{
j=min(n/(n/i), m/(m/i));
re+=(sum2[j]-sum2[i-1])*calc(n/i,m/i);
}
return re;
}
int main()
{
// setIO("input");
int i,j,T,n,m;
mu[1]=1;
for(i=2;i<=M;++i)
{
if(!vis[i]) prime[++cnt]=i, mu[i]=-1;
for(j=1;j<=cnt&&1ll*prime[j]*i<=M;++j)
{
vis[prime[j]*i]=1;
if(i%prime[j]==0)
{
mu[i*prime[j]]=0;
break;
}
mu[i*prime[j]]=-mu[i];
}
}
for(i=1;i<=M;++i) sumv[i]=sumv[i-1]+1ll*mu[i], sum2[i]=sum2[i-1]+1ll*i;
scanf("%d%d",&n,&m);
if(n>m) swap(n,m);
printf("%lld\n",solve(n,m)*2-1ll*n*m);
return 0;
}


posted @ 2019-06-26 11:05  EM-LGH  阅读(112)  评论(0编辑  收藏  举报