zoukankan      html  css  js  c++  java
  • poj2689(Prime Distance) 区间素数筛

    Prime Distance
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 20709   Accepted: 5563

    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.
    
    分析:区间素数筛。

    #include<cstdio>
    #include<cstring>
    int c[46342],cnt=1,p[46342];
    int s[1000010];
    void prime()
    {
        for(int i=4;i<46342;i+=2) c[i]=1;
        p[0]=2;
        for(int i=3;i<46342;i++)
        if(!c[i])
        {
            p[cnt++]=i;
            for(int j=i+i;j<46342;j+=i) c[i]=1;
        }
    }
    
    int main()
    {
        long long a,b;
        prime();
        while(scanf("%lld%lld",&a,&b)!=-1)
        {
            memset(s,0,sizeof(s));
            for(int i=0;i<cnt&&p[i]*p[i]<=b;i++) 
            {
                long long temp=a/p[i]*p[i];
                if(temp<a) temp+=p[i];
                if(temp==p[i]) temp+=p[i];
                while(temp<=b)
                {
                    s[temp-a]++;
                    temp+=p[i];
                }
            }
            if(a==1) s[0]=1;
            int K=(int)(b-a);
            int Min=99999999,Max=0;
            int i=0;
            while(s[i]&&i<=K) i++;//先找出第一个素数 
            int C1=i,C2=-1,D1=i,D2=-1;
            int last1=i,last2=i;
            for(i++;i<=K;i++)
            {
                if(!s[i])
                {
                    if(C2==-1) {C2=i;Min=C2-C1;}
                    else if(Min>i-last1) 
                    {
                        Min=i-last1;
                        C1=last1;
                        C2=i;
                    }
                    
                    if(D2==-1) {D2=i;Max=D2-D1;}
                    else if(Max<i-last2)
                    {
                        Max=i-last2;
                        D1=last2;
                        D2=i;
                    }
                    last1=last2=i;
                }
            }
            if(D2==-1) printf("There are no adjacent primes.
    ");
            else printf("%lld,%lld are closest, %lld,%lld are most distant.
    ",C1+a,C2+a,D1+a,D2+a);
        }
        return 0;
    }
    View Code








  • 相关阅读:
    最短母串
    noip模拟测试7
    linux下的对拍程序
    noip模拟测试6
    QWidget 前后位置设定
    C++编程规范_第5~13条 设计风格
    random /timer/sort 示例代码
    资料准备
    【转】三维动态数组 分配与释放
    批量修改文件名
  • 原文地址:https://www.cnblogs.com/ACRykl/p/8654387.html
Copyright © 2011-2022 走看看