A - Farey Sequence——(筛法求欧拉函数)

传送门

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

Submit Status

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

题目大意:

就是求一个F(n)的中元素的个数。

解题思路:

首先我们分析一下题目,我们一看就是关于欧拉函数的题,就是求<=b中与b互素的个数,但是我们别忘记还得加上phi(<n)的值,所以这个问题就是求欧拉函数的和的问题,然后我们如果用正常的做法也就是暴力做的话 会超时的(我试过了/笑cry),所以我们要找到更快速的方法
——筛法,其实这个筛法就是跟素数筛差不多的,也算是又学了一个知识点

void Init()
{
    memset(phi, 0, sizeof(phi)) ;
    for(int i=2; i<MAXN; i++)
    {
        if(!phi[i])
        {
            for(int j=i; j<MAXN;j+=i )
            {
                if(!phi[j])
                    phi[j] = j ;
                phi[j] = phi[j]/i*(i-1) ;
            }
        }
    }
}

Code:

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int MAXN = 1e6+5;

int phi[MAXN];
void Init()
{
    memset(phi, 0, sizeof(phi)) ;
    for(int i=2; i<MAXN; i++)
    {
        if(!phi[i])
        {
            for(int j=i; j<MAXN;j+=i )
            {
                if(!phi[j])
                    phi[j] = j ;
                phi[j] = phi[j]/i*(i-1) ;
            }
        }
    }
}
int main()
{
    Init();
    int m;
    while(cin>>m,m)
    {
        LL sum = 0;
        for(int i=2; i<=m; i++)
            sum += phi[i];
        cout<<sum<<endl;
    }
    return 0;
}
时间: 2024-09-07 11:45:25

A - Farey Sequence——(筛法求欧拉函数)的相关文章

如何用线性筛法求欧拉函数

前几天做了一个关于欧拉函数的题,当时就做超时了,因为我是暴力做的,后来百度了一下 线性晒法求欧拉函数,所以今天就打算系统的看一下筛法求欧拉函数的问题,该算法在可在线性时间内筛素数的同时求出所有数的欧拉函数: 先介绍一下暴力的欧拉函数: Eular(m) = m - (1-1/p1) - (1-1/p2) - ... - (1-1/pk)  [其中 p1, p2...pk为m的素因子] int Eular(int m) { int ret = m; for(int i=2; i<m; i++) {

欧拉函数模板

欧拉函数:表示1-(n-1)中,与n互质的数的个数 本以为学会容斥原理就不必再看欧拉函数,可是偏偏就是有些题用容斥原理解不了,必须参考欧拉,没办法只好回头看欧拉函数 下面贴一个筛法求欧拉函数模板: //初始化eu[1]=0或者eu[1]=1,具体情况根据题目变化! //下面计算2-10000的欧拉函数 const int MAX = 10001; int eu[MAX];//不要忘记初始化eu[1]. void eular(){ for(int i=2;i<MAx;i++){ if(!eu[i]

HDU 3501 Calculation 2:欧拉函数的应用

HDU 3501 Calculation 2:http://acm.hdu.edu.cn/showproblem.php?pid=3501 大意:求1~n之间与n不互质的数的总和. 思路:欧拉函数的应用:先用欧拉函数求出与n互质的总数m,计算m个数的总和,用n的总和减去m的总和就是想要的结果. 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/ #include <stdio.h> #define LL _

UVa 11417 GCD (欧拉φ函数)

11417 - GCD Time limit: 2.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2412 Given the value of N, you will have to find the value of G. The definition of G is given

hdu 1695 GCD【欧拉函数+容斥原理】

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6253    Accepted Submission(s): 2291 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y

HDU 1286 找新朋友(欧拉函数模板)

HDU 1286 找新朋友:http://acm.hdu.edu.cn/showproblem.php?pid=1286 题意:中文题. 思路:欧拉函数的纯模板题,没什么好说的,主要是理解欧拉函数的意义. 在数论,对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目.此函数以其首名研究者欧拉命名,它又称为Euler's totient function.φ函数.欧拉商数等. 例如φ(8)=4,因为1,3,5,7均和8互质.   ----by度娘. 更多精彩内容:http://www.bia

欧拉函数性质

先来介绍几个与欧拉函数有关的定理: 定理一:设m与n是互素的正整数,那么 定理二:当n为奇数时,有. 因为2n是偶数,偶数与偶数一定不互素,所以只考虑2n与小于它的奇数互素的情况,则恰好就等于n的欧拉函数值. 定理三:设p是素数,a是一个正整数,那么 关于这个定理的证明用到容斥: 由于表示小于与互素数的正整数个数,所以用减去与它不互素的数的个数就行了. 那么小于与不互素数的个数就是p的倍数个数,有个.所以定理得证. 定理四:设为正整数n的素数幂分解,那么 这个定理可以根据定理一和定理三证明,其实

数论之 素因子分解,素数筛选法,欧拉函数和扩展欧几里得算法 (整理)

今天突然想复习一下数论,也就顺便整理了一下关于数论的基础知识, 以后用的时候直接用就行了,也不用现敲了,其实就是有点懒.... 具体解释都在代码里有解释 直接上代码了: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue>

欧拉函数

欧拉函数: 是少于或等于n的数中与n互质的数的数目 欧拉函数的值 通式:Euler(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)-..(1-1/pn),其中p1, p2--pn为x的所有质因数,x是不为0的整数.Euler(1)=1(唯一和1互质的数(小于等于1)就是1本身). (注意:每种质因数只一个.比如12=2*2*3那么Euler(12)=12*(1-1/2)*(1-1/3)=4: Eg : Euler(12) = 4,与12互质的数有1 5 7 11,所以E