zoukankan      html  css  js  c++  java
  • POJ-2689-Prime Distance(素数区间筛法)

    链接:

    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.
    ", mil, mir, mal, mar);
            }
        }
    
        return 0;
    }
    
  • 相关阅读:
    手把手教你搭建docker-hub
    fabric可以跨链吗?
    手把手教你编译Fabric源代码
    区块链中的密码学之数字证书体系(十四)
    区块链中的密码学之默克尔树(十五)
    区块链中的密码学之数字签名方案(十二)
    区块链中的密码学之非对称密码概述(九)
    区块链中的密码学之非对称密码椭圆曲线(十三)
    在Visual Studio 2017中使用Asp.Net Core构建Angular4应用程序
    从USB驱动器运行Windows 10
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11789662.html
Copyright © 2011-2022 走看看