Prime Distance POJ

来源:互联网 发布:text对应java什么类型 编辑:程序博客网 时间:2024/06/07 02:00

题目链接


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 1714 17

Sample Output

2,3 are closest, 7,11 are most distant.There are no adjacent primes.

题意:

给定一个区间,找到这个区间内距离最近和距离最远的两对相邻的素数.

思路:

题目给的区间数很大, 需要用到大素数筛选,所谓大素数筛选其实就是在已经筛选的小素数中, 将小素数的倍数去掉.

代码:

#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<iostream>using namespace std;typedef long long LL;const int maxn = 1e7+10;bool vis[maxn];int pri[maxn];int num;void prime(){//素数表    num = 0;    memset(vis, false, sizeof(vis));    vis[1] = vis[0] = true;    for(int i = 2; i < maxn; ++i){        if (!vis[i]) pri[++num] = i;        for(int  j = 1; j <= num && i * pri[j] < maxn; ++j){            vis[i * pri[j]] = true;            if (i % pri[j] == 0) break;        }    }}int main(){    prime();    LL L, U;    while(scanf("%lld %lld", &L, &U) != EOF){        LL x1 = 0, y1 = 0, x2 = 0, y2 = 0;        LL minx = 0;        LL  maxx = 0;        if ( U < maxn){            int k = lower_bound(pri + 1, pri + 1 + num, L) - pri;            for(int i = k + 1; i <= num && pri[i] <= U; ++i){                if(minx == 0 || minx > pri[i] - pri[i-1]){                    minx = pri[i] - pri[i-1];                    x1 = pri[i-1];                    y1 = pri[i];                }if ( maxx == 0 || maxx < pri[i] - pri[i-1]){                        maxx = pri[i] - pri[i-1];                        x2 = pri[i-1];                        y2 = pri[i];                }            }        }else {            memset(vis, false, sizeof(vis));            int a;            //大素数筛法,          for(int  i = 1;i <= num && pri[i] * pri[i] <= U; ++i){                LL  k = L / pri[i];                LL t = k * pri[i];                if ( t < L) t += pri[i];                for(LL  tt = t; tt <= U; tt += pri[i])//标记每一个素数的倍数                    vis[tt - L] = true;                }for(LL  i = L; i <= U; ++i)                    if(!vis[i - L]){                     if(x1 == 0)                        x1 = x2 = i;                     else if(y1 == 0){                        y1 = y2 = i;                        minx = y1 - x1;                        maxx = y2 - x2;                        a = i;                     }else{                        if (minx > i - a){                            x1 = a;                            y1 = i;                            minx = i - a;                        }                        if(maxx < i - a){                            x2 = a;                            y2 = i;                            maxx = i - a;                        }                        a = i;                     }                }        }if (x1 && y1)        printf("%lld,%lld are closest, %lld,%lld are most distant.\n", x1, y1, x2, y2);        else printf("There are no adjacent primes.\n");    }return 0;}