zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    图片旋转 1. cv2.getRotationMatrix2D(获得仿射变化矩阵) 2. cv2.warpAffine(进行仿射变化)
    OpenCV入门之寻找图像的凸包(convex hull)
    从泊松方程的解法,聊到泊松图像融合
    人脸属性分析--性别、年龄和表情识别
    numpy的文件存储.npy .npz 文件详解
    python dlib学习(五):比对人脸
    OpenCV3与深度学习实例:Dlib+VGG Face实现两张脸部图像相似度比较
    用Python实现一个简单的——人脸相似度对比
    pom.xml配置,针对mvn clean install -P参数(环境参数)打包
    Springboot使用alibaba的fastJson,@JSONField不起作用的问题
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5483847.html
Copyright © 2011-2022 走看看