Longge's problem(欧拉函数应用)

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



 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 #define ll long long int
 6 using namespace std;
 7 ll  eular(ll n)
 8 {
 9     ll i,ret=n;
10     for(i=2; i<=sqrt(n); i++)
11     {
12         if(n%i==0)
13         {
14             ret=ret/i*(i-1);
15             while(n%i==0)
16             {
17                 n/=i;
18             }
19         }
20     }
21     if(n>1)
22     {
23         ret=ret/n*(n-1);
24     }
25     return ret;
26 }
27 int main()
28 {
29     ll n,num,i,j;
30     ll ans;
31     while(scanf("%lld",&n)!=EOF)
32     {
33 
34         ans=eular(n)+n;
35         for(i=2;i<=sqrt(n);i++)
36         {
37             if(n%i==0)
38             {
39                 if(i*i==n)
40                 {
41                     ans=ans+eular(i)*i;
42                 }
43                 else
44                 {
45                     ans=ans+eular(i)*(n/i);
46                     ans=ans+eular(n/i)*i;
47                 }
48             }
49         }
50         printf("%lld\n",ans);
51     }
52     return 0;
53 }

猜你喜欢

转载自www.cnblogs.com/wkfvawl/p/9640525.html