【素数】Prime Distance
题目描述
The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no proper factors (it is only evenly divisible by 1 and itself). The first prime numbers are 2,3,5,7 but they quickly become less frequent. One of the interesting questions is how dense they are in various ranges. Adjacent primes are two numbers that are both primes, but there are no other prime numbers between the adjacent primes. For example, 2,3 are the only adjacent primes that are also adjacent numbers.
Your program is given 2 numbers: L and U (1<=L< U<=2,147,483,647), and you are to find the two adjacent primes C1 and C2 (L<=C1< C2<=U) that are closest (i.e. C2-C1 is the minimum). If there are other pairs that are the same distance apart, use the first pair. You are also to find the two adjacent primes D1 and D2 (L<=D1< D2<=U) where D1 and D2 are as distant from each other as possible (again choosing the first pair if there is a tie).
Your program is given 2 numbers: L and U (1<=L< U<=2,147,483,647), and you are to find the two adjacent primes C1 and C2 (L<=C1< C2<=U) that are closest (i.e. C2-C1 is the minimum). If there are other pairs that are the same distance apart, use the first pair. You are also to find the two adjacent primes D1 and D2 (L<=D1< D2<=U) where D1 and D2 are as distant from each other as possible (again choosing the first pair if there is a tie).
输入
Each line of input will contain two positive integers, L and U, with L < U. The difference between L and U will not exceed 1,000,000.
输出
For each L and U, the output will either be the statement that there are no adjacent primes (because there are less than two primes between the two given numbers) or a line giving the two pairs of adjacent primes.
样例输入
2 17
14 17
样例输出
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.
这里要二次筛素数,还要注意数组不要越了界
#include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define ld long double using namespace std; const int N=1000005; const int M=47000; const int INF=0x3f3f3f3f; int ant1,ant2; int prime1[M+1],prime2[N]; int d,l,r,maxx,minn,mx1,mx2,mn1,mn2; bool isprime[N]; void prime_1() { memset(isprime,false,sizeof(isprime)); for(int i=2;i<=M;i++) { if(!isprime[i]) { prime1[ant1++]=i; for(int j=2*i;j<=M;j+=i) isprime[j]=true; } } } void prime_2(int l,int r) { memset(isprime,false,sizeof(isprime)); ant2=0; l=max(l,2); int k=sqrt(r*1.0); for(int i=0;i<ant1&&prime1[i]<=k;i++) { int t=l/prime1[i]; if(t*prime1[i]<l) t++; t=max(2,t); for(ll j=t;(ll)j*prime1[i]<=r;j++) { isprime[j*prime1[i]-l]=1; } } for(int i=0;i<=r-l;i++) { if(!isprime[i]) prime2[ant2++]=i+l; } } int main() { prime_1(); while(scanf("%d %d",&l,&r)!=EOF) { prime_2(l,r); //for(int i=0;i<1000;i++) //cout<<prime2[i]<<endl; if(ant2<2) { printf("There are no adjacent primes.\n"); continue; } maxx=-1; minn=INF; for(int i=1;i<ant2&&prime2[i]<=r;i++) { int d=prime2[i]-prime2[i-1]; if(d>maxx) { maxx=d; mx1=prime2[i-1]; mx2=prime2[i]; } if(d<minn) { minn=d; mn1=prime2[i-1]; mn2=prime2[i]; } } printf("%d,%d are closest, %d,%d are most distant.\n",mn1,mn2,mx1,mx2); } return 0; }