【SDOI2012】【BZOJ2705】【Longge的问题】【题解】【数论】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2705

扔下代码就跑

Code:

/*
	ID:zky
	OJ:BZOJ
	Index:2705
	Language:C++ 
*/
#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long lld;
lld n;
long long ans=0;
lld phi(lld n){
  lld ans=n,i;
  for(i=2;i*i<=n;i++)
    if(!(n%i)){
       ans=ans/i*(i-1);
       while(!(n%i))n/=i;
    }
  if(n>1)ans=ans/n*(n-1);
  return ans;
}
int main(){
	cin>>n;
	for(lld i=1;i<=sqrt(n);i++){
		if(n%i==0){
			ans+=phi(n/i)*i;
			if(i*i!=n)ans+=phi(i)*n/i;			
		}
	} 
	cout<<ans<<endl;
	return 0;
} 


你可能感兴趣的:(数论,bzoj)