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;
    }
    
  • 相关阅读:
    ajax传参后台接受遇到问题
    使用xUnit为.net core程序进行单元测试(上)
    流程图规范
    知乎上看到的一些生活道理
    Dos常用命令
    c# 操作word写入特殊字符
    vb程序安装时需要在客户端安装MSSOAP30.dll,但注册不上,请问怎么处理
    VB 调用 webservice 出现:WSDLReader:Loading of the WSDL file failed HRESULT=0×80040154: 没有注册类别 解决方案
    The type 'System.Object' is defined in an assembly that is not referenced
    数据库还原失败解决方法
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11789662.html
Copyright © 2011-2022 走看看