hdu 1286( 欧拉函数 )

来源:互联网 发布:诺基亚lumia800软件 编辑:程序博客网 时间:2024/06/05 02:10

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1286

 数学题真的是有点吃不消了。。。

View Code
 1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 //可以快速求出欧拉函数的值 ( P为N的质因子 ) 5 //若(N%P==0 && (N/P)%P==0) 则有:E(N)=E(N/P)*P; 6 //若(N%P==0 && (N/P)%P!=0) 则有:E(N)=E(N/P)*(P-1); 7  8 //欧拉公式求1-n中与n互质的个数 9 int Eular(int n){10     int cnt=1;11     for(int i=2;i*i<=n;i++){12         //由于是从i=2开始的,故if中的i一定为n的质因子13         if(n%i==0){14             cnt*=i-1;15             n/=i;16             while(n%i==0){17                 n/=i;18                 cnt*=i;19             }20         }21     }22     if(n>1){23         cnt*=n-1;24     }25     return cnt;26 }27 28 29 30 int main(){31     int _case;32     scanf("%d",&_case);33     while(_case--){34         int n;35         scanf("%d",&n);36         printf("%d\n",Eular(n));37     }38     return 0;39 }

 

0 0