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).
题意翻译
- 给定两个正整数 l,r,求 [l,r] 间 相邻 的两个差最大的质数和 相邻 的两个差最小的质数。如果区间内质数个数≤1,输出
There are no adjacent primes.
。 - 1<l<r<2^31,r-l<=10^6
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只有1e6的大小
由唯一分解定理(算数基本定理)可知,任何一个合数都可以写成若干质数的乘积
因此 l 到 r 这个区间内的合数肯定可以被写成若干个小于根号r的区间内的质数的乘积
因此我们可以先筛出来1~根号r的区间内的质数,再类似的把l到r内这个质数的倍数都标记为合数,质数的倍数肯定是合数嘛
筛过之后,就可以知道 l~r区间内哪个数是质数,再扫一遍,就能求出距离相差最大的质数和最小的质数,要注意细节
#include<bits/stdc++.h>
using namespace std;
const int Max=1e6+7;
bool prime[Max],Prime[Max];
void Math(int a,int b)
{
for(int i=0;i*i<=b;i++)
prime[i]=1;
for(int i=0;i<=b-a;i++)
Prime[i]=1;
for(int i=2;i*i<=b;i++)
{
if(prime[i])
{
for(int j=i+i;j<=b/j;j+=i)
prime[j]=0;
for(int j=ceil(a*1.0/i)*i;j<=b;j+=i)
{
if(j!=i)
Prime[j-a]=0;
}
}
}
}
int main()
{
int a,b;
int last=-1,now,a1,b1,a2,b2;
while(scanf("%d %d",&a,&b)!=EOF)
{
Math(a,b);
if(a>b) swap(a,b);
if(a==1) Prime[0]=0;
last=-1;
a1=-1;b1=1e8;
a2=1e8;b2=-1;
for(int i=0;i<=b-a;i++)
{
if(!Prime[i]) continue;
now=i+a;
if(last==-1)
{
last=i+a;
continue;
}
if(now-last>a1-b1)
{
a1=now;
b1=last;
}
if(now-last<a2-b2)
{
a2=now;
b2=last;
}
last=now;
}
if(b2==-1||a1==-1) printf("There are no adjacent primes.\n");
else printf("%d,%d are closest, %d,%d are most distant.\n",b2,a2,b1,a1);
}
return 0;
}