您的位置:首页 > 其它

hdoj2824 The Euler function(欧拉函数)

2017-03-07 18:29 239 查看
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) = 6p * 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 = 12phi(12) = 12 * (1-1/2) * (1-1/3) = 4phi(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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: