欧拉函数

一:欧拉函数

欧拉函数定义:

φ(x)=x(1-1/p(1))(1-1/p(2))(1-1/p(3))(1-1/p(4))…..(1-1/p(n)) 其中p(1),p(2)…p(n)为x

的所有质因数;x是正整数; φ(1)=1(唯一和1互质的数,且小于等于1)。注意:每种质因数只有一个。

 

 

欧拉函数性质:

(1)   p^k型欧拉函数:

若N是质数p(即N=p), φ(n)= φ(p)=p-p^(k-1)=p-1。

若N是质数p的k次幂(即N=p^k),φ(n)=p^k-p^(k-1)=(p-1)p^(k-1)。

(2)mn型欧拉函数

设n为正整数,以φ(n)表示不超过n且与n互素的正整数的个数,称为n的欧拉函数值。若m,n互质,φ(mn)=(m-1)(n-1)=φ(m)φ(n)。

(3)特殊性质:

若n为奇数时,φ(2n)=φ(n)。

对于任何两个互质 的正整数a,n(n>2)有:a^φ(n)=1 mod n (恒等于)此公式即 欧拉定理

当n=p 且 a与素数p互质(即:gcd(a,p)=1)则上式有: a^(p-1)=1 mod n (恒等于)此公式即 费马小定理

 

欧拉函数模板O( n*sqrt(ai) ):

 

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int n;
 4 
 5 int oula(int x)
 6 {
 7     int res=x;
 8     for(int i=2;i<=x/i;i++)
 9     {
10         if(x%i==0)
11         {
12             res=res/i*(i-1);
13             while(x%i==0)x/=i;
14         }
15     }
16     if(x>1)res=res/x*(x-1);
17     return res;
18 }
19 
20 int main()
21 {
22     scanf("%d",&n);
23     for(int i=1;i<=n;i++)
24     {
25         int a;
26         scanf("%d",&a);
27         printf("%d\n",oula(a));
28     }
29 
30     return 0;
31 }

 

筛法求欧拉函数模板

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int N=1e6+100;
 5 int n,primes[N],cnt,phi[N];  //phi存储每个数的欧拉函数
 6 bool st[N];
 7 
 8 void get_eulers(int n)
 9 {
10     phi[1]=1;
11     for(int i=2;i<=n;i++)
12     {
13         if(!st[i])
14         {
15             primes[cnt++]=i;
16             phi[i]=i-1;
17         }
18         for(int j=0;primes[j]<=n/i;j++)
19         {
20             st[primes[j]*i]=1;
21             if(i%primes[j]==0)
22             {
23                 phi[primes[j]*i]=phi[i]*primes[j];
24                 break;
25             }
26             phi[primes[j]*i]=phi[i]*(primes[j]-1);
27         }
28     }
29 }
30 
31 int main()
32 {
33     scanf("%d",&n);
34     get_eulers(n);
35     
36     ll res=0;
37     for(int i=1;i<=n;i++)res+=phi[i];
38     printf("%lld\n",res);
39     
40     return 0;
41 }

 

posted @ 2022-03-08 17:44  wellerency  阅读(1471)  评论(0编辑  收藏  举报