# BZOJ2005 [NOI2010]能量采集

## 能量采集

5 4

3 4

36

20

##### 题解

$\sum _{i=1}^{n}\sum _{j=1}^{m}gcd\left(i,j\right)\phantom{\rule{0ex}{0ex}}\begin{array}{rl}& =\sum _{d=1}^{min\left(n,m\right)}d\sum _{i=1}^{n}\sum _{j=1}^{m}\left[d=gcd\left(i,j\right)\right]\\ & =\sum _{d=1}^{min\left(n,m\right)}d\sum _{i=1}^{⌊\frac{n}{d}⌋}\sum _{j=1}^{⌊\frac{m}{d}⌋}\left[1=gcd\left(i,j\right)\right]\\ & =\sum _{d=1}^{min\left(n,m\right)}d\sum _{i=1}^{⌊\frac{n}{d}⌋}\sum _{j=1}^{⌊\frac{m}{d}⌋}\sum _{{d}^{\prime }|gcd\left(i,j\right)}\mu \left({d}^{\prime }\right)\\ & =\sum _{d=1}^{min\left(n,m\right)}d\sum _{{d}^{\prime }=1}^{⌊\frac{min\left(n,m\right)}{d}⌋}\mu \left({d}^{\prime }\right)\sum _{i=1}^{⌊\frac{n}{d}⌋}\sum _{j=1}^{⌊\frac{m}{d}⌋}\left[{d}^{\prime }|gcd\left(i,j\right)\right]\\ & =\sum _{d=1}^{min\left(n,m\right)}d\sum _{{d}^{\prime }=1}^{⌊\frac{min\left(n,m\right)}{d}⌋}\mu \left({d}^{\prime }\right)⌊\frac{n}{d{d}^{\prime }}⌋⌊\frac{m}{d{d}^{\prime }}⌋\\ 设T=d{d}^{\prime }\\ & =\sum _{T=1}^{min\left(n,m\right)}⌊\frac{n}{T}⌋⌊\frac{m}{T}⌋\sum _{{d}^{\prime }|T}\mu \left({d}^{\prime }\right)⌊\frac{T}{{d}^{\prime }}⌋\\ & =\sum _{T=1}^{min\left(n,m\right)}⌊\frac{n}{T}⌋⌊\frac{m}{T}⌋\phi \left(T\right)\end{array}$

$\mathcal{A}\mathcal{C}$$\mathcal{AC}$

##### 代码
#include<bits/stdc++.h>
using namespace std;
const int M=1e5+5;
int p[M/3],phi[M],n,m;
long long ans;
bool isp[M];
void pre()
{
phi[1]=isp[1]=1;int t;
for(int i=2;i<=n;++i)
{
if(!isp[i])p[++p[0]]=i,phi[i]=i-1;
for(int j=1;j<=p[0];++j)
{
t=i*p[j];if(t>n)break;
isp[t]=1;
if(i%p[j]==0){phi[t]=phi[i]*p[j];break;}
phi[t]=phi[i]*(p[j]-1);
}
phi[i]+=phi[i-1];
}
}
void in(){scanf("%d%d",&n,&m);}
void ac()
{
if(n>m)swap(n,m);pre();
for(int l=1,r;l<=n;l=r+1){r=min(n/(n/l),m/(m/l));ans+=1ll*(n/l)*(m/l)*(phi[r]-phi[l-1]);}
printf("%lld",2*ans-1ll*n*m);
}
int main(){in();ac();}

©️2019 CSDN 皮肤主题: 精致技术 设计师: CSDN官方博客