poj 2478 Farey Sequence

题目链接:http://poj.org/problem?id=2478

题目大意:给定一个整数 n ( 2≤n≤10), 求 phi(2)+phi(3)+···+phi(n).

解题思路:简单题,主要是求 1-106 的欧拉函数 phi(i), 方法与素数筛法类似。

 1 ///////////////////////////////////////////////////////////////////////////
 2 //problem_id: poj 2478
 3 //user_id: SCNU20102200088
 4 ///////////////////////////////////////////////////////////////////////////
 5 
 6 #include <algorithm>
 7 #include <iostream>
 8 #include <iterator>
 9 #include <iomanip>
10 #include <cstring>
11 #include <cstdlib>
12 #include <string>
13 #include <vector>
14 #include <cstdio>
15 #include <cctype>
16 #include <cmath>
17 #include <queue>
18 #include <stack>
19 #include <list>
20 #include <set>
21 #include <map>
22 using namespace std;
23 
24 ///////////////////////////////////////////////////////////////////////////
25 typedef long long LL;
26 const double PI=acos(-1.0);
27 
28 const int x4[]={-1,0,1,0};
29 const int y4[]={0,1,0,-1};
30 const int x8[]={-1,-1,0,1,1,1,0,-1};
31 const int y8[]={0,1,1,1,0,-1,-1,-1};
32 
33 typedef int T;
34 T max(T a,T b){ return a>b? a:b; }
35 T min(T a,T b){ return a<b? a:b; }
36 ///////////////////////////////////////////////////////////////////////////
37 
38 ///////////////////////////////////////////////////////////////////////////
39 //Add Code:
40 const int maxn=1000000;
41 LL phi[maxn+5],ans[maxn+5];
42 ///////////////////////////////////////////////////////////////////////////
43 
44 int main(){
45     ///////////////////////////////////////////////////////////////////////
46     //Add code:
47     int n,i,j;
48     for(i=1;i<=maxn;i++) phi[i]=i;
49     for(i=2;i<=maxn;i++){
50         if(phi[i]==i){
51             for(j=i;j<=maxn;j+=i) phi[j]=phi[j]/i*(i-1);
52         }
53     }
54     ans[2]=phi[2];
55     for(i=3;i<=maxn;i++) ans[i]=ans[i-1]+phi[i];
56     while(scanf("%d",&n)!=EOF){
57         if(n==0) break;
58         printf("%lld
",ans[n]);
59     }
60     ///////////////////////////////////////////////////////////////////////
61     return 0;
62 }
63 
64 ///////////////////////////////////////////////////////////////////////////
65 /*
66 Testcase:
67 Input:
68 2
69 3
70 4
71 5
72 0
73 Output:
74 1
75 3
76 5
77 9
78 */
79 ///////////////////////////////////////////////////////////////////////////
原文地址:https://www.cnblogs.com/linqiuwei/p/3271778.html