• POJ2689:素数区间筛选


    Prime Distance
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 15820   Accepted: 4202

    Description

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

    Input

    Each line of input will contain two positive integers, L and U, with L < U. The difference between L and U will not exceed 1,000,000.

    Output

    For each L and U, the output will either be the statement that there are no adjacent primes (because there are less than two primes between the two given numbers) or a line giving the two pairs of adjacent primes.

    Sample Input

    2 17
    14 17
    

    Sample Output

    2,3 are closest, 7,11 are most distant.
    There are no adjacent primes.
    注意:L可能为1
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int MAXN = 1000005;
    typedef long long LL;
    LL l, u;
    bool isPrime[MAXN], isSmallPrime[MAXN];
    int prime[MAXN], len;
    void prep()
    {
        memset(isPrime, true, sizeof(isPrime));
        memset(isSmallPrime, true, sizeof(isSmallPrime));
        isSmallPrime[0] = false;
        isSmallPrime[1] = false;
        len = 0;
        for(LL i = 2; i * i <= u; i++)
        {
            if(isSmallPrime[i])
            {
                for(LL j = i + i; j * j <= u; j += i)
                {
                    isSmallPrime[j] = false;
                }
                for(LL j = max(i + i, (l + i - 1) / i * i); j <= u; j += i)
                {
                    isPrime[j-l] = false;
                }
            }
        }
        for(LL i = l; i <= u; i++)
        {
            if(isPrime[i-l])
            {
                prime[len++] = i;
            }
        }
    }
    int main()
    {
        while(scanf("%I64d %I64d", &l, &u) != EOF)
        {
            if(l == 1)  l++;
            prep();
            if(len <= 1)
            {
                printf("There are no adjacent primes.
    ");
                continue;
            }
            int mind = 0x3f3f3f3f, a, b;
            int maxd = 0, c, e;
            for(int i = 1; i < len; i++)
            {
                int d = prime[i] - prime[i-1];
                if(mind > d)
                {
                    mind = d;
                    a = prime[i-1];
                    b = prime[i];
                }
                if(maxd < d)
                {
                    maxd = d;
                    c = prime[i-1];
                    e = prime[i];
                }
            }
            printf("%d,%d are closest, %d,%d are most distant.
    ", a, b, c, e);
        }
        return 0;
    }
  • 相关阅读:
    怎么重新启动网卡
    @JsonProperty的使用
    JAVA中的反射机制
    spring的IOC入门案例
    spring的IOC底层原理
    maven+Spring环境搭建
    SpringMVC与Struts2区别与比较总结
    Struts2面试题
    oracle自增序列创建
    Hibernate分页查询报错
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5483847.html
Copyright © 2020-2023  润新知