POJ 2689 Prime Distance

Prime Distance
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18618   Accepted: 4984

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.

两次筛选素数,好题
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int g[50010],pos[50010],vis[5000010];
ll a,b,top=0;;
void get_prime()
{
    memset(g,0,sizeof(g));
    g[1]=1;
    g[2]=0;
    for(int i=2;i<=50010;i++)
    {
        if(g[i]==1) continue;
        pos[top++]=i;
        for(int j=2;j*i<=50010;j++)
        {
            g[j*i]=1;
        }
    }
}
int main()
{
    get_prime();
    while(scanf("%lld%lld",&a,&b)!=EOF)
    {
        if(a==1) a=2;
        memset(vis,0,sizeof(vis));
        for(ll i=0;i<top;i++)
        {
            ll l=(a-1)/pos[i]+1;//去掉本身
            ll r=b/pos[i];
            for(ll j=l;j<=r;j++)
            {
                if(j>1) vis[j*pos[i]-a]=1;
                //不减2145000000开不下
            }
        }
        ll maxn=-1,minn=INF,x1,x2,y1,y2,p=-1;
        for(ll i=0;i<=b-a;i++)
        {
            if(vis[i]==0)
            {
                if(p==-1) {p=i;continue;}
                if(i-p<minn){minn=i-p;x1=p;y1=i;}
                if(i-p>maxn){maxn=i-p;x2=p;y2=i;}
                p=i;
            }
        }
        if(maxn==-1) printf("There are no adjacent primes.
");
        else printf("%lld,%lld are closest, %lld,%lld are most distant.
",x1+a,y1+a,x2+a,y2+a);
    }
    return 0;   
}
原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7230834.html