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

Posted wkfvawl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
",ans);
51     }
52     return 0;
53 }

 

以上是关于Longge's problem(欧拉函数应用)的主要内容,如果未能解决你的问题,请参考以下文章

[poj 2480] Longge's problem 解题报告 (欧拉函数)

poj 2480 Longge&#39;s problem 积性函数性质+欧拉函数

POJ 2480 Longge&#39;s problem 积性函数

BZOJ 2705 [SDOI2012]Longge的问题(欧拉函数)

Longge's problem ( gcd的积性)

poj2480 Longge's problem