The Euler function http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2824
Screening Method
1 # include <cstdio> 2 # include <cstring> 3 # define MT (a, B) memset (a, B, sizeof (a) 4 typedef _ int64 ll; 5 const int M = 3000010; 6 int Phi [m]; // number of elements less than I and I 7 int pri [m], pricnt; 8 void sieve_phi () {// find the Euler's function Phi 9 pricnt = 0; 10 mT (PHI, 0); 11 Phi [1] = 1; 12 for (INT I = 2; I <m; I ++) {13 if (! Phi [I]) {14 pri [pricnt ++] = I; 15 Phi [I] = I-1; 16} 17 for (Int J = 0; PRI [J] * I <m; j ++) {18 if (I % pri [J]) {19 Phi [I * Pri [J] = Phi [I] * (PRI [J]-1 ); 20} 21 else {22 Phi [I * Pri [J] = Phi [I] * Pri [J]; 23 break; 24} 25} 26} 27} 28 int main () {29 sieve_phi (); 30 int X, Y; 31 While (~ Scanf ("% d", & X, & Y) {32 ll ans = 0; 33 for (INT I = x; I <= y; I ++) {34 ans + = Phi [I]; 35} 36 printf ("% i64d \ n", ANS); 37} 38 return 0; 39}
View code
Farey sequence http://poj.org/problem? Id = 2478
1 # include <cstdio> 2 # include <cstring> 3 # define MT (a, B) memset (a, B, sizeof (a) 4 typedef _ int64 ll; 5 const int M = 1000010; 6 int Phi [m]; // number of elements less than I and I 7 int pri [m], pricnt; 8 void sieve_phi () {// find the Euler's function Phi 9 pricnt = 0; 10 mT (PHI, 0); 11 Phi [1] = 1; 12 for (INT I = 2; I <m; I ++) {13 if (! Phi [I]) {14 pri [pricnt ++] = I; 15 Phi [I] = I-1; 16} 17 for (Int J = 0; PRI [J] * I <m; j ++) {18 if (I % pri [J]) {19 Phi [I * Pri [J] = Phi [I] * (PRI [J]-1 ); 20} 21 else {22 Phi [I * Pri [J] = Phi [I] * Pri [J]; 23 break; 24} 25} 26} 27} 28 ll sum [m]; 29 int main () {30 sieve_phi (); 31 sum [1] = 0; 32 For (INT I = 2; I <m; I ++) {33 sum [I] = sum [I-1] + Phi [I]; 34} 35 int N; 36 while (~ Scanf ("% d", & N), n) {37 printf ("% i64d \ n", sum [N]); 38} 39 return 0; 40}
View code
End