1. 程式人生 > >Farey Sequence——(篩法求尤拉函式)

Farey Sequence——(篩法求尤拉函式)

傳送門
A - 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

題目大意:

就是求一個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;
}


相關推薦

Farey Sequence——函式

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

數論約數和 與 互質和演算法 分解質因數與函式

Description One day, Qz met an easy problem. But after a 5-hout-long contest in CSU, he became very tired and he wanted to call his girl

The Euler functionhdoj2824快速函式

The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4757    Acce

CodeForces 594D REQ樹狀陣列+函式

題意:給出一個序列,有m個詢問,每次詢問要求回答一個區間內所有數的乘積的尤拉函式。 思路:這道題看上去像是一個莫隊,但是莫隊的時間複雜度為O(n∗sqrt(n)∗k),其中k是序列中每個數的質因子的數量的最大值,這樣做會超時。 考慮樹狀陣列,因為我們要求的是

Super A^B mod C指數迴圈節+函式

Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000).

素數個數埃氏

求1——n的素數的個數,有以下三種方法: 普通的O()演算法: #include<iostream> #include<cstdio> #include<cmath> using namespace std; bool isprim

2478 Farey Sequence 函式,利用素數篩選

#include<iostream>#include<stdio.h>using namespace std;int a[1000005];long long sum[1000005];void phi_table(int n , int * phi)

poj 2478 Farey Sequence函式

Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13204 Accepted: 5181 Description The Farey Sequence Fn fo

JD 題目1040:Prime Number 素數

rime 簡單 set end std tdi href num mod OJ題目:click here~~ 題目分析:輸出第k個素數 貼這麽簡單的題目,目的不清純 用篩法求素數的基本思想是:把從1開始的、某一範圍內的正整數從小到大順序排列

HihoCoder1181歐Fleury算路徑

dia 一個數 esp 最大 打開 歐拉 正整數 str png 描述 在上一回中小Hi和小Ho控制著主角收集了分散在各個木橋上的道具,這些道具其實是一塊一塊骨牌。 主角繼續往前走,面前出現了一座石橋,石橋的盡頭有一道火焰墻,似乎無法通過。 小Hi註意到在

線性函式莫比烏斯函式

在這裡提供三種線性篩的講解,它們分別是:素數篩,尤拉篩和莫比烏斯篩。 ·篩法正確性的重要理論依據: 上述函式均為積性函式。積性函式的性質為:若f(x)是一個積性函式,那麼對於任意素數a,b,滿足f(ab)=f(a)*f(b) ·一些可愛的要點(有助於理解篩法原理

埃氏的區別

Eratosthenes篩法(Sieve of Eratosthenes) 由於思想非常簡單,故只給出實現。 void eratosthenes_sieve(int n) { totPrimes = 0; memset(flag, 0, size

N10^14以內與N互質的數的和(容斥原理,或者函式

#include <iostream> #include <cstring> #include <algorithm> #include <cmath>

小於等於n的素數的個數埃式篩選篩選

問題描述 給定數字n,求出小於等於n的素數的個數,假設n<=1000000 思路 找出數字n之前的所有素數,用陣列isprime[i]表示i是否是素數;用num[i]表示小於等於數字i的素數有多少。 那麼最重要的就是解決判斷一個數是否是素數 方法

51nod 1040 1-n這n個數,同n的最大公約數的和函式

題目:給出一個n,求1-n這n個數,同n的最大公約數的和。比如:n = 6           1,2,3,4,5,6 同6的最大公約數分別為1,2,3,2,1,6,加在一起 = 15 思路:一個數與n的最大公約數肯定是n的因子中的一個,所以只需要列舉n的每一個因子x,然

BZOJ4869 六省聯考2017相逢是問候線段樹+函式

  由擴充套件尤拉定理,a^(a^(a^(……^x)))%p中x作為指數的模數應該是φ(φ(φ(φ(……p)))),而p取log次φ就會變為1,也即每個位置一旦被修改一定次數後就會變為定值。線段樹維護區間剩餘修改次數的最大值,暴力修改即可。   可以預處理出每個位置進行k次操作後的值。直接計算是log^3的

C程式設計案例二分方程的根

原理 設函式f(x)在[a,b]上連續,且f(a)*f(b)<0,則表明f(x)在[a,b]上至少有一個零點。 微積分中的介值定理。然後通過二分割槽間,縮小區間範圍,當小到一定的精確度的時候,這個x就是我們所求的近似根了。 問題描述: 用二分法求下面方程在區間

C程式設計案例矩形定積分問題

矩形法求定積分問題 程式碼實現: #include<stdio.h> #include<math.h> float fsin(float x); float func(float (*p)(float),float a,float b

hdu1395 2^x mod n = 1函式

2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 20133  

ACM-ICPC 2018 焦作賽區網路預賽 G. Give Candies大數冪,降冪

樣例輸入 1 4 樣例輸出 8 題意:n個小朋友n個糖果,從第一個小朋友開始給糖果,至少給一顆糖果,直到糖果給完,問有幾種分糖果的方法 思路:很容易看出來答案是2^(n-1),問題是n很