poj2478 欧拉函数水题

poj2478 欧拉函数水题

Y - Farey Sequence

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

Submit Status Practice POJ 2478

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

时间: 2024-12-20 03:14:54

poj2478 欧拉函数水题的相关文章

hdu 1787(欧拉函数+水题)

题意:给出一个n,求小于n大于0的所有与n不互质的数的个数 是一道欧拉函数的模板题 1 #include<iostream> 2 #include<string.h> 3 #include<string> 4 #include<sstream> 5 #include<vector> 6 #include<deque> 7 #include<map> 8 #include<algorithm> 9 #includ

hdu-1286 找新朋友(欧拉函数,水题)

题目链接: 找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10120    Accepted Submission(s): 5344 Problem Description 新年快到了,“猪头帮协会”准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是和会长是老朋友的,那么该会员的号码肯定和N有

hdu2824 The Euler function 筛选法求欧拉函数模板题

//求a , b范围内的所有的欧拉函数 //筛选法求欧拉函数模板题 #include<cstdio> #include<cstring> #include<iostream> using namespace std ; const int maxn = 3000010 ; typedef __int64 ll ; int e[maxn] ; int a ,  b ; void Euler() { int i,j; for (i=1;i<maxn;i++) e[i]

(hdu step 7.2.1)The Euler function(欧拉函数模板题——求phi[a]到phi[b]的和)

题目: The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 166 Accepted Submission(s): 96   Problem Description The Euler function phi is an important kind of function in number theory

POJ 2407 Relatives(欧拉函数入门题)

Relatives Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz. Input There are several t

Goldbach&#39;s Conjecture POJ - 2262 线性欧拉筛水题 哥德巴赫猜想

题意 哥德巴赫猜想:任一大于2的数都可以分为两个质数之和 给一个n 分成两个质数之和 线行筛打表即可 可以拿一个数组当桶标记一下a[i]  i这个数是不是素数  在线性筛后面加个装桶循环即可 #include<cstdio> #include<cstring> using namespace std; bool Is_Primes[1000005]; int Primes[1000005]; int cnt; void Prime(int n){ cnt=0; memset(Is_

poj2407(欧拉函数模板题)

题目链接:https://vjudge.net/problem/POJ-2407 题意:给出n,求0..n-1中与n互质的数的个数. 思路:欧拉函数板子题,先根据唯一分解定理求出n的所有质因数p1,p2,...,pn,然后根据Φ(m)=m*∏(1-1/pi)计算即可. AC代码: #include<cstdio> using namespace std; int n,ans; int main(){ while(scanf("%d",&n),n){ ans=n; f

【BZOJ4173】数学 欧拉函数神题

[BZOJ4173]数学 Description Input 输入文件的第一行输入两个正整数 . Output 如题 Sample Input 5 6 Sample Output 240 HINT N,M<=10^15 题解:STEP 1: 这步还是很容易的吧~毕竟原来的式子不太舒服.但是注意,最后一个式子的取值只能为0或1,所以就变成了. STEP 2: 这步倒是难理解一些,但是考虑:我们将这三个等式都算出来,如果满足了左边那个条件,那么这三个等式加起来为1,对答案的贡献正好为$\varphi

poj2478 (欧拉函数)

Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17894   Accepted: 7179 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)