POJ 2480 Longge's problem 欧拉函数—————∑gcd(i, N) 1<=i <=N

时间:2021-12-09 09:31:44
Longge's problem
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6383   Accepted: 2043

Description

Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms. Now a problem comes: Given an integer N(1 < N < 2^31),you are to calculate ∑gcd(i, N) 1<=i <=N.

"Oh, I know, I know!" Longge shouts! But do you know? Please solve it.

Input

Input contain several test case. 
A number N per line. 

Output

For each N, output ,∑gcd(i, N) 1<=i <=N, a line

Sample Input

2
6

Sample Output

3
15

Source

POJ Contest,Author:Mathematica@ZSU
 /*

 题意:∑gcd(i, N) 1<=i <=N。由于N 2^31.
刚开始想用欧拉求得1的个数,再求N的素因子,用容斥
来求解。发现,就算是求得的素因子,也不最大公约数。 HUD的一道题,提供了思路。 枚举吧。
if(N%i==0)
{
最大公约数为1的时候,有几个。欧拉值(N/1);
最大公约数为2的时候,有几个。欧拉值(N/2);
.....
} */ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std; __int64 Euler(__int64 n)
{
__int64 i,temp=n;
for(i=;i*i<=n;i++)
if(n%i==)
{
while(n%i==)
n=n/i;
temp=temp/i*(i-);
}
if(n!=)
temp=temp/n*(n-);
return temp;
} int main()
{
__int64 n,i,sum,k;
while(scanf("%I64d",&n)>)
{
sum=;
for(i=;i*i<=n;i++)
{
if(n%i==)
sum=sum+Euler(n/i)*i;
k=n/i;
if(n%k== && k!=i)
sum=sum+Euler(n/k)*k;
}
printf("%I64d\n",sum);
}
return ;
}