BZOJ2005 NOI2010 能量采集

【样例输入1】
5 4
【样例输入2】
3 4

【样例输出1】
36
【样例输出2】
20

HINT

$ans=n\ast m+2\ast \sum _{i=1}^{n}\sum _{j=1}^{m}\left(gcd\left(i,j\right)-1\right)$$ans=n*m+2*\sum_{i=1}^{n}\sum_{j=1}^{m}(gcd(i,j)-1)$

$ans=-n\ast m+2\ast \sum _{i=1}^{n}\sum _{j=1}^{m}gcd\left(i,j\right)$$ans=-n*m+2*\sum_{i=1}^{n}\sum_{j=1}^{m}gcd(i,j)$

$ans=-n\ast m+2\ast \sum _{d=1}^{min\left(n,m\right)}\sum _{i=1}^{n}\sum _{j=1}^{m}\left[gcd\left(i,j\right)==d\right]\ast d$$ans=-n*m+2*\sum_{d=1}^{min(n,m)}\sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j)==d]*d$

$f\left(d\right)=\sum _{i=1}^{⌊\frac{n}{d}⌋}\sum _{j=1}^{⌊\frac{m}{d}⌋}\left[gcd\left(i,j\right)==1\right]$$f(d)=\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}[gcd(i,j)==1]$

$f\left(d\right)=\sum _{i=1}^{⌊\frac{n}{d}⌋}\sum _{j=1}^{⌊\frac{m}{d}⌋}\sum _{p=1}^{min\left(⌊\frac{n}{d}⌋,⌊\frac{m}{d}⌋\right)}\mu \left(p\right)$$f(d)=\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}\sum_{p=1}^{min(\left\lfloor\frac{n}{d}\right\rfloor,\left\lfloor\frac{m}{d}\right\rfloor)}\mu(p)$

$f\left(d\right)=\sum _{p=1}^{min\left(⌊\frac{n}{d}⌋,⌊\frac{m}{d}⌋\right)}\mu \left(p\right)\sum _{p|i}^{⌊\frac{n}{d}⌋}\sum _{p|j}^{⌊\frac{m}{d}⌋}1$$f(d)=\sum_{p=1}^{min(\left\lfloor\frac{n}{d}\right\rfloor,\left\lfloor\frac{m}{d}\right\rfloor)}\mu(p)\sum_{p|i}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{p|j}^{\left\lfloor\frac{m}{d}\right\rfloor}1$

$f\left(d\right)=\sum _{p-1}^{min\left(⌊\frac{n}{d}⌋,⌊\frac{m}{d}⌋\right)}\mu \left(p\right)⌊\frac{n}{d\ast p}⌋⌊\frac{m}{d\ast p}⌋$$f(d)=\sum_{p-1}^{min(\left\lfloor\frac{n}{d}\right\rfloor,\left\lfloor\frac{m}{d}\right\rfloor)}\mu(p)\left\lfloor\frac{n}{d*p}\right\rfloor\left\lfloor\frac{m}{d*p}\right\rfloor$

$ans=-n\ast m+2\ast \sum _{d=1}^{min\left(n,m\right)}d\ast \sum _{p=1}^{min\left(⌊\frac{n}{d}⌋,⌊\frac{m}{d}⌋\right)}\mu \left(p\right)⌊\frac{n}{d\ast p}⌋⌊\frac{m}{d\ast p}⌋$$ans=-n*m+2*\sum_{d=1}^{min(n,m)}d*\sum_{p=1}^{min(\left\lfloor\frac{n}{d}\right\rfloor,\left\lfloor\frac{m}{d}\right\rfloor)}\mu(p)\left\lfloor\frac{n}{d*p}\right\rfloor\left\lfloor\frac{m}{d*p}\right\rfloor$

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 100010
LL n,m,tot=0,ans=0;
LL pri[N],mu[N],F[N];
bool mark[N]={0};
void init(){
mu[1]=1;
for(int i=2;i<N;i++){
if(!mark[i])pri[++tot]=i,mu[i]=-1;
for(int j=1;j<=tot&&pri[j]*i<N;j++){
mark[pri[j]*i]=1;
if(i%pri[j]==0){
mu[i*pri[j]]=0;
break;
}else mu[i*pri[j]]=-mu[i];
}
}
for(int i=1;i<N;i++)F[i]=F[i-1]+mu[i];
}
LL solve(int d){
LL res=0,n1=n/d,m1=m/d,up=min(n1,m1);
for(int i=1,j;i<=up;i=j+1){
j=min(n1/(n1/i),m1/(m1/i));
res+=(F[j]-F[i-1])*(n1/i)*(m1/i);
}
return res*d;
}
int main(){
init();
scanf("%lld%lld",&n,&m);
int up=min(n,m);
for(int i=1;i<=up;i++)ans+=solve(i);
ans*=2;
ans-=n*m;
printf("%lld",ans);
return 0;
}

$ans=-n\ast m+2\ast \sum _{d=1}^{min\left(n,m\right)}d\sum _{d|k}^{min\left(n,m\right)}\mu \left(k/d\right)⌊\frac{n}{k}⌋⌊\frac{m}{k}⌋$$ans=-n*m+2*\sum_{d=1}^{min(n,m)}d\sum_{d|k}^{min(n,m)}\mu(k/d)\left\lfloor\frac{n}{k}\right\rfloor\left\lfloor\frac{m}{k}\right\rfloor$

$ans=-n\ast m+2\ast \sum _{k=1}^{min\left(n,m\right)}\sum _{d|k}^{min\left(n,m\right)}d\mu \left(k/d\right)⌊\frac{n}{k}⌋⌊\frac{m}{k}⌋$$ans=-n*m+2*\sum_{k=1}^{min(n,m)}\sum_{d|k}^{min(n,m)}d\mu(k/d)\left\lfloor\frac{n}{k}\right\rfloor\left\lfloor\frac{m}{k}\right\rfloor$

$ans=-n\ast m+2\ast \sum _{k=1}^{min\left(n,m\right)}\sum _{d|k}^{min\left(n,m\right)}\frac{k}{d}\mu \left(d\right)⌊\frac{n}{k}⌋⌊\frac{m}{k}⌋$$ans=-n*m+2*\sum_{k=1}^{min(n,m)}\sum_{d|k}^{min(n,m)}\frac{k}{d}\mu(d)\left\lfloor\frac{n}{k}\right\rfloor\left\lfloor\frac{m}{k}\right\rfloor$

$ans=-n\ast m+2\ast \sum _{k=1}^{min\left(n,m\right)}⌊\frac{n}{k}⌋⌊\frac{m}{k}⌋\sum _{d|k}^{min\left(n,m\right)}\frac{k}{d}\mu \left(d\right)$$ans=-n*m+2*\sum_{k=1}^{min(n,m)}\left\lfloor\frac{n}{k}\right\rfloor\left\lfloor\frac{m}{k}\right\rfloor\sum_{d|k}^{min(n,m)}\frac{k}{d}\mu(d)$

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 100010
LL n,m,tot=0,ans=0;
LL pri[N],mu[N],F[N];
bool mark[N]={0};
void init(){
mu[1]=1;
for(int i=2;i<N;i++){
if(!mark[i])pri[++tot]=i,mu[i]=-1;
for(int j=1;j<=tot&&pri[j]*i<N;j++){
mark[pri[j]*i]=1;
if(i%pri[j]==0){
mu[i*pri[j]]=0;
break;
}else mu[i*pri[j]]=-mu[i];
}
}
for(int i=1;i<N;i++)
for(int j=1;j*i<N;j++)
F[i*j]+=mu[i]*j;
for(int i=1;i<N;i++)F[i]+=F[i-1];
}
int main(){
init();
scanf("%lld%lld",&n,&m);
int up=min(n,m);
for(int i=1,j;i<=up;i=j+1){
j=min(n/(n/i),m/(m/i));
ans+=(F[j]-F[i-1])*(n/i)*(m/i);
}
ans*=2;
ans-=n*m;
printf("%lld",ans);
return 0;
}

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