Prime DistanceTime Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5188 Accepted: 1384
Description
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).
Input
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.
Output
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.
Sample Input
2 17
14 17
Sample Output
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.
题意:给定L,U<= 2,147,483,647,U-L<10^6;
求这个区间距离最长连素数 和距离最短连续素数。
解法:筛选法求素数,筛选求出Sqrt(2^32)以内的素数。
利用 这些素数继续筛选[L,U]这个区间的素数。
因为U-L <10^6,所以方法是可行的。
理解筛选法的过程即可解出此题。
//1160K 0MS C++ 1762B #include<iostream> #include<algorithm> #include<math.h> #include<string.h> #include<string> using namespace std; const int M=47510; const int N=1000010; bool is[M]; bool mark[N]; int cnt=0; int prime[M]; int getprm(int n) { int i, j, k = 0; int s, e = (int)(sqrt(0.0 + n) + 1); memset(is, 1, sizeof(is)); is[0] = is[1] = 0; for (i = 4; i < n; i += 2) is[i] = 0; for (i = 3; i < e; i += 2) { if (is[i]) { for (s = i * 2, j = i * i; j < n; j += s) is[j] = 0; } } int cnt=0; for (i=2;i<n;i++) if(is[i]) prime[cnt++]=i; return cnt; } int main() { __int64 n,m,add,s,t,e,Min,Max,i,j,ans[4]; int cnt=getprm(47500+1); while(scanf("%I64d%I64d",&n,&m)!=EOF) { int sum=m-n+1; memset(mark,1,sum); if(n&1) s=n+1; else s=n; for (i=s;i<=m;i+=2) mark[i-n]=0; if(n==2) mark[0]=1; if(n==1) mark[0]=0,mark[1]=1; for(i=1;i<cnt;i++) { if(m<prime[i]*prime[i]) break; if(n%prime[i]) s=(n/prime[i]+1)*prime[i]; else s=n; if(s%2==0) { mark[s-n]=0; s+=prime[i]; } if(s<prime[i]*prime[i]) s=prime[i]*prime[i]; for (add=prime[i]*2,j=s;j<=m;j+=add) mark[j-n]=0; } i=0; while (!mark[i]) { i++; if(i>sum) break; } s=i; Max=-1,Min=2100000000; for (i=s+1;i<sum;i++) { if(mark[i]) { if(i-s>Max) { Max=i-s; ans[3]=i; ans[2]=s; } if(i-s<Min) { Min=i-s; ans[1]=i; ans[0]=s; } s=i; } } if(Max!=-1) printf("%I64d,%I64d are closest, %I64d,%I64d are most distant./n",ans[0]+n,ans[1]+n,ans[2]+n,ans[3]+n); else printf("There are no adjacent primes./n"); } return 0; }