Prime Distance
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10291 | Accepted: 2776 |
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).
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.
Source
题型:数论
题意:给出一个区间,求出这个区间里靠的最近的相邻素数和离的最远的相邻素数
分析:
题目中U,L可达整型上限,直接筛必然超时。
可以采用二次筛法。U和L之间的合数,质因子不超过O(L^0.5),于是用筛法选出50000内的素数即可,因为50000的平方大于int上限了。再用这些素数去筛出U-L之间的合数,剩下的就是U-L之间的素数了,边筛边计算两个素数之间的差。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define LL long long
using namespace std;
const int N=50000;
const int maxn=0xfffffff;
int r[1000000],a[N+100],b[N+100],z;
int main(){
int a0,b0,i,j;
for(i=2;i<=N;i++){
if(!a[i]){
b[++z]=i;
for(j=i*2;j<=N;j+=i)
a[j]=1;
}
}
while(~scanf("%d%d",&a0,&b0)){
memset(r,0,sizeof(r));
int t=0,dis,mmax=-1,mmin=maxn,m1,m2;
for(i=1;i<=z;i++){
int s,t;
s=(a0-1)/b[i]+1;
t=b0/b[i];
for(j=s;j<=t;j++){
if(j>i)
r[j*b[i]-a0]=1;
}
}
int k=-1;
for(i=0;i<=b0-a0;i++){
if(!r[i]){
if(k!=-1){
dis=i-k;
if(dis>mmax){
mmax=dis;
m1=i+a0;
}
if(dis<mmin){
mmin=dis;
m2=i+a0;
}
}
if(i+a0!=1)
k=i;
}
}
if(mmax<0)
printf("There are no adjacent primes.\n");
else{
printf("%d,%d are closest, %d,%d are most distant.\n",m2-mmin,m2,m1-mmax,m1);
}
}
return 0;
}