码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-01-17 22:34:16      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:like   long   ever   ber   pre   iostream   sam   lan   ica   

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 }

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

标签:like   long   ever   ber   pre   iostream   sam   lan   ica   

原文地址:https://www.cnblogs.com/acgoto/p/10284800.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!