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;
    }
    
  • 相关阅读:
    Enterprise Library 2.0 技巧(4):如何用编程的方法来配置Logging Application Block
    Castle IOC容器实践之EnterpriseLibrary Configuration Facility
    Castle开发系列文章上了Castle的官方网站
    DataGridView也泛型?——一个不错的DataGridView控件
    Enterprise Library for .NET Framework 3.0 what would you like to see?
    设计是否可以更合理一点?——关于ORM中业务实体的讨论
    Enterprise Library 2.0 技巧(2):如何将配置信息保存到数据库中
    关于Castle IOC容器自动装配的问题
    数据库重构与数据库单元测试
    BLINQ初体验
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11789662.html
Copyright © 2011-2022 走看看