HDU1286+线性筛素数

View Code
 1 /*
 2 欧拉函数+求与之互素的数的个数
 3 */
 4 #include<stdio.h>
 5 #include<string.h>
 6 #include<stdlib.h>
 7 #include<algorithm>
 8 #include<iostream>
 9 #include<queue>
10 #include<vector>
11 #include<map>
12 #include<math.h>
13 typedef long long ll;
14 //typedef __int64 int64;
15 const int maxn = 40005;
16 const int inf = 0x7FFFFFFF;
17 const double pi = acos(-1.0);
18 const double eps = 1e-8;
19 using namespace std;
20 int prime[ 50001 ],shu[ 50001 ];
21 int getPrime( int n ){
22     for( int i=1;i<n;i+=2 ) shu[ i ] = 1;
23     for( int i=0;i<n;i+=2 ) shu[ i ] = 0;
24     shu[ 1 ] = 0;
25     shu[ 2 ] = 1;
26     for( int i=3;i<n;i+=2 ){
27         if( shu[i]==1 ){
28             int t,delta;
29             delta = i*2;
30             t = delta+i;
31             while( t<n ){
32                 shu[ t ] = 0;
33                 t += delta;
34             }
35         }
36     }
37     int cnt = 1;
38     for( int i=2;i<n;i++ ){
39         if( shu[i]==1 ){
40             prime[ cnt++ ] = i;
41             //printf("%d\n",i);
42         }
43     }
44     return cnt;
45 }
46 int main(){
47     //freopen( "in2.txt","r",stdin );
48     //freopen( "out.txt","w",stdout );
49     int ca;
50     int cnt = getPrime( 50001 );
51     
52     scanf("%d",&ca);
53     while( ca-- ){
54         int n;
55         scanf("%d",&n);
56         
57         if( shu[n]==1 ){
58             printf("%d\n",n-1);
59             continue;
60         }
61         
62         int ans[ maxn ];
63         int tt = 0;
64         for( int i=1;i<cnt/*prime[i]*prime[i]<=n*/;i++ ){//在这里    WA了4,5次。。。
65             if( n%prime[i]==0 ){
66                 ans[ tt++ ] = prime[i];
67                 //printf("%d\n",prime[i]);
68             }
69         }
70         int res = n;
71         for( int i=0;i<tt;i++ ){
72             res = res*( ans[i]-1 )/ans[i];
73         }
74         //printf("n:%d ",n);
75         printf("%d\n",res);
76     }
77     return 0;
78 }

没什么好说的。。。wa了几次。。。因为for循环条件没写好。。

欧拉函数。。。

 

转载于:https://www.cnblogs.com/xxx0624/archive/2013/04/26/3045708.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值