前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >poj 2689 巧妙地运用素数筛选

poj 2689 巧妙地运用素数筛选

作者头像
全栈程序员站长
发布2022-07-06 10:17:51
2110
发布2022-07-06 10:17:51
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是全栈君。

称号:

给出一个区间[L,R]求在该区间内的素数最短,最长距离。 (R < 2 * 10^9 , R – L <= 10 ^ 6)

由数论知识可得一个数的因子可在开根号内得到。

所以,我们能够打出5*10^4内得素数。然后,在用一次筛法把在[L。R]内得合数找到,则剩下的就是素数了。这里要用到离散化。把一个数 x – L 保存在数组里。由于,直接保存肯定不行。可是我们发现区间特点较小。所以。能够想到离散化。

代码语言:javascript
复制
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

typedef long long LL;
const int MAXN = 50000;
int primes[MAXN];
bool vst[MAXN];
int notPrimes[1000010];
int pos[1000010];
int top,pcnt;

void init(){
    top = 0;
    memset(vst,0,sizeof(vst));
    vst[0] = vst[1] = 1;
    for(int i = 2;i < MAXN;++i)if(!vst[i]){
        primes[top++] = i;
        for(int j = i + i;j < MAXN;j += i) vst[j] = 1;
    }
    //printf("top: %d\n",top);
}

void solve(int L,int R){
    memset(notPrimes,0,sizeof(notPrimes));

    if(L == 1) L = 2;              /// 防止筛掉全部该区间的素数本身!!!!!
    for(int i = 0;i < top&&(LL)primes[i]*primes[i] <= R;++i){  //筛选因子
        int s = L / primes[i] + (L % primes[i] > 0); //当前素数的最小倍数达到L
       
        s = (s == 1 ? 2 : s);    /// 防止筛掉全部该区间的素数本身!!!!!
       
        for(int j = s;(LL)j*primes[i] <= R;++j){
            if((LL)j*primes[i] >= L)   //合数
               notPrimes[j*primes[i] - L] = 1;   // 相当与离散化
        }
    }

    pcnt = 0;
    for(int i = 0;i <= R - L;++i){
        if(!notPrimes[i]){
            pos[pcnt++] = i + L;
            //printf("i -- > %d\n",i + L);
        }
    }
    
    
    if(pcnt < 2){
        puts("There are no adjacent primes.");
    } else {
        int minl,minr,maxl,maxr,minv = 999999,maxv = -1;
        for(int i = 1;i < pcnt;++i){
            if(pos[i] - pos[i-1] > maxv){
                maxv = pos[i] - pos[i-1];
                maxl = pos[i-1];
                maxr = pos[i];
            }
            if(pos[i] - pos[i-1] < minv){
                minv = pos[i] - pos[i-1];
                minl = pos[i-1];
                minr = pos[i];
            }
        }
        printf("%d,%d are closest, %d,%d are most distant.\n",minl,minr,maxl,maxr);
    }
}
int main()
{
    init();
    int L,R;
    while(~scanf("%d%d",&L,&R)){
        solve(L,R);
    }
    return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117277.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月8,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档