传送门

一道挺有趣的。

第一眼以为每个数都用miller_rabin判一次,但感觉会被卡时间啊。

继续分析发现可以晒出sqrt(r)中的所有素数,然后用类似埃式筛法的方法晒出[l,r]" role="presentation" style="position: relative;">[l,r][l,r]之间的素数。

然后就没了。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#define N 1000005
#define ll long long
using namespace std;
bool f[N];
ll pri[150005],vis[150005],tot=0;
inline void init(ll len){
    for(ll i=2;i<=len;++i){
        if(!vis[i])pri[++tot]=i;
        for(ll j=1;j<=tot;++j){
            ll k=pri[j]*i;
            if(k>len)break;
            vis[k]=1;
            if(i%pri[j]==0)break;
        }
    }
}
inline void check(ll l,ll r){
    memset(f,1,sizeof(f));
    if(l==1)f[0]=0;
    for(ll i=1;pri[i]*pri[i]<=r;++i){
        ll tmp=pri[i];
        for(ll j=(l/tmp)*tmp;j<=r;j+=tmp){
            if(j<l||j==tmp)continue;
            f[j-l]=0;
        }
    }
}
int main(){
    init(50000);
    ll l,r;
    while(~scanf("%lld%lld",&l,&r)){
        check(l,r);
        ll cnt=0,tmp,mx=0,mn=1e9,pos,mx1,mx2,mn1,mn2;
        for(ll i=0;i<=r-l;++i){
            if(f[i]){
                ++cnt;
                if(cnt>=2){
                    tmp=i-pos;
                    if(tmp>mx)mx=tmp,mx1=pos,mx2=i;
                    if(tmp<mn)mn=tmp,mn1=pos,mn2=i;
                }
                pos=i;
            }
        }
        if(cnt>=2)printf("%lld,%lld are closest, %lld,%lld are most distant.\n",mn1+l,mn2+l,mx1+l,mx2+l);
        else printf("There are no adjacent primes.\n");
    }
    return 0;
}

2018.09.01 poj2689 Prime Distance(埃式筛法)的更多相关文章

  1. POJ-2689 Prime Distance,区间素数筛法

                                                    Prime Distance 只会埃氏筛法的弱鸡今天读了读挑战程序设计120页,明白了求小区间内素数的方 ...

  2. poj2689 Prime Distance题解报告

    题目戳这里 [题目大意] 给定一个区间[L,R],求区间内的质数相邻两个距离最大和最小的. [思路分析] 其实很简单呀,很明显可以看出来是数论题,有关于质数的知识. 要注意一下的就是L和R的数据范围都 ...

  3. POJ2689 Prime Distance(数论:素数筛选模板)

    题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...

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

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13961   Accepted: 3725 D ...

  5. 解题报告:poj2689 Prime Distance

    2017-10-03 11:29:20 writer:pprp 来源:kuangbin模板 从已经筛选好的素数中筛选出规定区间的素数 /* *prime DIstance *给出一个区间[L,U],找 ...

  6. POJ 2689 - Prime Distance - [埃筛]

    题目链接:http://poj.org/problem?id=2689 Time Limit: 1000MS Memory Limit: 65536K Description The branch o ...

  7. POJ2689:Prime Distance(大数区间素数筛)

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

  8. POJ2689 - Prime Distance(素数筛选)

    题目大意 给定两个数L和U,要求你求出在区间[L, U] 内所有素数中,相邻两个素数差值最小的两个素数C1和C2以及相邻两个素数差值最大的两个素数D1和D2,并且L-U<1,000,000 题解 ...

  9. POJ2689 Prime Distance 质数筛选

    题目大意 求区间[L, R]中距离最大和最小的两对相邻质数.R<2^31, R-L<1e6. 总体思路 本题数据很大.求sqrt(R)的所有质数,用这些质数乘以j, j+1, j+2... ...

随机推荐

  1. 20.OGNL与ValueStack(VS)-普通方法访问

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 首先在User中增加一个成员方法,代码如下: public String g ...

  2. Algorithm-多目标优化-博文路径

    参考博文: 多目标进化算法(MOEA)概述: https://blog.csdn.net/qithon/article/details/72885053 多目标优化问题的算法及其求解: https:/ ...

  3. insert NULL into mysql

    https://stackoverflow.com/questions/36898130/python-how-to-insert-null-mysql-values You are insertin ...

  4. map模块使用方法

    map指令使用ngx_http_map_module模块提供的.默认情况下,nginx有加载这个模块,除非人为的 --without-http_map_module.ngx_http_map_modu ...

  5. jqeury datatable/http://www.cnblogs.com/jobs2/p/3431567.html

    0.http://blog.csdn.net/mickey_miki/article/details/8240477 1.1 修改默认值 代码  841处options 添加分页选择 oInit.bL ...

  6. -moz 火狐 -msIE -webkit[chrome safari]

    -moz代表firefox浏览器私有属性 -ms代表IE浏览器私有属性 -webkit代表chrome.safari私有属性

  7. Alpha Level (Significance Level)

    1.Alpha Level (Significance Level,显著水平): What is it? 显著性水平α是指当零假设是正确的,但做出了错误决策的概率(即一类错误的概率).Alpha水平( ...

  8. mysql连接数据库存报下面错误:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

    输入 mysql -u root 登录 mysql 的时候出现以下错误: ERROR 2002 (HY000): Can't connect to local MySQL server through ...

  9. 调用webservices报错 原因是没有导入commons-logging和commons-discovery

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/discovery/to ...

  10. Python3 tuple 函数

    Python3 tuple 函数  Python3 内置函数 描述 tuple 函数将列表转换为元组.. 语法 以下是 tuple 的语法: tuple( seq ) 参数 seq -- 要转换为元组 ...