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。
#include<cstdio>
#include<cmath>
using namespace std;
#define LL long long
LL n,m,ans;
inline LL phi(LL x)
{
LL y=x;
for(LL i=2;i<=m;i++)
if(x%i==0)
{
y=y/i*(i-1);
while (x%i==0) x/=i;
}
if (x>1) y=y/x*(x-1);
return y;
} //查找互质因子个数
int main()
{
scanf("%lld",&n);
m=sqrt(n);
for (LL i=1;i<=m;i++)
if(n%i==0)
{
ans+=(LL)i*phi(n/i); //互质因子个数乘以最大公约数
if (i*i<n) ans+=(LL)(n/i)*phi(i);
}
printf("%lld",ans);
}
注解:互质数同乘以一个不为零的数m,则它们的最大公约数为m