# bzoj2005 [Noi2010]能量采集

http://www.elijahqi.win/archives/2747
Description

Input

Output

Sample Input
【样例输入1】

5 4

【样例输入2】

3 4

Sample Output
【样例输出1】

36

【样例输出2】

20

$2\ast gcd\left(i,j\right)-1$$2*gcd(i,j)-1$

#include<cstdio>
#include<algorithm>
#define N 110000
#define ll long long
#define rg register
using namespace std;
int mu[N],prime[N],tot,n,m;
bool not_prime[N];
int main(){
//freopen("bzoj2005.in","r",stdin);
scanf("%d%d",&n,&m);int mx=max(n,m);mu[1]=1;
for (rg int i=2;i<=mx;++i){
if (!not_prime[i]) mu[i]=-1,prime[++tot]=i;
for (rg int j=1;prime[j]*i<=mx;++j){
not_prime[i*prime[j]]=1;
if (i%prime[j]==0){mu[i*prime[j]]=0;break;}else mu[i*prime[j]]=-mu[i];
}
}ll ans=0;int last=1;int mn=min(m,n);
for (rg int i=1;i<=mx;++i) mu[i]+=mu[i-1];
//for (rg int i=1;i<=mx;++i) printf("%d ",mu[i]);puts("");
for (rg int i=1;i<=mn;++i){
ll tmp=0;
for (rg int j=1;j<=mn/i;j=last+1){
last=min((m/i)/(m/(j*i)),(n/i)/(n/(j*i)));
tmp+=(ll)(m/(j*i))*(n/(j*i))*(mu[last]-mu[j-1]);//printf("%lld\n",ans);
}ans+=i*tmp<<1;
}ans-=(ll)n*m;
printf("%lld\n",ans);
return 0;
}

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