POJ 2689 Prime Distance (大素数筛)

Prime Distance
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14876   Accepted: 3946

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 都在int范围内,它们相差小于100w,求他们之间相距最小和相距最大的素数

思路:普通筛素数肯定超时,今天见到一种新的筛数法,挺好的一道题
ac代码:
 
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#define MAXN 1001010
#define MOD 1000000007  
#define LL long long 
#define INF 0xfffffff 
using namespace std;
int v[MAXN];
LL prime[MAXN];
LL prime2[MAXN];
LL k,l,u;
void db()//第一次筛
{
	memset(v,0,sizeof(v));
	v[1]=1;
	k=0;
	for(LL i=2;i<=50012;i++)
	{
		if(!v[i])
		{
			prime[++k]=i;
			for(LL j=i*i;j<50001;j+=i)
			v[j]=1;
		}
	}
}
void dbagain()//第二次筛,筛的是非素数
{
	LL t;
	memset(v,0,sizeof(v));
	for(LL i=1;i<=k;i++)
	{
		t=l/prime[i];
		while(t*prime[i]<l||t<=1)
		t++;
		for(LL j=t*prime[i];j<=u;j+=prime[i])
		{
			if(j>=l)
			v[j-l]=1;
		}
	}
	if(l==1)
	v[0]=1;
}
int main()
{
	db();
	LL i;
	while(scanf("%lld%lld",&l,&u)!=EOF)
	{
		dbagain();
		LL cnt=0;
		for(i=0;i<=u-l;i++)
		if(!v[i])
		prime2[++cnt]=i+l;
		if(cnt<=1)
		{
			printf("There are no adjacent primes.\n");
			continue;
		}
		LL min=INF,max=-INF;
		LL aa,bb,cc,dd;
		for(i=1;i<cnt;i++)
		{
				if(prime2[i+1]-prime2[i]<min)
				min=prime2[i+1]-prime2[i],aa=prime2[i],bb=prime2[i+1];
				if(prime2[i+1]-prime2[i]>max)
				max=prime2[i+1]-prime2[i],cc=prime2[i],dd=prime2[i+1];
		}
		printf("%lld,%lld are closest, %lld,%lld are most distant.\n",aa,bb,cc,dd);
	}
	return 0;
} 


你可能感兴趣的:(POJ 2689 Prime Distance (大素数筛))