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

    地址:http://poj.org/problem?id=2689

    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,R求[L,R]内相距最近的素数对,和最远的素数对。如果有同样的,输出靠前的一对。
       解析:1<=L< U<=2,147,483,647。一个一个打素数表,不太行。任何一个合数n,必定包含一个不超过根号n的素因子。所以可以先把5e4内的素数(46341≈√2147483647)打出来,用它们把[L,R]内的合数标记掉,剩下的就是素数了。

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    const int maxn=1e6+10;
    bool vis[maxn],note[maxn];
    int a[maxn];
    ll l,r;
    void get_prime()
    {
        memset(vis,0,sizeof(vis));
        memset(note,0,sizeof(note));
        for(ll i=2;i*i<=r;i++)
        {
            if(vis[i]==0)
            {
                for(ll j = i+i;j*j<=r;j+=i)
                    vis[j]=1;
                for(ll j=max(2ll,(l+i-1)/i)*i;j<=r;j+=i)
                    note[j-l]=1;//此处防溢出
            }
        }
    }
    int main()
    {
        while(cin>>l>>r)
        {
            if(l==1)
                l++;
            get_prime();
            int ok = 0 ;
            int tot=0;
            for(int i =0;i<=r-l;i++)
            {
                if(note[i]==0)
                {
                    ok=1;
                    a[tot++]=i+l;
                }
            }
            if(!ok||tot==1)
            {
                cout<<"There are no adjacent primes."<<endl;
            }
            else
            {
                int maxx=-1,minn=0x3f3f3f3f;
                int l1,r1,l2,r2;
                for(int i=tot-1;i>0;i--)
                {
                    ll md=a[i]-a[i-1];
                    if(md>=maxx)
                    {
                        maxx=md;
                        r1=i;
                    }
                    if(md<=minn)
                    {
                        minn=md;
                        r2=i;
                    }
                }
                printf("%d,%d are closest, %d,%d are most distant.
    ",a[r2-1],a[r2],a[r1-1],a[r1]);
            }
        }
    }
    
    
    
     
  • 相关阅读:
    swift2.2当中的inout参数的使用
    Swift的7大误区
    Swift 设计指南之 编程规范
    我为什么用 SQLite 和 FMDB 而不用 Core Data
    ios学习笔记——代理设计模式
    ios学习笔记——UIImagePickerController
    ios学习笔记——保存图片到相册
    KVC中setValuesForKeysWithDictionary: (转载)
    ios学习笔记——GCD简介
    ios学习笔记——操作队列NSOperation的基本操作
  • 原文地址:https://www.cnblogs.com/liyexin/p/12728672.html
Copyright © 2011-2022 走看看