可惜没如果=_=
时光的河入海流
Farey Sequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15449   Accepted: 6149

Description

The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b) = 1 arranged in increasing order. The first few are 
F2 = {1/2} 
F3 = {1/3, 1/2, 2/3} 
F4 = {1/4, 1/3, 1/2, 2/3, 3/4} 
F5 = {1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5} 

You task is to calculate the number of terms in the Farey sequence Fn.

Input

There are several test cases. Each test case has only one line, which contains a positive integer n (2 <= n <= 106). There are no blank lines between cases. A line with a single 0 terminates the input.

Output

For each test case, you should output one line, which contains N(n) ---- the number of terms in the Farey sequence Fn. 

Sample Input

2
3
4
5
0

Sample Output

1
3
5
9

Source

POJ Contest,Author:Mathematica@ZSU
 
欧拉函数φ( )是指不超过n且与n互素的正整数的个数

φ函数的值 通式:φ(n)=n(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pk),其中p1, p2……pk为n的所有质因数

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <queue>
 6 #include <stack>
 7 #include <vector>
 8 #include <iostream>
 9 #include "algorithm"
10 using namespace std;
11 typedef long long LL;
12 const int MAX=1000005;
13 int n,m;
14 int phi[MAX];
15 LL sum[MAX];
16 void init(){
17     int i,j;
18     for (i=1;i<MAX;i++) phi[i]=i;
19     for (i=2;i<MAX;i+=2) phi[i]/=2;
20     for (i=3;i<MAX;i+=2) 
21      if (phi[i]==i){
22          for (j=i;j<MAX;j+=i)
23           phi[j]=phi[j]/i*(i-1);
24      }
25     sum[1]=0;
26     sum[2]=phi[2];
27     for (i=3;i<MAX;i++){
28         sum[i]=sum[i-1]+(LL)phi[i];
29     }
30 }
31 int main(){
32     freopen ("sequence.in","r",stdin);
33     freopen ("sequence.out","w",stdout);
34     int i,j;
35     init();
36     while (1){
37         scanf("%d",&j);
38         if (j==0)
39          break;
40         printf("%lld\n",sum[j]);
41     }
42     return 0;
43 }
如果你不会,那你最好看看

 

 
posted on 2016-11-14 20:41  珍珠鸟  阅读(211)  评论(0编辑  收藏  举报