欧拉函数篇

欧拉函数:

 

定义和简单性质

欧拉函数在OI中是个非常重要的东西,不知道的话会吃大亏的.

欧拉函数用希腊字母φ表示,φ(N)表示N的欧拉函数.

对φ(N)的值,我们可以通俗地理解为小于N且与N互质的数的个数(包含1).

欧拉函数的一些性质:

1.对于素数p, φ(p)=p-1,对于对两个素数p,q φ(pq)=pq-1

欧拉函数是积性函数,但不是完全积性函数.

证明:

函数的积性即:若m,n互质,则φ(mn)=φ(m)φ(n).由“m,n互质”可知m,n无公因数,所以φ(m)φ(n)=m(1-1/p1)(1-1/p2)(1-1/p3)…(1-1/pn)·n(1-1/p1')(1-1/p2')(1-1/p3')…(1-1/pn'),其中p1,p2,p3...pn为m的质因数,p1',p2',p3'...pn'为n的质因数,而m,n无公因数,所以p1,p2,p3...pn,p1',p2',p3'...pn'互不相同,所以p1,p2,p3...pn,p1',p2',p3'...pn'均为mn的质因数且为mn质因数的全集,所以φ(mn)=mn(1-1/p1)(1-1/p2)(1-1/p3)…(1-1/pn)(1-1/p1')(1-1/p2')(1-1/p3')…(1-1/pn'),所以φ(mn)=φ(m)φ(n).

即φ(mn)=φ(n)*φ(m)只在(n,m)=1时成立.

2.对于一个正整数N的素数幂分解N=P1^q1*P2^q2*...*Pn^qn.

   φ(N)=N*(1-1/P1)*(1-1/P2)*...*(1-1/Pn).

3.除了N=2,φ(N)都是偶数.

4.设N为正整数,∑φ(d)=N (d|N).

5.欧拉降幂公式:φ(C)表示C的欧拉函数值)

 

下面为求单个欧拉函数的代码(C++):

LL eular(LL m)
{
LL res=m,a=m;
for(LL 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;
}

如果求phi[1] phi[2] phi[3].....phi[n]代码如下:

int phi[maxn];
void Get_phi(int n)
{
memset(phi,0,sizeof(phi));
phi[1]=1;

for(int i=2;i<=n;i++)
{
if(!phi[i])
for(int j=i;j<=n;j++)
{
if(!phi[j]) phi[j]=j;
phi[j]=phi[j]/i*(i-1);
}
}

}

 

对于降幂公式的应用,我有一篇Exponial的题目(求 (n)=n(n − 1)(n − 2)21);

代码如下:

 

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 using namespace std;
 5 typedef long long LL;
 6 LL N,M;
 7 
 8 LL eular(LL m)
 9 {
10     LL res=m,a=m;
11     for(LL i=2;i*i<=a;i++)
12     {
13         if(a%i==0)
14         {
15             res=res/i*(i-1);
16             while(a%i==0)
17                 a/=i;
18         }
19     }
20     if(a>1) res=res/a*(a-1);
21     return res;
22 }
23 
24 LL Fast_mod(LL x,LL n,LL m)
25 {
26     LL res=1;
27     while(n>0)
28     {
29         if(n & 1) res=(res*x)%m;
30         x=(x*x)%m;
31         n/=2;
32     }
33     return res;
34 }
35 
36 LL work(LL n,LL m)
37 {
38     LL ans;
39     if(m==1) return 0;
40     else if(n==1) return 1;
41     else if(n==2) return 2%m;
42     else if(n==3) return 9%m;
43     else if(n==4) return Fast_mod(4,9,m);
44     else
45     {
46         LL phi=eular(m);
47         LL z=work(n-1,phi);
48         ans=Fast_mod(n,phi+z,m);
49     }
50     return ans;
51 }
52 
53 int main()
54 {
55     while(~scanf("%lld%lld",&N,&M))
56     {
57         printf("%lld\n",work(N,M));    
58     }
59     return 0;
60 }
View Code

 

posted @ 2018-04-06 14:57  StarHai  阅读(304)  评论(0编辑  收藏  举报