[题解](区间质数筛)POJ_2689 Prime Distance

区间筛素数:先筛出1~sqrt(R)的素数,然后对于每个询问只要用这些素数筛掉区间内的合数即可。

几个细节:
1.特判和1有关的一些情况

2.每次减去L偏移量,数组只开区间大小

3.POJ无法使用万能头文件(需要火星救援(大雾

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int prime[50009];
bool ck[1000009],tmp[1000009];
int l,r,tot;
int main(){
    for(int i=2;i<=50005;i++){
        if(!ck[i])prime[++tot]=i;
        for(int j=1;j<tot;j++){
            if(i*prime[j]>50005)break;
            ck[i*prime[j]]=1;
            if(i%prime[j]==0)break;
        }
    }
    while(scanf("%d%d",&l,&r)!=EOF){
        if(l==1)l=2;//特判
        memset(tmp,0,sizeof(tmp));
        for(int i=1;i<=tot;i++){
            int a=(l-1)/prime[i]+1;
            int b=r/prime[i];
            a=max(2,a);
            for(int j=a;j<=b;j++)
            tmp[j*prime[i]-l]=1;//减去l映射
        }
        int lst=-1,minans=0x7fffffff,maxans=0,xa,ya,xb,yb;
        for(int i=0;i<=r-l;i++){
            if(!tmp[i]){
                if(lst==-1){lst=i;continue;}
                if(maxans<i-lst)
                maxans=i-lst,xa=lst+l,ya=i+l;
                if(minans>i-lst)
                minans=i-lst,xb=lst+l,yb=i+l;
                lst=i;
            }
        }
        if(maxans==0)printf("There are no adjacent primes.\n");
        else printf("%d,%d are closest, %d,%d are most distant.\n",xb,yb,xa,ya);
    }
}

原文地址:https://www.cnblogs.com/superminivan/p/10846146.html

时间: 2024-10-12 07:10:02

[题解](区间质数筛)POJ_2689 Prime Distance的相关文章

uva_10140/poj_2689 Prime Distance(區間素數)

uva 題目鏈接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=35&page=show_problem&category=13&problem=1081&mosmsg=Submission+received+with+ID+15689014 題目大意:在給定範圍L和R,找到L和R範圍內差距最小和最大的兩對素數.1=< L<=R<=2,147,483,647,

POJ-2689 Prime Distance (两重筛素数,区间平移)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13961   Accepted: 3725 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

POJ题目2689 Prime Distance(任何区间素数筛选)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13459   Accepted: 3578 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

[ACM] POJ 2689 Prime Distance (大区间素数筛选)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12811   Accepted: 3420 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

[ACM] POJ 2689 Prime Distance (筛选范围大素数)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12811   Accepted: 3420 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

UVA 10140 - Prime Distance(数论)

10140 - Prime Distance 题目链接 题意:求[l,r]区间内最近和最远的素数对. 思路:素数打表,打到sqrt(Max)即可,然后利用大的表去筛素数,由于[l, r]最多100W,所以可以去遍历一遍,找出答案.注意1的情况,一开始没判断1,结果WA了 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #define INF 0x3f

POJ 2689 Prime Distance 素数筛选法应用

题目来源:POJ 2689 Prime Distance 题意:给出一个区间L R 区间内的距离最远和最近的2个素数 并且是相邻的 R-L <= 1000000 但是L和R会很大 思路:一般素数筛选法是拿一个素数 然后它的2倍3倍4倍...都不是 然后这题可以直接从2的L/2倍开始它的L/2+1倍L/2+2倍...都不是素数 首先筛选出一些素数 然后在以这些素数为基础 在L-R上在筛一次因为 R-L <= 1000000 可以左移开一个1百万的数组 #include <cstdio>

poj 2689 Prime Distance 【数论】【筛法求素数】

题目链接:传送门 题目大意: 给你L和R两组数,L和R的范围是2^32,其间隔(即R-L最大为1,000,000.) .让你求出L和R之间素数的最大间隔和最小的间隔. 比如 2 17.之间的最小素数间隔是2 3,最大的素数间隔是11 17. 要是直接进行一个2^32次方筛法然后在判断是会T的. 我们这样来想,筛法求素数的原理是什么: /**vis数组标记为0则说明是素数*/ int vis[10005]; void getPrimevis(int n) { int m=sqrt(n+0.5);

POJ2689 Prime Distance

题意:输入[l,r]代表,问区间里面最近和最远的两个素数(0<l<r<int_max, r-l<100000) 题解:素数筛,一个数是合数,那么它存在小于sqrt(n)的素因子,找到所有小于sqrt(int_max)的素数,注意数据:1 2 #include <iostream> #include <string.h> #include <stdio.h> #define N 1001000 #define ll long long using