Description
Longge的數學成績非常好,並且他非常樂於挑戰高難度的數學問題。現在問題來了:給定一個整數N,你需要求出∑gcd(i, N)(1<=i
<=N)。
Input
一個整數,爲N。
Output
一個整數,爲所求的答案。
Sample Input
6
Sample Output
15
HINT
【數據範圍】
對於60%的數據,0<N<=216 。
對於100%的數據,0<N<=232 。
Source
round1 day1
枚舉
#include<cstdio>
#include<iostream>
#include<math.h>
using namespace std;
typedef long long ll;
ll n,ans;
void read(ll &x)
{
char t=getchar();int f=1;x=0;
while((t<48)or(t>57)){if(t=='-')f=-1;t=getchar();}
while((t>=48)and(t<=57)){x=(x*10)+t-48;t=getchar();}
x*=f;
}
ll phi(ll x)
{
ll ans=1;
for (int i=2;i*i<=x;++i)
if (x%i==0)
{
ans*=(i-1);
x/=i;
while (x%i==0)
{
ans*=i;
x/=i;
}
}
if (x>1) ans*=(x-1);
return ans;
}
int main()
{
read(n);
for (int i=1;i*i<=n;++i)
if (n%i==0)
{
ans+=i*phi(n/i);
if (i*i!=n) ans+=(n/i)*phi(i);//對應的另一約數
}
cout<<ans;
return 0;
}