1. 程式人生 > >poj 2478 Farey Sequence(尤拉函式)

poj 2478 Farey Sequence(尤拉函式)

Farey Sequence
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13204 Accepted: 5181

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

Source

POJ Contest,Author:[email protected]

簡單的尤拉函式模板題。

所謂尤拉函式:對於一個正整數n,小於n且和n 互質的正整數(包括1 )的個數,記做φ(n) 。

通式:φ(x)=x*(1-1/p1)*(1-1/p2)*(1-1/p3)*(1-1/p4)…..(1-1/pn),其中p1, p2……pn為x的所有質因數,x是不為0的整數。φ(1)=1(唯一和1互質的數就是1本身)。

尤拉函式程式碼實現:   

//直接求解尤拉函式
int euler(int n){ //返回euler(n) 
     int res=n,a=n;
     for(int i=2;i*i<=a;i++){
         if(a%i==0){
             res=res/i*(i-1);//先進行除法是為了防止中間資料的溢位 
             while(a%i==0) a/=i;
         }
     }
     if(a>1) res=res/a*(a-1);
     return res;
}

//篩選法打尤拉函式表 
#define Max 1000001
int euler[Max];
void Init(){ 
     euler[1]=1;
     for(int i=2;i<Max;i++)
       euler[i]=i;
     for(int i=2;i<Max;i++)
        if(euler[i]==i)
           for(int j=i;j<Max;j+=i)
              euler[j]=euler[j]/i*(i-1);//先進行除法是為了防止中間資料的溢位 
}

本題就是尤拉函式的直接使用:
#include<stdio.h>
#include<string.h>
#include<math.h>
#define LL  __int64
#define Max 1005000
LL sum[1005000];
void init(){
	sum[1]=1;
	for(LL i=2;i<Max;i++)
	sum[i]=i;
	for(LL i=2;i<Max;i++)
	if(sum[i]==i)
	for(LL j=i;j<Max;j+=i)
	sum[j]=sum[j]/i*(i-1);
}
	
int main()
{
	LL n;
    LL i,t;
	init();
	while(scanf("%I64d",&n)!=EOF)
	{
		t=0;
		if(n==0)break;
		for(i=2;i<=n;i++)
		t+=sum[i];
		printf("%I64d\n",t);
	}
	return 0;
}


相關推薦

poj 2478 Farey Sequence函式

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

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)

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  

hdoj 1286 找新朋友 函式

找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15913 Accepted Submission(s): 8501 Pr

線性篩函式莫比烏斯函式

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

51nod-1040 最大公約數之和函式

基準時間限制:1 秒 空間限制:131072 KB 分值: 80 難度:5級演算法題  收藏  關注 給出一個n,求1-n這n個數,同n的最大公約數的和。比如:n = 6 1,2,3,4,5,6 同6的最大公約數分別為1,2,3,2,1,6,加在一起 = 15

2478 Farey Sequencephi打表

題目: Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible

51nod 1040 最大公約數之和函式

水平較水,想不到,看的討論版 與n的公約數,肯定是n的因子 那我們列舉n的因子就好了 假設因子為x,那麼x的貢獻次數就是1-n有多少個數與n的gcd=x,即1-n/x有多少個數與n/x互質,即ph

1040函式

【題目描述】 給出一個n,求1-n這n個數,同n的最大公約數的和。比如:n = 6 1,2,3,4,5,6 同6的最大公約數分別為1,2,3,2,1,6,加在一起 = 15 Input 1個數N(N <= 10^9) Output 公約數之

【51NOD】 1040-最大公約數之和函式

原題連線 首先補充一個知識點,尤拉函式: 在數論,對正整數n,尤拉函式是小於n的正整數中與n互質的數的數目(φ(1)=1)。此函式以其首名研究者尤拉命名(Euler’s totient function),它又稱為Euler’s totient f

hdu4556函式

把樹從中間隔開,只看前一半,然後第n行的分子分母大於n的數去掉,明顯這裡的個數是法裡數列,也就是0到1的最簡真分數的個數,而當法裡數列a[n]=k的時候,a[n+1]=a[n]+phi[n+1]=k+phi[n+1],其中phi[n+1]是n+1的尤拉函式值,這也很明顯,

HDOJ 1787 GCD Again函式

GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total

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,然

BZOJ 2818: Gcd函式

Description給定整數N,求1<=x,y<=N且Gcd(x,y)為素數的數對(x,y)有多少對.Input一個整數NOutput如題Sample Input4Sample Output4HINThint對於樣例(2,2),(2,4),(3,3),(4,2)

最大公約數之和 V2函式

【題目描述】 給出一個數N,輸出小於等於N的所有數,兩兩之間的最大公約數之和。相當於求 Ans=∑i=1i<n∑j=i+1j<ngcd(i,j)Ans=∑i=1i<n∑j=i+1j<ngcd(i,j) Input 第1行:1個數

hdu3501函式

Calculation 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5342    Accepted S

hdu2588 GCD 函式

GCD 題意:輸入N,M(2<=N<=1000000000, 1<=M<=N), 設1<=X<=N,求使gcd(X,N)>=M的X的個數。  (文末有題) 題解一: 當M==1時,顯然答案為N。 當M!=1。  X是

Farey Sequence——篩法求函式

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

Farey Sequence函數

公式 log function include string.h -1 mod eof 歐拉 題意:給出式子F F中分子分母互質,且分子小於分母 例: F2 = {1/2} F3 = {1/3, 1/2, 2/3} F4 = {1/4, 1/3, 1/2

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

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