欧拉函数。
参考博客:https://www.cnblogs.com/handsomecui/p/4755455.html
相关题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1136
欧拉+费马小定理题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1256
欧拉函数的定义:
在数论中,对于正整数N,少于或等于N ([1,N]),且与N互质的正整数(包括1)的个数,记作φ(n)。
φ函数的值:
φ(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)。注意:每种质因数只有一个。
例如:
φ(10)=10×(1-1/2)×(1-1/5)=4;
1 3 7 9
φ(30)=30×(1-1/2)×(1-1/3)×(1-1/5)=8;
φ(49)=49×(1-1/7)=42;
1 #include<stdio.h> 2 long long phi(long long x) 3 { 4 int res=x,a=x; 5 for (int i=2;i*i<=a;i++) 6 { 7 if(a%i==0) 8 { 9 res=res/i*(i-1); 10 while(a%i==0) a=a/i; 11 } 12 } 13 if(a>1) res= res/a*(a-1); 14 return res; 15 } 16 int main() 17 { 18 long long N; 19 scanf("%lld",&N); 20 long long ans=phi(N); 21 printf("%lld\n",ans); 22 return 0; 23 }

浙公网安备 33010602011771号