A - 算数基本定理

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.

题意:给定一个区间,输出: 在这个区间中,相差最少的一对素数和相差最多的一对素数,且每对素数中间无其他素数。

如果不存在,则输出"There are no adjacent primes."


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stack>
using namespace std;
const int N = 100010;
const int M = 46500;
const int INF = 999999999;
bool vis[N];
int mapp1[M+1], mapp2[N];
int x = 0, y;

//素数筛选;
//先在整个大的范围内筛选素数,是素数的标记vis为‘0’, 不是素数的标记vis为‘1’;
void Primemax()
{
    memset(vis, false, sizeof(vis));
    for(int i=2; i<=M; i++)
    {
        if(!vis[i])
        {
            mapp1[x++] = i;  //mapp1用于存储大范围内的素数;
            for(int j=2*i; j<=M; j+=i)
            {
                vis[j] = true;
            }
        }
    }
}

//在给定区间内,筛选素数;
void Primemin(int l , int u)
{
    memset(vis, false, sizeof(vis));
    y = 0;

    //如果l即区间的开始为2或者小于2,第一个素数一定为2;
    if(l<2)
        l = 2;

    int k = sqrt(u*1.0);

    //因为所给的区间会有在上边函数得出的素数区间范围外的情况,所以,可以根据所给区间的根下最大值筛选素数,如果该数不为素数,则一定会有mapp1素数区间的因子;
    for(int i=0; i<x&&mapp1[i]<=k; i++)
    {
        //计算第一个比L大且能把mapp1[i]整除的数是mapp1[i]的几倍,从此处开始筛
        int t = l/mapp1[i];
        if(t*mapp1[i]<l)
            t++;
        
        //如果不写 那么从1开始筛,2会被筛成非素数;
        if(t<=1)
            t = 2;

        //区间映射,比如区间长度为4的区间[4,7],映射到[0,3]中,因为题目范围2,147,483,647数组超出范围开不出来;并且标记不为素数的数;
        for(int j=t; (long long) j*mapp1[i]<=u; j++)
        {
            vis[j*mapp1[i]-l] = true;
        }
    }

    for(int i=0; i<=u-l; i++)
    {
        if(!vis[i])
            mapp2[y++] = i+l;//因为在区间映射的时候减去了l,所以现在加上l才是素数;
    }
}

int main()
{
    int l, u, dis, a1, b1, a2, b2, minn, maxx;
    Primemax();
    while(scanf("%d%d", &l, &u)!=EOF)
    {
        minn = INF, maxx = -1;
        Primemin(l, u);
        
        //如果所选区间的素数的个数小于两个,则输出There are no adjacent primes.;
        if(y<2)
        {
            printf("There are no adjacent primes.\n");
            continue;
        }
        //比较得出距离最小的一对素数和距离最大的一对素数,并输出;
        for(int i=1; i<y&&mapp2[i]<=u; i++)
        {
            dis = mapp2[i]-mapp2[i-1];
            if(dis>maxx)
            {
                x1 = mapp2[i-1];
                x2 = mapp2[i];
                maxx = dis;
            }
            if(dis<minn)
            {
                y1 = mapp2[i-1];
                y2 = mapp2[i];
                minn = dis;
            }
        }
        printf("%d,%d are closest, %d,%d are most distant.\n", y1, y2, x1, x2);
    }
    return 0;
}

【注】:素数筛

   如果一个数不是素数,则必存在一个小于它的素数为其的因数。假如我们已经获得了小于一个数的所有素数,我们只需确定该数不能被这些素数整除,这个数即为素数。换一个角度,获得一个素数时,将它的所有倍数均标记成非素数,这样当遍历到一个数时,如果它没有被任何小于它的素数标记为非素数,则确定其为素数。

      从2开始遍历2到1000000的所有整数,若当前整数没有因为它是某个小于其的素数的倍数而被标记成非素数,则判定其为素数,并标记它所有的倍数为非素数。然后继续遍历下一个数,直到遍历完2到000000区间内所有的整数。此时,所有没被标记成非素数的数字即为我们要求的素数。

#define MAXX 10001
int Mark[MAXX];//标记数组
int prime[MAXX];//记录素数的数组

//判断是否是一个素数  Mark 标记数组 index 素数个数
int Prime()
{
	int index = 0;
	memset(Mark,0,sizeof(Mark));
	for(int i=0; i<MAXX; i++)
    {
        //已被标记
        if(Mark[i] == 1)
        {
            continue;
        }
        else
        {
            //否则得到一个素数
            prime[index++] = i;
            //标记该素数的倍数为非素数
            for(int j = 2*i; j<MAXX; j += i)
            {
                Mark[j] = 1;
            }
        }
    }
    return index;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值