liyunlonghere

你必须非常努力,才能看起来毫不费力。

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


题意:求1-n中与n互质的数的个数。

分析:欧拉函数就是这样定义的。


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


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liyunlong41/article/details/49932593
文章标签: 数论
个人分类: 数论相关
上一篇lightoj 1236 pairs of lcm
下一篇poj 3660 传递性关系 floyd更新
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭