Prime Distance
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15305 Accepted: 4067 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.
题意:
输入一个 2,147,483,647 内的区间,区间最大不超过10^6,找出区间内的最近的两个素数和最远的两个素数,如果没有就输出 -1 ,距离相同输出比较小的一对。
思路:
从数的大小来看,数据比较大,所以从区间不超过10^6入手。
已知,非素数 S 一定存在一个小于等于 sqrt(S) 的因子,所以只需要预处理出5*10^4以内的素数,去除区间内这些素数的倍数,和数筛原理相似,剩下的就是区间内所有的素数,暴力找出答案即可。
代码:
#include"iostream"
#include"cstring"
#include"cstdio"
#include"string"
#include"vector"
#include"cmath"
#include"queue"
#include"map"
#include"set"
#include"algorithm"
#define MAXN 10005
#define lson id<<1
#define rson id<<1|1
#define LL long long
#define INF 0x7f7f7f7f
#define mod 1000000007
const double eps = 1e-10;
const double PI = 2.0*asin(1.0);
using namespace std;
bool flag[50005],mid[1000005];
int prime[50005],num;
void init()
{
num=0;
memset(flag,true,sizeof(flag));
flag[1]=false;
for(int i=2;i<=50000;i++)
{
if(flag[i])
{
prime[num++]=i;
for(int j=2*i;j<=50000;j+=i)
flag[j]=false;
}
}
}
int main(void)
{
init();
int l,r;
while(~scanf("%d%d",&l,&r))
{
memset(mid,true,sizeof(mid));
for(int i=0;i<num;i++)
{
if(prime[i]>r)
break;
int st=l/prime[i],ed=r/prime[i];
if(st<2)//从两倍开始筛选
st=2;
for(int j=st;j<=ed;j++)
{
if(prime[i]*j>=l&&prime[i]*j<=r)
mid[prime[i]*j-l]=false;
}
}
if(l==1)
mid[0]=false;
int k=0,minn=2000000,maxx=-1,ed=r-l+1,last=-1,id1,id2;
for(int i=0;i<ed;i++)
{
if(mid[i])
{
k++;
if(last==-1)
{
last=i;
continue;
}
if(minn>i-last)
{
id1=i;
minn=i-last;
}
if(maxx<i-last)
{
id2=i;
maxx=i-last;
}
last=i;
}
}
if(k<2)
printf("There are no adjacent primes.\n");
else
printf("%d,%d are closest, %d,%d are most distant.\n",id1+l-minn,id1+l,id2+l-maxx,id2+l);
}
return 0;
}