poj2480 数论

原题:poj.org/problem?id=2480

题目:求\sum gcd(i,N) 1<=i<=N。可以枚举gcd即:gcd(x,N)=g

变形一下:

gcd(\frac{x}{g},\frac{N}{g})=1,这样就转化为:\varphi(\frac{N}{g})个gcd为g的数

答案为:

\sum_{g|x} \varphi(\frac{N}{g})*g

#include<cstdio>
#include<cstring>
#include<algorithm>
#ifdef WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
#define ll long long
using namespace std;
ll n;
inline int phi(int x){
	int ans=x;
	for(int i=2;1ll*i*i<=1ll*x;i++){
		if(x%i==0) {
			ans=ans/i*(i-1);
			while(x%i==0) x/=i;
		}
	}
	if(x>1) ans=ans/x*(x-1);
	return ans;
}
int main(){
//	 freopen("poj2480.in","r",stdin);
	 while(scanf(LLD,&n)!=EOF){
	 	ll ans=0;
	 	for(int i=1;1ll*i*i<=1ll*n;i++){
			if(n%i==0){
				int x=phi(n/i);ans+=1ll*x*i;
				if(i*i!=n) {
					int y=phi(i);ans+=1ll*y*(n/i);
				} 	
			}
		 }
		printf(LLD"\n",ans);
	}
	return 0;
}

 

 

posted @ 2018-12-15 20:33  Exception2017  阅读(235)  评论(0编辑  收藏  举报