1. 程式人生 > >POJ 2689 Prime Distance(素數區間篩法--經典題)

POJ 2689 Prime Distance(素數區間篩法--經典題)

大致題意:給定[L,R]區間,找出區間內的每個素數

資料範圍 :

1<=L< R<=2,147,483,647)

R-L <=1,000,000.

R的數值太大,所以不能直接篩[0,R]的,要空間和時間優化,用到區間篩法,另外注意不能用int,因為R和L都是滿int的,中間有很多細節處理會爆int的,還要注意1不是素數,所以在區間篩中要特判一下,是個易錯的地方

//1160K	16MS	C++	1539B
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;


const int MAXN = 1e6+1e3;   //待篩的區間[L,R]長度
const int N = 50001;//保證大於(2^31-1)的算數平方根
bool prime[MAXN];
bool seive[N];
typedef long long ll;
int L,R,len;

void seg_seive(ll L,ll R)   //區間篩法
{
    len=R-L+1;
    for(int i=0;i<len;i++) prime[i]=1;
    if(1-L>=0) prime[1-L]=0;   //易錯因為1不是素數也不是合數,這也是區間篩的一個易錯bug
    for(ll i=2; i*i<=R ;i++)
    {
        if(seive[i])
        {
            for(ll j=max((ll)2,(L-1+i)/i)*i;j<=R;j+=i)  //第二個易錯點,j必須從大於1,因為L可能小於i,但是seive[i]是素數。
                prime[j-L]=false;
        }
    }
}
int main()
{

    for(int i=2;i<N;i++) seive[i]=1;
    for(int i=2;i*i<N;i++)  //預處理
        if(seive[i])
            for(int j=2*i;j<N;j+=i)
                seive[j]=false;
        
    while(~scanf("%d%d",&L,&R))
    {
        seg_seive(L,R);

        int lmax,rmax,lmin,rmin;
        int mmax=-1,mmin=(1<<30);
        int t=-1;

        for(int i=0;i<len;i++)
            if(prime[i])
            {
                if(t>=0)
                {
                    if(mmax<i-t) mmax=i-t,lmax=t+L,rmax=i+L;
                    if(mmin>i-t) mmin=i-t,lmin=t+L,rmin=i+L;
                    t=i;
                }
                else t=i;
            }

        if(mmax>0) printf("%d,%d are closest, %d,%d are most distant.\n",lmin,rmin,lmax,rmax);
        else puts("There are no adjacent primes.");
    }
    return 0;
}


相關推薦

POJ 2689 Prime Distance素數區間--經典

大致題意:給定[L,R]區間,找出區間內的每個素數 資料範圍 : 1<=L< R<=2,147,483,647) R-L <=1,000,000. R的數值太大,所以不能直接篩

poj 2689 Prime Distance大數區間素數

題意:給定區間[L,R],求區間內距離最近的相鄰素數對和距離最遠的相鄰素數對,區間長度不超過1e6。 解題方案:用篩法求出[L,R]的所有素數——利用“合數n一定有小於或等於sqrt(n)的素數因子“這條性質,先預處理出sqrt(2,147,483,647)範圍內的所有素

POJ 2689 Prime Distance 二次?素數

Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2689 Description

POJ 2689 Prime Distance素數

The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticia

Poj.2689 Prime Distance素數篩選】 2015/11/13

Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14884 Accepted: 3949 Description The branch of mathematic

POJ 2689 Prime Distance-卡時間-素數

題意:給定兩個數l,r求這之間最近和最遠的兩個素數。資料範圍是整數的上限。r-l<=10^6 分析:總思路是把l和r間的素數全部找出來,然後遍歷一遍求最小距離和最大距離。用一個函式預處理資料範圍內的所有素數是不現實的,一來陣列不可r能開那麼大二來會超時。想想素數篩的思

POJ 2689 - Prime Distance - [素數]

代碼 one mini rop esc imu script less ogr 題目鏈接:http://poj.org/problem?id=2689 Time Limit: 1000MS Memory Limit: 65536K Description The branc

POJ2689:Prime Distance大數區間素數

possible lib ios rop sel poj art eve led The branch of mathematics called number theory is about properties of numbers. One of the areas

POJ 2689 Prime Distance [選取素數]【數論】

題目連結:http://poj.org/problem?id=2689 ————————-. Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1

Prime Distance二次素數

led question nds state rip input bsp round easy Description The branch of mathematics called number theory is about properties of

2689 Prime Distance2次用

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

POJ 3216 Prime Path寬搜+素數

POJ 3216 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they woul

POJ 2689 - Prime Distance - [埃]

only namespace sting typedef led print it is visible define 題目鏈接:http://poj.org/problem?id=2689 Time Limit: 1000MS Memory Limit: 65536K

poj - 2689 Prime Distance

ans air stream string its put cau rime memset The branch of mathematics called number theory is about properties of numbers. One of the a

POJ 3126 Prime Path素數,BFS最短路

【連結】http://poj.org/problem?id=3126 【題意】給個字串,求滿足形如“E...E...E”這種鬼樣子的E串最大長度,其中“...”可以是任意個數的任意字元(沒有也ok),但是三個E之間不能重疊 【思路】  ① 掏出素數篩的模板,找到1W以內素數

POJ 2689 Prime Distance

The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of n

POJ 1258 Agri-Net最小生成樹 Prim 模版

大意:新鎮長競選宣言就是將網路帶到每一個農場,給出農場個數,兩兩之間建光纜的耗費,求所有都聯通的最小耗費。 思路:最小生成樹,因為邊比較稠密,用Prim做。 PS;對於比較稠密的圖,用Prim,對於比較稀疏的圖,用 Kruskal。Kruskal是找邊的過程,

POJ-1321 棋盤問題DFS深搜+DFS板子

題目連結: 棋盤問題 題意: 在一個給定形狀的棋盤(形狀可能是不規則的)上面擺放棋子,棋子沒有區別。要求擺放時任意的兩個棋子不能放在棋盤中的同一行或者同一列,求解對於給定形狀和大小的棋盤,擺放k個棋子的所有可行的擺放方案C。 ‘#’ 表示棋盤區域,‘.’表示空白區域。

ZOJ 1729 & ZOJ 2006最小表示模板

輸出每個字串的最小字典序字串的下標! #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace

Prime DistancePOJ 2689

possible const code ini tin tput strong family name Description The branch of mathematics called number theory is about properties of num