HDU 2824 The Euler function【模板题】

求区间欧拉函数和

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 const int maxn = 3000005;
 7 long long e[maxn + 10];
 8 
 9 void init() {
10     e[1] = 1;
11     for(int i = 2; i < maxn; i++) e[i] = i;
12     for(int i = 2; i < maxn; i++) 
13         if(e[i] == i) 
14             for(int j = i; j < maxn; j += i) 
15                 e[j] = e[j] / i * ( i - 1);
16     for(int i = 2; i < maxn; i++) {
17         e[i] += e[i - 1];
18     }
19 }
20 
21 int main() {
22     init();
23     int n, m;
24     while(EOF != scanf("%d %d",&n, &m)) {
25         printf("%I64d
", e[m] - e[n - 1]);
26     }
27     return 0;
28 }
View Code
原文地址:https://www.cnblogs.com/zhanzhao/p/4364021.html