hdu 1286( 欧拉函数 )

题目链接: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值