P1447 [NOI2010]能量采集

题意:
\(\sum_{i=1}^{n} \sum_{j=1}^{m} 2*gcd(i,j)+1\)

=> \(\sum_{i=1}^{n} \sum_{j=1}^{m} 2*\sum_{d\mid gcd(i,j)}\phi(d)\)

=> \(2*\sum_{d}^{min(n,m)}\phi(d) \lfloor {n\over d}\rfloor \lfloor {m\over d}\rfloor\) - n*m;
然后筛欧拉函数即可;

#include<iostream>
#include<cstdio>
using namespace std;
const int N=1e5+7;
int n,m,cnt;
long long ans;
long long prime[N],is_prime[N],phi[N];
void get_phi(){
	phi[1]=1;
	for(int i=2;i<=n;i++){
		if(!is_prime[i]){
			prime[++cnt]=i;
			phi[i]=i-1;
		}
		for(int j=1;j<=cnt&&i*prime[j]<=n;j++){
//			cout<<"kkk"<<"\n";
			is_prime[i*prime[j]]=1;
			if(i%prime[j]==0){
//				cout<<i*prime[j]<<"\n";
				phi[i*prime[j]]=phi[i]*prime[j];
				break;
			}else{
				phi[i*prime[j]]=phi[i]*(prime[j]-1);
			}
		}
	}
}
int main(){
	scanf("%d%d",&n,&m);
	if(n<m)swap(n,m);
	get_phi();
//	return 0;
	for(int i=1;i<=m;i++){
		ans+=1LL*phi[i]*1LL*(n/i)*(m/i);
	}
	ans=2*ans-1LL*n*m;
	cout<<ans;
}
posted @ 2020-09-14 21:19  Aswert  阅读(107)  评论(0编辑  收藏  举报