# poj 2478 欧拉函数

Farey Sequence
 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13845 Accepted: 5475

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

#include<bitset>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

inline int in()
{
int res=0;char c;
while((c=getchar())<'0' || c>'9');
while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
return res;
}
const int N=1000100;
ll ans[N];
int cnt[N];
int gcd(int a,int b)
{
return b==0? a : gcd(b,a%b);
}
int main()
{
int n;
for(int i=2;i<N;i++) cnt[i]=i;
for(int i=2;i<N;i++)
{
if(cnt[i]==i)
{
for(int j=i;j<N;j+=i)
{
cnt[j]=cnt[j]/i*(i-1);
}
}
}
for(int i=2;i<N;i++)
{
ans[i]=ans[i-1]+cnt[i];
}
while(~scanf("%d",&n) && n)
{
printf("%I64d\n",ans[n]);
}
return 0;
}


#### POJ2478(典型的欧拉函数)

2012-03-07 16:04:37

#### poj2478 Farey Sequence（线性筛法+欧拉函数+递推）

2015-06-02 10:04:43

#### poj 2478 Farey Sequence 线性筛法优化的欧拉函数

2015-08-10 15:51:35

#### 欧拉函数的证明以及应用(附POJ例题)

2015-10-01 23:58:44

#### POJ - 2478 欧拉函数..

2012-02-26 14:55:43

#### poj3090 欧拉函数打表

2014-07-28 00:41:40

#### poj 2478 欧拉函数

2013-08-23 22:39:27

#### poj 2478 （欧拉函数）

2017-09-11 21:48:47

#### poj 2478 欧拉函数筛选

2016-08-13 10:06:55

#### POJ 2478(欧拉函数)

2018-04-28 21:56:02