标签:sea search lap called limit code 分享 lld local
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17021 | Accepted: 4536 |
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.
Source
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #define N 50000 5 #define len 1000000 6 #define inf 0x7fffffff 7 using namespace std; 8 typedef long long LL; 9 bool isprime[N+5]; 10 LL prime[N],cnt; 11 bool res[len+5]; 12 void init(){ 13 LL i,j; 14 cnt=0; 15 memset(isprime,true,sizeof(isprime)); 16 for (i=2;i<=N;i++){ 17 if (isprime[i]) prime[cnt++]=i; 18 for (j=0;j<cnt && i*prime[j]<=N;j++){ 19 isprime[i*prime[j]]=false; 20 if (i%prime[j]==0) break; 21 } 22 } 23 } 24 int main(){ 25 freopen ("prime.in","r",stdin); 26 freopen ("prime.out","w",stdout); 27 LL L,U,i,j,k,min,max,s,t; 28 init(); 29 while(scanf("%lld%lld",&L,&U)!=EOF){ 30 memset(res,0,sizeof(res)); 31 for(i=0;i<cnt;i++){ 32 s=(L-1)/prime[i]+1; 33 t=U/prime[i]; 34 for(j=s;j<=t;j++) 35 if(j>1) 36 res[j*prime[i]-L]=true; 37 } 38 k=-1,min=inf,max=-1; 39 LL dis,m1,m2; 40 for(i=0;i<=U-L;i++){ 41 if(!res[i]){ 42 if(k!=-1){ 43 dis=i-k; 44 if(dis>max){ 45 max=dis; 46 m1=i; 47 } 48 if(dis<min){ 49 min=dis; 50 m2=i; 51 } 52 } 53 if(i+L!=1)//注意 1 的时候特殊判断 1 不是素数 54 k=i; 55 } 56 } 57 if(max==-1) 58 printf("There are no adjacent primes.\n"); 59 else{ 60 printf("%lld,%lld are closest, %lld,%lld are most distant.\n",m2- 61 min+L,m2+L,m1-max+L,m1+L); 62 } 63 } 64 return 0; 65 }
标签:sea search lap called limit code 分享 lld local
原文地址:http://www.cnblogs.com/keximeiruguo/p/6057834.html