wenbao与数论(大白书)

 

最大公约数之和

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=26&problem=2421&mosmsg=Submission+received+with+ID+18788108

输入n 输出gcd(i,j) 1<=i<j<=n总和

 

 1 #include <iostream>
 2 using namespace std;
 3 #define REP(i, x, n) for(int i = x; i < n; i++)
 4 #define ll long long
 5 const int maxn = 4000005;
 6 int phi[maxn], p[maxn];
 7 ll f[maxn], s[maxn];
 8 bool vis[maxn];
 9 void Euler(int n){
10     int i, j, k, cnt = 0;
11     phi[1] = 1;
12     for (i = 2; i < n; ++i){
13         if (!vis[i]){
14             p[cnt++] = i;
15             phi[i] = i - 1;
16         }
17         for (j = 0; j < cnt && i * p[j] < n; ++j){
18             vis[i * p[j]] = true;
19             if (i % p[j]) phi[i * p[j]] = phi[i] * phi[p[j]];
20             else {
21                 phi[i * p[j]] = phi[i] * p[j];
22                 break;
23             }
24         }
25     }
26 }
27 int main(){
28     Euler(maxn);
29     REP(i, 1, maxn){
30         for(int j = i*2; j < maxn; j += i){
31             f[j] += i * phi[j/i]; 
32         }
33     }
34     s[2] = f[2];
35     REP(i, 3, maxn) s[i] = s[i-1] + f[i];
36     int n;
37     while(scanf("%d", &n) && n){
38         printf("%lld\n", s[n]);
39     }
40     return 0;
41 }

 

 

 

 

 

 

只有不断学习才能进步!

 

posted @ 2018-04-14 13:54  wenbao  阅读(204)  评论(0编辑  收藏  举报