码迷,mamicode.com
首页 > 其他好文 > 详细

关于素数的那点小事儿

时间:2016-01-10 20:02:12      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

HDOJ:2161

 1     #include<cstdio>
 2     #include<cstring>
 3     const int MAXN = 16100;
 4     bool prime[MAXN];
 5     void init(){
 6         memset(prime,true,sizeof(prime));
 7         prime[0]=prime[1]=false;
 8         for(int i = 2; i < MAXN; i++){
 9             if(prime[i]){
10                 if( i>MAXN/i )
11                     continue;
12                 for(int j=i*i; j<MAXN; j+=i)
13                     prime[j]=false;
14             }
15         }
16         prime[2]=false;
17     }
18     int main(){
19         init();
20         int tmp;
21         int T = 0;
22         while(~scanf("%d",&tmp)){
23             if( tmp <= 0 )
24                 break;
25             printf("%d: ",++T);
26             if(prime[tmp])
27                 printf("yes\n");
28             else
29                 printf("no\n");
30         }
31     }

 

POJ: 2689

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 using namespace std;
 6 const int MAXN = 100010;
 7 int prime[MAXN+1];
 8 void getPrime(){
 9     memset(prime,0,sizeof(prime));
10     for( int i=2; i<=MAXN; i++){
11         if(!prime[i])
12             prime[++prime[0]]=i;
13         for(int j=1; j<=prime[0]&&prime[j]<=MAXN/i;j++){
14             prime[prime[j]*i]=1;
15             if(i%prime[j]==0)
16                 break;
17         }
18     }
19 }
20 bool notPrime[1000010];
21 int prime2[1000010];
22 void getPrime2( int L, int R){
23     memset(notPrime,false,sizeof(notPrime));
24     if(L<2)
25         L=2;
26     for(int i=1; i<=prime[0]&&(long long)prime[i]*prime[i]<=R;i++){
27         int s=L/prime[i]+(L%prime[i]>0);
28         if(s==1)            s=2;
29         for(int j=s;(long long)j*prime[i]<=R;j++){
30             if((long long)j*prime[i]>=L)
31                 notPrime[j*prime[i]-L]= true;
32         }
33     }
34     prime2[0]=0;
35     for(int i = 0; i <= R-L; i++)
36         if(!notPrime[i])
37             prime2[++prime2[0]]=i+L;
38 }
39 int main(){
40     getPrime();
41     int a, b;
42     int _min, _max;
43     int x1, x2;
44     int y1, y2;
45     while(~scanf("%d %d",&a,&b)){
46         memset(prime2,0,sizeof(prime2));
47         getPrime2(a,b);
48         if( prime2[0]<2 ){
49             puts("There are no adjacent primes.");
50             continue;
51         }
52         _min = 1000000;
53         _max = 0;
54         for(int i=1; i<=(prime2[0]-1);i++){
55 
56             if((prime2[i+1]-prime2[i])<_min){
57                 x1 = prime2[i];
58                 x2 = prime2[i+1];
59                 _min = x2-x1;
60             }
61             if((prime2[i+1]-prime2[i])>_max){
62                 y1 = prime2[i];
63                 y2 = prime2[i+1];
64                 _max = y2-y1;
65             }
66         }
67         printf("%d,%d are closest, %d,%d are most distant.\n",x1,x2,y1,y2);
68     }
69 }

 

关于素数的那点小事儿

标签:

原文地址:http://www.cnblogs.com/blueprintf/p/5118987.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!