其他分享
首页 > 其他分享> > 51Nod 1136

51Nod 1136

作者:互联网

1.通式: 其中p1,p2....pn为x的所有质因数,x是不为0的整数。(每种质因数只有一个,比如12=2*2*3那么φ(12)=φ(4*3)=φ(2^2*3^1)=(2^2-2^1)*(3^1-3^0)=4) 2.其中φ(1)=1(和1互质的数(小于等于1)就是1本身)。 3.若n是质数p的k次幂, ,因为除了p的倍数外,其他数都跟n互质。 4.特殊性质:当n为质数时, 。 5.欧拉函数是积性函数,但不是完全积性函数,若m,n互质,。 6.n的所有质因数之和等于φ(n)*n/2
 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<string.h>
 5 using namespace std;
 6 int euler(int x){
 7     int res=x,a=x;
 8     for(int i=2;i*i<=a;i++){
 9         if(a%i==0){//质因数
10             res=res-res/i;//N*(1-1/p)
11             while(a%i==0)a/=i;//每种质因数只有一个。
12         }
13     }
14     if(a>1)res=res-res/a;
15     return res;
16 }
17 int main(){
18     int n;
19     scanf("%d",&n);
20     printf("%d",euler(n));
21     return 0;
22 }

线性求欧拉函数模板:

缺点:范围不能过大。

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<string.h>
 5 using namespace std;
 6 const int maxn=1e6+7;
 7 int phi[maxn];
 8 void getphi(int n){
 9     int minDiv;
10     for(int i=1;i<n;i++)phi[i]=i;
11     for(int i=2;i*i<n;i++){
12         if(phi[i]==i){
13             for(int j=i*i;j<n;j+=i){
14                 phi[j]=i;
15             }
16         }
17     }
18     phi[1]=1;
19     for(int i=2;i<n;i++){
20         minDiv=phi[i];
21         if((i/phi[i])%phi[i]==0)
22             phi[i]=phi[i/phi[i]]*phi[i];
23         else
24             phi[i]=phi[i/phi[i]]*(phi[i]-1);
25     }
26 }
27 int main(){
28     int n;
29     getphi(1000);
30     scanf("%d",&n);
31     printf("%d",phi[n]);
32     return 0;
33 }

 

标签:质因数,函数,51Nod,res,1136,int,include,互质
来源: https://www.cnblogs.com/precious-LZY/p/10365088.html