您的位置:首页 > 其它

HDU:2824 The Euler function(欧拉函数)

2017-03-08 17:16 549 查看


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

2009 Multi-University Training
Contest 1 - Host by TJU

 

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  欧拉函数 数学