HDU:2824 The Euler function(歐拉函數)

The Euler function

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6383    Accepted Submission(s): 2682


Problem Description
The Euler function phi is an important kind of function in number theory, (n) represents the amount of the numbers which are smaller than n and coprime to n, and this function has a lot of beautiful characteristics. Here comes a very easy question: suppose you are given a, b, try to calculate (a)+ (a+1)+....+ (b)
 

Input
There are several test cases. Each line has two integers a, b (2<a<b<3000000).
 

Output
Output the result of (a)+ (a+1)+....+ (b)
 

Sample Input
3 100
 

Sample Output
3042
 

Source
 

Recommend
gaojie


歐拉函數知識點:

要想求歐拉函數需要用到以下幾個性質( p爲素數 ):


1.  phi(p) == p-1 因爲素數p除了1以外的因子只有p,所以與 p 互素的個數是 p - 1個


2. phi(p^k) == p^k - p^(k-1) == (p-1) * p^(k-1)

證明:

令n == p^k,小於 n 的正整數共有 p^k-1 個,其中與 p 不互素的個數共 p^(k-1)-1 個,它們是 1*p,2*p,3*p ... (p^(k-1)-1)*p

所以phi(p^k) == (p^k-1) - (p^(k-1)-1) == p^k - p^(k-1) == (p-1) * p^(k-1)。


3. 如果i mod p == 0, 那麼 phi(i * p) == p * phi(i) (證明略)

舉個例子:

假設 p = 3,i = 6,p * i = 18 = 2 * 3^2;

phi(3 * 6) == 18*(1-1/2)*(1-1/3) = 6

p * phi(i) = 3 * phi(6) = 3 * 6 * (1-1/2) *  (1-1/3) = 6 = phi(i * p) 正確


4. 如果i mod p != 0, 那麼 phi(i * p) == phi(i) * (p-1) 

證明:

i mod p 不爲0且p爲質數, 所以i與p互質, 那麼根據積性函數的性質 phi(i * p) == phi(i) * phi(p) 其中phi(p) == p-1

所以 phi(i * p) == phi(i) * (p-1).

再舉個例子:

假設i = 4, p = 3, i * p = 3 * 4 = 12

phi(12) = 12 * (1-1/2) * (1-1/3) = 4

phi(i) * (p-1) = phi(4) * (3-1) = 4 * (1-1/2) * 2 = 4 = phi(i * p)正確


瞭解了這些性質之後 我們要做的就是就是寫程序了,具體咋寫呢,就讓我們參考一下素數篩,然後就可以寫啦。

#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 1e6+5;
bool flag[MAXN];///標記數組
int phi[MAXN];///歐拉函數值,i的歐拉函數值=phi[i]
int p[MAXN];///素因子的值
int cnt = 0;
void Get_phi()///篩法求歐拉函數
{
    cnt = 0;
    memset(flag, true, sizeof(flag));
    phi[1] = 1;
    for(int i=2; i<MAXN; i++)///線性篩法
    {
        if(flag[i])///素數
        {
            p[cnt++] = i;
            phi[i] = i-1;///素數的歐拉函數值是素數 - 1
        }
        for(int j=0; j<cnt; j++)
        {
            if(i*p[j] > MAXN)
                break;
            flag[i*p[j]] = false;///素數的倍數,所以i*p[j]不是素數
            if(i%p[j] == 0)///性質:i mod p == 0, 那麼 phi(i * p) == p * phi(i)
            {
                phi[i*p[j]] = p[j] * phi[i];
                break;//這個break不加也行,加上去速度加快,不加的話後面還會重複賦值的
            }
            else
                phi[i*p[j]] = (p[j]-1) * phi[i];///i mod p != 0, 那麼 phi(i * p) == phi(i) * (p-1) 
        }
    }
}
int main()
{
    Get_phi();
    int m;
    while(cin>>m)///測試
    {
        cout<<phi[m]<<endl;
    }
    return 0;
}


代碼如下:
#include <cstdio>
#include <cstring> 
#define MAX 3000010
int a,b;
int flag[MAX];
int su[MAX];
int phi[MAX];
void dabiao()
{
	memset(flag,0,sizeof(flag));
	int cnt=0;
	for(int i=2;i<MAX;i++)
	{
		if(flag[i]==0)
		{
			su[cnt++]=i;
			phi[i]=i-1;
		} 
		for(int j=0;j<cnt;j++)
		{
			if(i*su[j]>=MAX)
			{
				break;
			}
			flag[i*su[j]]=1;
			if(i%su[j]==0)
			{
				phi[i*su[j]]=su[j]*phi[i];
				break;
			}
			else
			{
				phi[i*su[j]]=(su[j]-1)*phi[i];
			}
		}
	}
}
int main()
{
	dabiao();
	while(scanf("%d%d",&a,&b)!=EOF)
	{
		long long ans=0;
		for(int i=a;i<=b;i++)
		{
			ans=ans+(long long)phi[i];
		}
		printf("%lld\n",ans);
	}
	return 0;
 } 



或者公式打表也行:


代碼如下:
#include<cstdio>
#include<algorithm> 
#define  MAXN 3000300
int euler[MAXN]; 
void init()
{
    euler[1]=1;
    for(int i=1;i<MAXN;i++)
        euler[i]=i;//初始化就是公式裏的x本身 
    for(int i=2;i<MAXN;i++)
    {
        if(euler[i]==i)//相當於他本身是素數 
        {
            for(int j=i;j<MAXN;j+=i)
                euler[j]=euler[j]*(i-1)/i;//所有以i爲因子的都用公式(相當於多個公式裏面的小項) 
        }
    }
}
int main()
{
	init();
	int a,b;
	
	while(scanf("%d%d",&a,&b)!=EOF)
	{
		__int64 ans=0;
		for(int i=a;i<=b;i++)
		ans+=euler[i];
		printf("%I64d\n",ans);
	}
	return 0;
}


發佈了277 篇原創文章 · 獲贊 20 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章