[SDOI2012]Longge的問題
Description
Longge的數學成績非常好,並且他非常樂於挑戰高難度的數學問題。現在問題來了:給定一個整數N,你需要求出∑gcd(i, N)(1<=i <=N)。
Input
一個整數,爲N。
Output
一個整數,爲所求的答案。
Sample Input
6
Sample Output
15
HINT
【數據範圍】
對於60%的數據,0 < N<=2^16。
對於100%的數據,0 < N<=2^32。
Solution
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<typename T>inline void read(T &x){
x=0;T f=1;char ch=getchar();
while(!isdigit(ch)) {if (ch=='-')f=-1; ch=getchar();}
while(isdigit(ch)) {x=x*10+ch-'0'; ch=getchar();}
x*=f;
}
const int MaxN=100000;
ll n,tot,d[MaxN],ans=0;
inline ll phi(ll n){
ll res=n;
for(ll i=2;i*i<=n;i++){
if(n%i==0){res/=i;res*=(i-1);}
while(n%i==0) n/=i;
}
if (n!=1) res/=n,res*=(n-1);
return res;
}
int main(){
read(n);
for(ll i=1;i*i<=n;i++){
if(n%i==0) d[++tot]=i,d[++tot]=n/i;
if(i*i==n) tot--;
}
for(int i=1;i<=tot;i++) ans+=d[i]*phi(n/d[i]);
printf("%lld\n",ans);
return 0;
}