POJ-2689-Prime Distance（筛法）

Language:
Prime Distance
 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15777 Accepted: 4194

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.


Source

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <set>
#include <vector>
#define INF 0x3f3f3f3f
#define LL long long
#define bug cout<<"bug\n"
using namespace std;
#define MAXN 50005
const int M = 1e6+7;
int num_prime;
bool not_prime[MAXN*30];
int prime[MAXN];
long long a,b;
long long prime_2[M];
void get_prime()
{
long long i,j;
memset(not_prime,0,sizeof(not_prime));
num_prime=0;
not_prime[1]=1;
for(i=2; i<MAXN; ++i)
if(!not_prime[i])
{
num_prime++;
prime[num_prime]=i;
for(j=i*i; j<MAXN; j+=i)
not_prime[j]=1;
}
}
void get_primein_ab()
{
long long i,j,p;
memset(not_prime,0,sizeof(not_prime));
for(i=1; i<=num_prime; ++i)
{
p=a/prime[i];
while(p<=1)
p++;
for(j=prime[i]*p; j<=b; j+=prime[i])
if(j>=a)
not_prime[j-a]=1;
}
if(a==1)
not_prime[0]=1;
}
int main()
{
get_prime();
while(scanf("%I64d%I64d",&a,&b)!=EOF)
{
long long min=INF,max=-INF;
long long minl,minr,maxl,maxr;
get_primein_ab();
int num_prime2=0;
for(int i=0; i<=b-a; ++i)
if(!not_prime[i])
prime_2[++num_prime2]=i+a;
if(num_prime2<=1)
else
{
for(int i=1; i<num_prime2; ++i)
{
if(prime_2[i+1]-prime_2[i]>max)
{
max=prime_2[i+1]-prime_2[i];
maxl=prime_2[i];
maxr=prime_2[i+1];
}
if(prime_2[i+1]-prime_2[i]<min)
{
min=prime_2[i+1]-prime_2[i];
minl=prime_2[i];
minr=prime_2[i+1];
}
}
printf("%I64d,%I64d are closest, %I64d,%I64d are most distant.\n",minl,minr,maxl,maxr);
}
}
return 0;
}


©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客