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

Code:

#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;
}


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