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;
    }
    
  • 相关阅读:
    tp框架 php ajax 登陆
    js代码之编程习惯
    基于bootstrap的后台左侧导航菜单和点击二级菜单刷新二级页面时候菜单展开显示当前菜单
    用WebStorm进行Angularjs 2的开发
    关于datetimepicker只显示年、月、日的设置
    checkbox多选按钮变成单选
    mac navicate破解版汉化
    mac CodeIgniter和EasyWeChat 开发微信公众号
    python settings :RROR 1130: Host 'XXXXXX' is not allowed to connect to this MySQL server
    Eclipse中如何显示代码行
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11789662.html
Copyright © 2011-2022 走看看