其他分享
首页 > 其他分享> > 题解报告:poj 2480 Longge's problem(欧拉函数)

题解报告:poj 2480 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
解题思路:
AC代码:
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <map>
 5 #include <vector>
 6 #include <set>
 7 using namespace std;
 8 typedef long long LL;
 9 const int maxn = 1e6+5;
10 LL n, ans;
11 LL get_Euler(LL x){
12     LL res = x;
13     for(LL i = 2LL; i * i <= x; ++i) {
14         if(x % i == 0) {
15             res = res / i * (i - 1);
16             while(x % i == 0) x /= i;
17         }
18     }
19     if(x > 1LL) res = res / x * (x - 1);
20     return res;
21 }
22 
23 int main(){
24     while(cin >> n) {
25         ans = 0LL;
26         for (LL i = 1LL; i * i <= n; ++i) {
27             if(n % i == 0) {
28                 ans += i * get_Euler(n / i);
29                 if(i * i != n) ans += n / i * get_Euler(i); ///避免重复计数
30             }
31          }
32          cout << ans << endl;
33     }
34     return 0;
35 }

标签:Longge,题解,LL,2480,know,res,Input,include
来源: https://www.cnblogs.com/acgoto/p/10284800.html