【POJ 2689】Prime Distance

【题目】

 传送门

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,r,询问 [ l , r ] 中距离最短的两个素数和距离最长的两个素数

这道题中 l,r 的范围比较大,直接筛素数显然是不现实的,但是 r - l 的范围就比较小了,我们考虑区间筛

就是首先预处理出 \sqrt r 范围内的素数(这里用线性筛),然后用这些素数来筛 [ l , r ] 中的素数

最后统计答案就行了

这里说一下代码中坑过我的地方:

  • 各种符号的优先级,例如 int 范围应是 (1ll<<31ll)-1 而不是 1ll<<31ll-1
  • 注意数据中 l 等于 0 或 1 的情况,这些时候要特判
  • r 有可能取 int 上界,即 2147483647,这样如果用 for 循环,退出循环时要超 int 范围,要用 long long

 

【代码】

#include
#include
#include
#include
#define N 50000
#define L 1000005
#define inf (1ll<<31ll)-1
using namespace std;
bool f[L],mark[N];
int sum,m[L],prime[N];
void init()
{
	int i,j,Max=(int)sqrt(inf);
	memset(mark,true,sizeof(mark));
	mark[0]=mark[1]=false;
	for(i=1;i<=Max;++i)
	{
		if(mark[i])  prime[++sum]=i;
		for(j=1;j<=sum&&i*prime[j]<=Max;++j)
		{
			mark[i*prime[j]]=false;
			if(i%prime[j]==0)  break;
		}
	}
}
void solve(int l,int r)
{
	long long i,j;
	int k,x=sqrt(r)+1;
	memset(f,true,sizeof(f));
	for(i=1;i<=sum&&prime[i]<=x;++i)
	{
		k=l/prime[i];
		if(k<2)  k=2;
		for(j=prime[i]*k;j<=r;j+=prime[i])
		  if(j>=l)
		    f[j-l]=false;
	}
}
int main()
{
	int l,r,num;
	long long i;
	init();
	while(~scanf("%d%d",&l,&r))
	{
		num=0;
		if(l<2)
		  l=2;
		solve(l,r);
		for(i=l;i<=r;++i)
		  if(f[i-l])
		    m[++num]=i;
		if(num<2)
		  printf("There are no adjacent primes.\n");
		else
		{
			int cx,cy,fx,fy;
			int minn=inf,maxn=-inf+1;
			for(i=2;i<=num;++i)
			{
				if(m[i]-m[i-1]maxn)  maxn=m[i]-m[i-1],fx=m[i-1],fy=m[i];
			}
			printf("%d,%d are closest, ",cx,cy);
			printf("%d,%d are most distant.\n",fx,fy);
		}
	}
	return 0;
}

 

你可能感兴趣的:(#,线性筛)