Prime Distance
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10569 | Accepted: 2847 |
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.
Source
题意是说给你一个区间范围,让你求相邻的两个素数之间最近和最远的距离。因为此题的数据范围高达21亿,所以不能将所有的素数都存下来。只能进行筛选,因为区间范围小于1,000,000,所以可以把指定区间范围内的素数筛选出来。第一次筛选出来的是50001内的素数,然后根据第一次筛选出来的素数判断所给区间内的素数。
#include<stdio.h>
#include<string.h>
#define inf 99999999
#define N 50001
using namespace std;
int prime1[N],nprime1;
bool res[N*20],isprime[N*20];
long long l,u,re,m1,m2,m3,m4,minn,maxx;
void isprime1()
{
long long i,j;
nprime1=0;
memset(isprime,1,sizeof(isprime));
for(i=2; i<N; i++)
{
if(isprime[i])
{
prime1[nprime1++]=i;
for(j=i*i; j<N; j+=i)
{
isprime[j]=0;
}
}
}
}
int main()
{
isprime1();//第一次筛选
long long i,j,pre;
while(scanf("%lld%lld",&l,&u)!=EOF)
{
if(l==1)l=2;
minn=inf;
maxx=-inf;
re=0;
pre=-11;
memset(res,true,sizeof(res));
for(i=0; i<nprime1&&prime1[i]*prime1[i]<=u; i++)
{
long long st=(l/prime1[i])+(l%prime1[i]>0);
if(st==1)st++;
for(j=st*prime1[i]; j<=u; j+=prime1[i])
res[j-l]=false;
}
for(i=l; i<=u; i++)
{
if(res[i-l])
{
re++;
if(re!=1)
{
if(i-pre<minn)
{
m1=pre;
m2=i;
minn=i-pre;
}
if(i-pre>maxx)
{
m3=pre;
m4=i;
maxx=i-pre;
}
}
pre=i;
}
}
if(re<=1)
printf("There are no adjacent primes.\n");
else
printf("%lld,%lld are closest, %lld,%lld are most distant.\n",m1,m2,m3,m4);
}
return 0;
}