题目链接 POJ 2689
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).
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.
题目意思:给定一个区间,求出这个区间中相邻两个质数差的最大值和最小值,若有多组仅输出第一组。
解题思路:右边界最大值为,2147483647,开方大概为46500,先求出小范围2 - 46500 质数表,然后用求出的质数表去筛选大质数,原因是所有合数都能分解为质因子的乘积,然后求出区间内的质数表,在查询一遍求出答案。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define inf 0xffffffff
#define maxn 1000000+10
#define LL long long
LL L,U;
LL l1,r1;
LL l2,r2;
LL mind,maxd;
LL sPrime[46500+10];
LL sCnt;
LL bPrime[maxn];
LL bCnt;
bool isPrime[maxn];
void init(){
maxd = 0;
mind = inf;
// cout << inf << endl;
}
void smallPrime(){
for(int i=2;i<=46500;i++) isPrime[i] = true;
sCnt = 0;
for(int i=2;i<=46500;i++){
for(int j=i*2;j<=46500;j+=i){
isPrime[j] = false;
}
}
for(int i=2;i<=46500;i++){
if(isPrime[i]) {
sPrime[sCnt++] = i;
}
}
}
void largePrime(){
bCnt = 0;
int k;
if(U<46500){
int t = 0;
while(sPrime[t]<L) t++;
while(sPrime[t]<=U){
bPrime[bCnt++] = sPrime[t++];
}
}else{
for(int i=0;i<maxn;i++) isPrime[i] = true;
for(int i=0;i<sCnt && sPrime[i]*sPrime[i]<=U;i++){
k = L/sPrime[i];
if(k*sPrime[i]<L) k++;
if(k<=1) k++;
while(k*sPrime[i] <= U){
isPrime[k*sPrime[i]-L] = false;
k++;
}
}
for(int i=0;i<=U-L;i++){
if(isPrime[i]){
bPrime[bCnt++] = i+L;
}
}
}
}
int main(){
smallPrime();
while(scanf("%lld %lld",&L,&U)!=EOF){
init();
largePrime();
if(bCnt<2){
printf("There are no adjacent primes.\n");
}else{
long long pre = bPrime[0],cur,temp;
for(int i=1;i<bCnt;i++){
cur = bPrime[i];
temp = cur - pre;
if(temp<mind){
l1 = pre;
r1 = cur;
mind = temp;
}
if(temp>maxd){
l2 = pre;
r2 = cur;
maxd = temp;
}
pre = cur;
}
printf("%lld,%lld are closest, %lld,%lld are most distant.\n",l1,r1,l2,r2);
}
}
return 0;
}