hdoj2824 The Euler function(欧拉函数)

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

欧拉函数介绍:

先介绍一下暴力的欧拉函数:

Eular(m) = m - (1-1/p1) - (1-1/p2) - ... - (1-1/pk)  [其中 p1, p2...pk为m的素因子]

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<cstdio>
#include<algorithm> 
#define  MAXN 3000300
int euler[MAXN]; 
void init()
{
    euler[1]=1;
    for(int i=1;i<MAXN;i++)
        euler[i]=i;
    for(int i=2;i<MAXN;i++)
    {
        if(euler[i]==i)
        {
        	//printf("%d\n",i);
            for(int j=i;j<MAXN;j+=i)
                euler[j]=euler[j]*(i-1)/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;
}
方法二:(从性质定义入手)


代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define   maxn  3000300
int flag[maxn];
int phi[maxn];
int p[maxn];
int get_phi()
{
	int i,j;
	memset(flag,1,sizeof(flag));
	int l=0;
	phi[1]=1; 
	for(i=2;i<maxn;i++)
	{
		if(flag[i])
		{
			p[l++]=i;
			phi[i]=i-1;
		}
		for(j=0;j<l;j++)
		{
			if(i*p[j]>maxn)
			break;
			flag[i*p[j]]=0;
			if(i%p[j]==0)
			{
			  phi[i*p[j]]=phi[i]*p[j];
			  break;
			}
			  else
			  phi[i*p[j]]=phi[i]*(p[j]-1);
		} 
	}
 } 
int main()
{
	get_phi();
	/*for(int i=2;i<=100;i++)
	printf("%d\n",phi[i]);*/
	__int64 a,b;
	while(scanf("%d%d",&a,&b)!=EOF)
	{
	
		__int64 ans=0;
		for(int i=a;i<=b;i++)
		{
			ans+=phi[i];
		}
		printf("%I64d\n",ans);
	}
	
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值