# 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\left( 1\le L < U \le 2,147,483,647\right)$, and you are to find the two adjacent primes $C_1$ and $C_2\left( L\le C_1<C_2\le U\right)$ that are closest (i.e. $C_2-C_1$ 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 $D_1$ and $D2 \left( L ≤ D1 < D2 ≤ U\right)$ where $D_1$ and $D_2$ 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\in [1,2^{31}-1],r-l\le 10^6$，求这个区间内两个相邻且距离最近的质数，相邻且距离最远的质数。如果这个区间里没有质数，那么输出类似无解的一行。

## 题解：

题目的数据范围是$O(2^{31})/O(10^9)$，就算直接跑$O(N)$的线筛都跑不过，何况是多组数据。所以我们要考虑如何筛出长度为$O(10^6)$的区间中所有素数。

我们判断一个数$n$是不是质数，只需要在$O(\sqrt{n})$的时间内遍历$[2,\lfloor {\sqrt{n}}\rfloor ]$，看有没有数能整除$n$的。在这个题中，一组数据的复杂度是$(r-l)\times \sqrt{r}$，一组数据就难以承受。

如果我们把在上面遍历$[2,\lfloor {\sqrt{n}}\rfloor ]$改成遍历$[2,\lfloor {\sqrt{n}}\rfloor ]$之间的素数，那时间复杂度就降为$(r-l)\log r$，这样可以过掉更多的数据。

实际上上面的$[2,\lfloor {\sqrt{n}}\rfloor ]$枚举是没有必要的。我们只需要改变埃筛的枚举起点，就能直接在$O(\sqrt(r)\log^2 r)$的时间内筛出$l~r$之间的所有质数了。起点是$\lfloor \frac{l}{prime[i]}$，剩下的和埃筛就一样了。

这里不能用线筛，因为线筛需要连续，每个数只能被它最小的质因子筛掉，因此中间的部分就无法跳过了。

记得开long long，不然不小心枚举稍微超过$\mathrm{maxint}$就会死循环导致TLE。

## Code：

#include<cstdio>
#include<cstring>
bool is1[66666];
long long pri[10000],cnt=0;
bool is[1010000];
int main()
{
int l,r;
memset(is1,1,sizeof(is1));
is1[0]=0;
is1[1]=0;
for(int i=2;i<=65536;i++)//预处理出2^16内的所有质数
{
if(is1[i])
pri[++cnt]=i;
for(int j=1;j<=cnt&&i*pri[j]<=65536;j++)
{
is1[i*pri[j]]=0;
if(i%pri[j]==0)
break;
}
}
while(scanf("%d",&l)!=EOF)
{
memset(is,1,sizeof(is));
if(l==1)//特判1
is[0]=0;
scanf("%d",&r);
for(long long i=1;pri[i]*pri[i]<=r;i++)
{
int k=l/pri[i];
if(k<2)
k=2;//小心l很小的情况，不能让2成为合数
for(long long j=k*pri[i];j<=r;j+=pri[i])
if(j>=l)
is[j-l]=0;
}

int ans1=-10000000,ans2=-1;
int ans3=-1,ans4=-1;
int lst=-1;
for(int i=0;i<=r-l;i++)
if(is[i])
{
if(lst!=-1)
{
if(i-lst<ans2-ans1)
{
ans1=lst;
ans2=i;
}
if(i-lst>ans4-ans3)
{
ans3=lst;
ans4=i;
}
}
lst=i;
}
if(ans1==-10000000)
}