1. 程式人生 > >POJ 2689 Prime Distance【素數篩】

POJ 2689 Prime Distance【素數篩】

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.

題解:任意一個合數 n 必定包含一個不超過 sqrt(n) 的質因子。用篩法求出 2~sqrt(n) 之間的所有素數,對於每個素數 p,將[l, r] 區間中能被 p 整除的數標記。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1000010;
int v[maxn], prime[maxn], k;
int m;
int a[maxn], vis[maxn];
void Prime(int n){
    memset(v, 0, sizeof v);
    m = 0;
    for(int i = 2; i <= n; i++){
        if(v[i] == 0){
            v[i] = i;
            prime[m++] = i;
        }
        for(int j = 0; j < m; j++){
            if(prime[j] > v[i] || prime[j] > n/i) break;
            v[i*prime[j]] = prime[j];
        }
    }
}
/*void Prime(int n){
    memset(v, 0, sizeof v);
    m = 0;
    for(int i = 2; i <= n; i++){
        if(v[i]) continue;
        prime[m++] = i;
        for(int j = i; j <= n/i; j++){
            v[i*j] = 1;
        }
    }
}*/
int main()
{
    int l, r;
    while(cin >> l >> r){
        if(l == 1) l = 2;
        memset(prime, 0, sizeof prime);
        memset(a, 0, sizeof a);
        memset(vis, 0, sizeof vis);
        Prime(int(sqrt(r+0.5)));
        int maxx = -1, minx = INF;
        int c1, c2, d1, d2;
        int k = 0;
        for(int i = 0; i < m; i++){
            for(int j = (l-1)/prime[i]+1; j <= r/prime[i]; j++){
                if(j > 1) vis[prime[i]*j - l] = 1;
            }
        }
        for(int i = 0; i <= r-l; i++){
            if(!vis[i]) a[++k] = i+l;
        }
        if(k < 2) printf("There are no adjacent primes.\n");
        else{
            for(int i = 2; i <= k; i++){
                int t = a[i] - a[i-1];
                if(t < minx){
                    minx = t;
                    c1 = a[i-1], c2 = a[i];
                }
                if(t > maxx){
                    maxx = t;
                    d1 = a[i-1], d2 = a[i];
                }
            }
            printf("%d,%d are closest, %d,%d are most distant.\n", c1, c2, d1, d2);
        }
    }
    return 0;
}