码迷,mamicode.com
首页 > 其他好文 > 详细

POJ-2689-Prime Distance(素数区间筛法)

时间:2019-11-03 23:36:52      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:you   思路   example   mem   区间   cto   sizeof   als   numbers   

链接:

https://vjudge.net/problem/POJ-2689

题意:

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).

思路:

考虑素数区间筛法, 然后遍历一遍素数即可。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>

using namespace std;
typedef long long LL;
const int INF = 1e9;

const int MAXN = 1e6+10;
LL l, r;

int Isprime[MAXN];
int Prime[MAXN];
int Islarge[MAXN];
int cnt;

void Euler()
{
    memset(Isprime, 0, sizeof(Isprime));
    memset(Islarge, 0, sizeof(Islarge));
    cnt = 0;
    int n = sqrt(r);
    for (int i = 2;i <= n;i++)
    {
        if (Isprime[i] == 0)
            Prime[++cnt] = i;
        for (int j = i;j <= n/i;j++)
            Isprime[j*i] = 1;
    }
    for (int i = 1;i <= cnt;i++)
    {
        int s = l/Prime[i];
        int e = r/Prime[i];
        for (int j = max(s, 2);j <= e;j++)
        {
            Islarge[1LL*Prime[i]*j-l] = 1;
        }
    }
}

int main()
{
    while(~scanf("%lld%lld", &l, &r))
    {
        Euler();
        /*
        for (int i = 1;i <= cnt;i++)
            cout << Prime[i] << ' ';
        cout << endl;
        */
        vector<int> p;
        if (l == 1)
            Islarge[0] = 1;
        for (int i = 0;i <= r-l;i++)
        {
            if (Islarge[i] == 0)
                p.push_back(i);
        }
        if (p.size() < 2)
            puts("There are no adjacent primes.");
        else
        {
            int mmax = 0, mmin = INF;
            int mal, mar, mil, mir;
            for (int i = 1;i < (int)p.size();i++)
            {
                if (p[i]-p[i-1] > mmax)
                {
                    mmax = p[i]-p[i-1];
                    mal = p[i-1];
                    mar = p[i];
                }
                if (p[i]-p[i-1] < mmin)
                {
                    mmin = p[i]-p[i-1];
                    mil = p[i-1];
                    mir = p[i];
                }
            }
            mil += l, mir += l, mal += l, mar += l;
            printf("%d,%d are closest, %d,%d are most distant.\n", mil, mir, mal, mar);
        }
    }

    return 0;
}

POJ-2689-Prime Distance(素数区间筛法)

标签:you   思路   example   mem   区间   cto   sizeof   als   numbers   

原文地址:https://www.cnblogs.com/YDDDD/p/11789662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!