POJ2689-Prime Distance(區間素篩)

Prime Distance
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20161 Accepted: 5424
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.
題目:POJ2689
題意:給定一個區間[l,u],求區間內相鄰最近和最遠的兩個質數並輸出。
思路:(廢話,可以直接跳至第二段)題意很簡單,查找區間素數並輸出即可。但是(1<=l,u<=2,147,483,647),用素篩去篩的話,不說數組是否開的下,時間也會TLE。注意到題目中說|u-l|<=1000000。也就是說,我們真正需要的素數的區間範圍只有1e6。我們都會素篩這個算法,能否改進一下這個算法,只求得我們需要的區間的素數呢?
如果我們要求一個數n是否是素數,我們只需要判斷n/(2~sqrt(n))==0?即可。那麼同理我們要篩出n這個數是否爲素數,也只需要用2~sqrt(n)就可以篩出
那麼對於這道題,我們只需要用2~sqrt(u)去篩選[l,u],就可以篩出[l,u]間的素數,求的我們想要的答案了。具體實現見代碼。
AC代碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
#define met(s,k) memset(s,k,sizeof s)
#define scan(a) scanf("%d",&a)
#define scanl(a) scanf("%lld",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define scannl(a,b) scanf("%lld%lld",&a,&b)
#define scannn(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define prin(a) printf("%d\n",a)
#define prinl(a) printf("%lld\n",a)
using namespace std;
typedef long long ll;
const ll maxn=46400;//sqrt2,147,483,647int prime[maxn],big_prime[1000005];
ll min_prime[2],max_prime[2];
ll l,u;
void select(ll l,ll u)
{
    met(prime,0);
    met(big_prime,0);
    ll limit=(ll)sqrt((double)u);//每次篩到sqrt(u),即可得到想要的區間的素數
    for(ll i=2; i<=limit; i++)
    {
        if(!prime[i])
        {
            for(ll j=2*i; j<=limit; j+=i)prime[j]=1;//普通素篩
            for(ll j=(l/i+(l%i!=0))*i; j<=u&&j>0/*有可能最靠近l的i的倍數以及溢出了ll,注意RE*/; j+=i)if(j!=i)big_prime[j-l]=1;//j=(l/i+(l%i!=0))*i找出最靠近l的i的倍數,
        }
    }
}
int main()
{
    while(~scannl(l,u))
    {
        select(l,u);
        ll last=-1;
        max_prime[0]=-1,min_prime[0]=100000;
        if(l==1)big_prime[0]=1;
        for(int i=0; i<=(u-l); i++)
        {
            if(!big_prime[i])
            {
                if(last!=-1)
                {
                    if(i-last>max_prime[0])max_prime[0]=i-last,max_prime[1]=last;
                    if(i-last<min_prime[0])min_prime[0]=i-last,min_prime[1]=last;
                }
                last=i;
            }
        }
        if(max_prime[0]!=-1)printf("%lld,%lld are closest, %lld,%lld are most distant.\n",min_prime[1]+l,min_prime[1]+l+min_prime[0],max_prime[1]+l,max_prime[1]+l+max_prime[0]);
        else printf("There are no adjacent primes.\n");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章