标签:++ other cap tin eof poj ges ogr enter
Description
Input
Output
Sample Input
2 17 14 17
Sample Output
2,3 are closest, 7,11 are most distant. There are no adjacent primes.
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 const int maxn=50000+10; 6 using namespace std; 7 int p[maxn],a[maxn],f[1000000+10],t=0; 8 void prepare(){ 9 int m=(int)sqrt(50000); 10 for(int i=2;i<=m;i++) 11 if(!p[i]) 12 for(int j=i*i;j<=50000;j+=i)p[j]=1; 13 for(int i=2;i<=50000;i++)if(!p[i])a[++t]=i; 14 } 15 int main(){ 16 prepare(); 17 int L,U; 18 while(scanf("%d%d",&L,&U)!=EOF){ 19 if(L<2)L=2; 20 memset(f,0,sizeof(f)); 21 for(int i=1;i<=t;i++){ 22 int s=(L)/a[i],t=U/a[i]; 23 if(L%a[i])s++; //这步是必须的,不然会出现j*a[i]-L<0 而出现runtime error 24 if(s<2)s=2; 25 for(int j=s;j<=t;j++)f[j*a[i]-L]=1; 26 } 27 int p=-1,max_ans=-1,min_ans=1000000000,x1,y1,x2,y2; 28 for(int i=0;i<=U-L;i++){ 29 if(f[i]==0){ 30 if(p==-1){p=i;continue;} 31 if(max_ans<i-p){max_ans=i-p;x1=p+L;y1=i+L;} 32 if(min_ans>i-p){min_ans=i-p;x2=p+L;y2=i+L;} 33 p=i; 34 } 35 } 36 if(max_ans==-1)cout<<"There are no adjacent primes."<<endl; 37 else cout<<x2<<","<<y2<<" are closest, "<<x1<<","<<y1<<" are most distant."<<endl; 38 } 39 return 0; 40 }
标签:++ other cap tin eof poj ges ogr enter
原文地址:http://www.cnblogs.com/xingkongyihao/p/7232146.html