一个数 $n$ 必有一个不超过 $\sqrt n$ 的质因子。

打表处理出 $1$ 到 $\sqrt n$ 的质因子后去筛掉属于 $L$ 到 $R$ 区间的素数即可。

Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int Range=50000;
const int N=1000000+233;
int f[N],vis[Range+233],prime[Range];
int cnt;
void get_prime(){
for(int i=2;i<=Range;++i){
if(!vis[i])prime[++cnt]=i;
for(int j=1;j<=cnt&&prime[j]*i<=Range;++j){
vis[prime[j]*i]=1;
if(i%prime[j]==0)break;
}
}
}
int main(){
get_prime();
int L,U;
while(scanf("%d%d",&L,&U)!=EOF){
memset(f,0,sizeof(f));
if(L==1)L=2;
for(int i=1;i<=cnt;++i){
int a=L%prime[i]==0?L/prime[i]:L/prime[i]+1;
int b=U/prime[i];
for(int j=a;j<=b;++j)if(j>1)f[j*prime[i]-L]=1;
}
int p=-1,x1,x2,maxans=-1,minans=N,y1,y2;
for(int i=0;i<=U-L;++i)
if(f[i]==0){
if(p==-1){p=i;continue;};
if(maxans<i-p){maxans=i-p,x1=p+L,x2=i+L;}
if(minans>i-p){minans=i-p,y1=p+L,y2=i+L;}
p=i;
}
if(maxans==-1)
cout<<"There are no adjacent primes."<<endl;
else cout<<y1<<","<<y2<<" are closest, "<<x1<<","<<x2<<" are most distant."<<endl;
}
return 0;
}

  

Prime Distance POJ - 2689 线性筛的更多相关文章

  1. Prime Distance POJ - 2689 (数学 素数)

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

  2. Sum of Consecutive Prime Numbers POJ - 2739 线性欧拉筛(线性欧拉筛证明)

    题意:给一个数 可以写出多少种  连续素数的合 思路:直接线性筛 筛素数 暴力找就行   (素数到n/2就可以停下了,优化一个常数) 其中:线性筛的证明参考:https://blog.csdn.net ...

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

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

  4. 题解报告:poj 2689 Prime Distance(区间素数筛)

    Description The branch of mathematics called number theory is about properties of numbers. One of th ...

  5. Prime Distance(二次筛素数)

    Description The branch of mathematics called number theory is about properties of numbers. One of th ...

  6. poj 2689 Prime Distance(大区间素数)

    题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...

  7. poj 2689 Prime Distance (素数二次筛法)

    2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...

  8. POJ 3126 - Prime Path - [线性筛+BFS]

    题目链接:http://poj.org/problem?id=3126 题意: 给定两个四位素数 $a,b$,要求把 $a$ 变换到 $b$.变换的过程每次只能改动一个数,要保证每次变换出来的数都是一 ...

  9. 数论 - 素数的运用 --- poj 2689 : Prime Distance

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12512   Accepted: 3340 D ...

随机推荐

  1. python的jieba分词

    # 官方例程 # encoding=utf-8 import jieba seg_list = jieba.cut("我来到北京清华大学", cut_all=True) print ...

  2. vue中使用base64进行加解密

    vue进行Base64加解密 背景 项目中需要对特殊字符进行处理,避免json和数据库的特殊字符(""等)冲突,刚好学了信息安全,干脆整个加解密,wkk.. 使用步骤 打开dos, ...

  3. LINUX 中 VSFTPD安裝

    VSFTPD 简写:vsftpd是very secure FTP daemon 的缩写,是一个完全免费的,开源代码的ftp服务器软件 特点:vsftpd是一款在LINUX发行版中最受推崇的FTP服务器 ...

  4. FreeBSD的VIM基本配置

    实现的基本功能 中文解决乱码问题 鼠标右键原样粘贴 最后一次编辑文件后,鼠标位置的记忆 if v:lang =~ "utf8$" || v:lang =~ "UTF-8$ ...

  5. Linux中的gpio口使用方法

    Linux中的IO使用方法 应该是新版本内核才有的方法.请参考:./Documentation/gpio.txt文件 提供的API:驱动需要包含 #include <linux/gpio.h&g ...

  6. 2019-03-28 SQL Server char/nchar/nvarchar

    带n 的是用于处理unicode 字符,即处理中英文的字符 一般来说,如果含有中文字符,用nchar/nvarchar,如果纯英文和数字,用char/varchar char 定长 char(10), ...

  7. GOF23设计模式之建造者模式

    GOF23设计模式之建造者模式 场景: 我们要建造一个复杂的产品.比如:神州飞船,Iphone.这个复杂的产品的创建.有这样的一个问题需要处理: 装配这些子组件是不是有个步骤问题? 实际开发中,我们所 ...

  8. anaconda jupyter

    本文主要讲解在Ubuntu系统中,如何在Anaconda下安装TensorFlow以及配置Jupyter Notebook远程访问的过程. 在官方文档中提到,TensorFlow的安装主要有以下五种形 ...

  9. (1)spring boot起步之Hello World【从零开始学Spring Boot】

    Spring Boot交流平台 1.1 介绍 自从structs2出现上次的漏洞以后,对spring的关注度开始越来越浓. 以前spring开发需要配置一大堆的xml,后台spring加入了annot ...

  10. 编译打包部署 Dubbo Admin

    1.下载,Dubbo地址: https://github.com/alibaba/dubbo/tree/2.5.x ,直接ZIP下载 2.解压并打开项目,mvn package 得到war包,如下图: ...