大区间筛素数 标记法 poj 2689

                                                                                          Prime Distance
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9355   Accepted: 2543

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,U,其中L和U的差值不超过1000000,计算这个区间内那两个素数的距离最小,那两个之间的距离最大。
解题思路:
打一张10000000以内的素数表,然后开一个标记数组,找出L到U以内的素数,然后再线性扫一遍。
代码:
#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
bool isprime[1000010],a[1000010];
long long prime[100000],b[100000],i,j,index=0;
void getprime()
{
   memset(isprime,1,sizeof(isprime));
      for(i=2;i<1000000;i++)
      {
          if(isprime[i])prime[index++]=i;
           for(j=0;j<index&&i*prime[j]<1000000;j++)
           {
              isprime[i*prime[j]]=0;if(i%prime[j]==0)break;
           }
      }
}
int main()
{   long long A,B,k,p,q,g,h,ans,m,n;
    getprime();
        while(~scanf("%lld%lld",&A,&B))
        {   memset(a,1,sizeof(a));
            if(A==1)A=2;ans=B-A+1;
             for(i=0;i<index&&prime[i]*prime[i]<=B;i++)
             {
                k=A/prime[i]+(A%prime[i]>0);if(k==1)k=2;
                 for(p=k*prime[i];p<=B;p+=prime[i])
                 {
                   if(a[p-A])a[p-A]=0,ans--;
                 }
             }
             if(ans<=1)puts("There are no adjacent primes.");
             else
             {     memset(b,0,sizeof(b));
                q=0;for(i=A;i<=B;i++)if(a[i-A])b[q++]=i;
               g=h=b[1]-b[0];m=n=1;
                for(i=2;i<q;i++)
                {
                   if(g<b[i]-b[i-1])g=b[i]-b[i-1],m=i;
                   else if(h>b[i]-b[i-1])h=b[i]-b[i-1],n=i;
                }
                cout<<b[n-1]<<","<<b[n]<<" are closest, "<<b[m-1]<<","<<b[m]<<" are most distant."<<endl;
             }
        }
    
     return 0;
}

posted @ 2013-05-26 16:40  线性无关  阅读(164)  评论(0)    收藏  举报