题解报告:poj 2480 Longge's problem(欧拉函数)
Posted acgoto
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了题解报告: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.
"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.
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(欧拉函数)的主要内容,如果未能解决你的问题,请参考以下文章
POJ 2480 Longge's problem 积性函数
poj 2480 Longge's problem 积性函数性质+欧拉函数