题目:

http://bjutacm.openjudge.cn/lianxi/193E/

思路:

n的所有质因数之和等于phi(n) * n / 2, phi(n)为欧拉函数。

实现:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int euler(int n)
 5 {
 6     int res = n;
 7     for (int i = 2; i * i <= n; i++)
 8     {
 9         if (n % i == 0)
10         {
11             res = res / i * (i - 1);
12             while (n % i == 0) n /= i;
13         }
14     }
15     if (n > 1) res = res / n * (n - 1);
16     return res;
17 }  
18 
19 int main()
20 {
21     int n;
22     while (cin >> n)
23     {
24         cout << (n == 1 ? 1 : (long long)euler(n) * n / 2) << endl;
25     }
26     return 0;
27 }

 

相关文章:

  • 2021-06-18
  • 2021-07-22
  • 2021-05-27
  • 2021-08-14
  • 2022-01-09
  • 2021-10-23
  • 2022-02-18
猜你喜欢
  • 2021-09-28
  • 2022-12-23
  • 2021-06-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-15
  • 2021-06-18
相关资源
相似解决方案