地址:http://poj.org/problem?id=2689
Prime Distance
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10234 | Accepted: 2755 |
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.
题意:求区间内那两个素数间距最小,那两个素数间距最大。
利用二次筛选,就是用1到sqrt(区间右端点)的素数将区间内和数筛掉。
未超时代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int moban[50010],shu[50010],prime[1000010];
int main()
{
unsigned int i,j,l,r,k;
memset(moban,0,sizeof(moban));
moban[1]=1;k=0;
for(i=1;i<=50000;i++)
if(moban[i]==0)
{
shu[k++]=i;
for(j=i*2;j<=50000;j+=i)
moban[j]=1;
}
while(scanf("%d%d",&l,&r)>0)
{
int maxx=0,minn=1000000,a=0,b=0;
memset(prime,0,sizeof(prime));
for(i=0;i<k;i++)
{
//if(shu[i]*shu[i]>r) break;
int s,t;
s=(l-1)/shu[i]+1; //大数运算好像会浪费时间,所以这里先将大数变小,然后再循环
t=r/shu[i];
for(j=s;j<=t;j++)
if(j>1) prime[j*shu[i]-l]=1;
}
if(l==1) prime[0]=1;
i=0;
while(prime[i]!=0)
i++;
int sum=0;
for(i=i+1;i<=r-l;i++)
{
sum++;
if(prime[i]==0)
{
if(sum>maxx)
{
maxx=sum;
a=i;
}
if(sum<minn)
{
minn=sum;
b=i;
}
sum=0;
}
}
if(maxx==0) printf("There are no adjacent primes.\n");
else printf("%d,%d are closest, %d,%d are most distant.\n",b+l-minn,b+l,a+l-maxx,a+l);
}
return 0;
}
超时代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int moban[50010],prime[1000010];
int main()
{
unsigned int i,j,l,r;
memset(moban,0,sizeof(moban));
moban[1]=1;
for(i=1;i<=50000;i++)
if(moban[i]==0)
for(j=i*2;j<=50000;j+=i)
moban[j]=1;
while(scanf("%d%d",&l,&r)>0)
{
int maxx=0,minn=1000000,a=0,b=0;
memset(prime,0,sizeof(prime));
for(i=1;i*i<=r;i++)
{
if(moban[i]==0)
{
j=l>i*2?l:i*2;
while(j%i!=0)
j++;
for(;j<=r;j+=i) //循环次数是一样的,但是数值大小不同
{
prime[j-l]=1;
}
}
}
if(l==1) prime[0]=1;
i=0;
while(prime[i]!=0)
i++;
int sum=0;
for(i=i+1;i<=r-l;i++)
{
sum++;
if(prime[i]==0)
{
if(sum>maxx)
{
maxx=sum;
a=i;
}
if(sum<minn)
{
minn=sum;
b=i;
}
sum=0;
}
}
if(maxx==0) printf("There are no adjacent primes.\n");
else printf("%d,%d are closest, %d,%d are most distant.\n",b+l-minn,b+l,a+l-maxx,a+l);
}
return 0;
}