码迷,mamicode.com
首页 > 其他好文 > 详细

poj2478 欧拉函数水题

时间:2015-06-12 00:54:54      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

poj2478 欧拉函数水题

Y - Farey Sequence
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 <= 10 6). 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
题意:如题,给定n,求1~n的范围内互质数对的数目。
思路:欧拉函数水题。显然,小于n且与n互质的数即为欧拉函数,累积求和即为题中所要的答案。
技术分享
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<math.h>
#include<cctype>
#define ll long long
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define REPP(i,a,b,t) for(int (i)=(a);(i)<=(b);(i)+=(t))
#define PII pair<int,int>
#define MP make_pair
#define PB push_back
#define RI(x) scanf("%d",&(x))
#define RLL(x) scanf("%lld",&(x))
#define RI64(x) scanf("%I64d",&(x))
#define DRI(x) int x;scanf("%d",&(x))
#define DRLL(x) ll x;scanf("%lld",&(x))
#define DRI64(x) llx;scanf("%I64d",&(x))
#define MS0(a) memset((a),0,sizeof((a)))
#define MS1(a) memset((a),0,sizeof((a)))
#define MS(a,b) memset((a),(b),sizeof((a)))

using namespace std;

const int maxn=4000100;
const int INF=(1<<29);
const double EPS=0.0000000001;
const double Pi=acos(-1.0);

int euler[maxn];
ll f[maxn];
ll n;

void getEuler()
{
    MS0(euler);
    euler[1]=0;
    REP(i,2,maxn-1){
        if(!euler[i]){
            REPP(j,i,maxn-1,i){
                if(!euler[j]) euler[j]=j;
                euler[j]=euler[j]/i*(i-1);
            }
        }
    }
}

int main()
{
    getEuler();
    MS0(f);
    REP(i,1,maxn-1) f[i]=f[i-1]+euler[i];
    while(cin>>n,n>0){
        cout<<f[n]<<endl;
    }
    return 0;
}
View Code

 

poj2478 欧拉函数水题

标签:

原文地址:http://www.cnblogs.com/--560/p/4570588.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!