[题解](区间质数筛)POJ_2689 Prime Distance
区间筛素数:先筛出1~sqrt(R)的素数,然后对于每个询问只要用这些素数筛掉区间内的合数即可。
几个细节:
1.特判和1有关的一些情况
2.每次减去L偏移量,数组只开区间大小
3.POJ无法使用万能头文件(需要火星救援(大雾
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int prime[];
bool ck[],tmp[];
int l,r,tot;
int main(){
for(int i=;i<=;i++){
if(!ck[i])prime[++tot]=i;
for(int j=;j<tot;j++){
if(i*prime[j]>)break;
ck[i*prime[j]]=;
if(i%prime[j]==)break;
}
}
while(scanf("%d%d",&l,&r)!=EOF){
if(l==)l=;//特判
memset(tmp,,sizeof(tmp));
for(int i=;i<=tot;i++){
int a=(l-)/prime[i]+;
int b=r/prime[i];
a=max(,a);
for(int j=a;j<=b;j++)
tmp[j*prime[i]-l]=;//减去l映射
}
int lst=-,minans=0x7fffffff,maxans=,xa,ya,xb,yb;
for(int i=;i<=r-l;i++){
if(!tmp[i]){
if(lst==-){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==)printf("There are no adjacent primes.\n");
else printf("%d,%d are closest, %d,%d are most distant.\n",xb,yb,xa,ya);
}
}
[题解](区间质数筛)POJ_2689 Prime Distance的更多相关文章
- 题解报告:poj 2689 Prime Distance(区间素数筛)
Description The branch of mathematics called number theory is about properties of numbers. One of th ...
- POJ-2689 Prime Distance (两重筛素数,区间平移)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13961 Accepted: 3725 D ...
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- 【题解】UVA10140 [Prime Distance]
[题解]UVA10140 Prime Distance 哈哈哈哈\(miller-rabbin\)水过去了哈哈哈 还能怎么办呢?\(miller-rabbin\)直接搞.枚举即可,还跑得飞快. 当然此 ...
- POJ-2689 Prime Distance,区间素数筛法
Prime Distance 只会埃氏筛法的弱鸡今天读了读挑战程序设计120页,明白了求小区间内素数的方 ...
- [POJ268] Prime Distance(素数筛)
/* * 二次筛素数 * POJ268----Prime Distance(数论,素数筛) */ #include<cstdio> #include<vector> using ...
- 洛谷 P1865 A % B Problem[筛素数/前缀和思想/区间质数个数]
题目描述 区间质数个数 输入输出格式 输入格式: 一行两个整数 询问次数n,范围m 接下来n行,每行两个整数 l,r 表示区间 输出格式: 对于每次询问输出个数 t,如l或r∉[1,m]输出 Cros ...
- poj 2689 Prime Distance(区间筛选素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9944 Accepted: 2677 De ...
- ZOJ 1842 Prime Distance(素数筛选法2次使用)
Prime Distance Time Limit: 2 Seconds Memory Limit: 65536 KB The branch of mathematics called nu ...
随机推荐
- STM32 ~ 如何从ST网站找到对应的固件库
ST官方网站改版后,基本上很难搜索到固件库的地址,找了半天才找到固件库的下载地址,通过此方法可以找到其他需要的资源,故记下来方便大家. 下载的网站地址为: Home>Tools and Soft ...
- linux vsftpd
FTP是CS架构的.使用的是ftp协议. root@ubuntu:/# apt install vsftpd root@ubuntu:/# service vsftpd status ● vsftpd ...
- HDU5950 Recursive sequence —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others) ...
- MySQL丨分页查询
直奔主题 一.sql语句:select * from table limit startPageNum,everyPageNum 1)语句解析: table:你要查询的表 startPageNum:从 ...
- SSD Network Architecture--keras version
这里的网络架构和论文中插图中的网络架构是相一致的.对了,忘了说了,这里使用的keras版本是1.2.2,等源码读完之后,我自己改一个2.0.6版本上传到github上面.可别直接粘贴复制,里面有些中文 ...
- Python的GIL是什么鬼,多线程性能究竟如何
前言:博主在刚接触Python的时候时常听到GIL这个词,并且发现这个词经常和Python无法高效的实现多线程划上等号.本着不光要知其然,还要知其所以然的研究态度,博主搜集了各方面的资料,花了一周内几 ...
- opencv 知识点笔记
1.Opencv中cvScalar是CV_RGB的宏,cvScalar的储存顺序是B-G-R,而CV_RGB的储存顺序是R-G-B
- 算法实现c语言--02
从键盘上输入字符,将小写字母转换成大写字母.输入“ctl + z” 结束 . #include<stdio.h> #include<stdlib.h> //从键盘上输入字符,将 ...
- bzoj 1127 [POI2008]KUP——思路(悬线法)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1127 大于2*K的视为不能选的“坏点”.有单个格子满足的就直接输出. 剩下的都是<K的 ...
- Word2013 在一个页面双列显示
1. 效果图 2. 实现方法 (1) 进入页面布局 (2) 选中要整理的字,选中Columns,然后选择Two