bzoj 2005: [Noi2010]能量采集

Solution

\begin{aligned}f&=\sum_{i=1}^{n}\sum_{j=1}^{m}\gcd(i,j)\\ &=\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{d|\gcd(i,j)}\varphi(d)\\ &=\sum_{d=1}^{\min(n,m)}\varphi(d)(\sum_{d|i}^{i\leq n}1)(\sum_{d|j}^{j\leq m}1)\\ &=\sum_{d=1}^{\min(n,m)}\lfloor\frac nd\rfloor\lfloor\frac md\rfloor\varphi(d)\end{aligned}

#include<cstdio>
#include<cstdlib>
#include<cstring>

#define reg register
const int MAXN=100000;
typedef long long ll;
#define int long long

bool vis[MAXN+10];
int p[MAXN+10];
int phi[MAXN+10];
int len=0;
int n,m;
ll ans=0;

void init(){
memset(vis,1,sizeof(vis));phi[1]=1;
for(reg int i=2;i<=MAXN;++i){
if(vis[i]){
p[++len]=i;
phi[i]=i-1;
}
for(reg int j=1;j<=len&&i*p[j]<=MAXN;++j){
vis[i*p[j]]=0;
if(i%p[j])
phi[i*p[j]]=phi[i]*(p[j]-1);
else{
phi[i*p[j]]=phi[i]*p[j];
break;
}
}
}
}
int min(int x,int y){
if(x<y) return x;
return y;
}
signed main(){
init();
scanf("%d%d",&n,&m);
for(reg int i=1;i<=min(n,m);++i)
ans+=(n/i)*(m/i)*phi[i];
printf("%lld",ans+ans-n*m);
}


©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客