# poj 2478 欧拉函数

169人阅读 评论(0)

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;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：34694次
• 积分：1759
• 等级：
• 排名：千里之外
• 原创：148篇
• 转载：0篇
• 译文：0篇
• 评论：2条
阅读排行
最新评论