poj-2689-素数区间筛
Description
Your program is given 2 numbers: L and U (1<=L< U<=2,147,483,647), and you are to find the two adjacent primes C1 and C2 (L<=C1< C2<=U) that are closest (i.e. C2-C1 is the minimum). If there are other pairs that are the same distance apart, use the first pair. You are also to find the two adjacent primes D1 and D2 (L<=D1< D2<=U) where D1 and D2 are as distant from each other as possible (again choosing the first pair if there is a tie).
Input
Output
Sample Input
2 17
14 17
Sample Output
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
#define LL long long
#define mp make_pair
#define pb push_back
#define inf 0x3f3f3f3f
int maxn=;
vector<int>prime;
vector<int>p;
bool is[];
void init(){
is[]=is[]=;
for(LL i=;i<=maxn;++i){
if(!is[i]) prime.push_back(i);
for(LL j=;j<prime.size()&&i*prime[j]<=maxn;j++){
is[i*prime[j]]=;
if(i%prime[j]) break;
}
}
}
void solve(LL L,LL R){
p.clear();
memset(is,,sizeof(is));
for(LL i=;i<prime.size()&&1LL*prime[i]*prime[i]<=R;i++){
LL s=L/prime[i]+(L%prime[i]>);
if(s==)s=;
for(LL j=s;j*prime[i]<=R;j++){
if(j*prime[i]>=L) is[j*prime[i]-L]=;
}
}
for(int i=;i<=R-L;i++){
if(!is[i]&&i+L>=) p.push_back(i+L);
}
}
int main(){
LL L,R;
init();
while(scanf("%lld%lld",&L,&R)!=EOF){
solve(L,R); if(p.size()<) puts("There are no adjacent primes.");
else{
int c1,c2,m1,m2;
c1=m1=p[];
c2=m2=p[];
for(int i=;i<p.size();++i){
if(p[i]-p[i-]<c2-c1){
c1=p[i-];
c2=p[i];
}
if(p[i]-p[i-]>m2-m1){
m1=p[i-];
m2=p[i];
}
}
printf("%d,%d are closest, %d,%d are most distant.\n",c1,c2,m1,m2);
}
}
return ;
}
poj-2689-素数区间筛的更多相关文章
- poj2689(素数区间筛法模板)
题目链接: http://poj.org/problem?id=2689 题意: 给出一个区间 [l, r] 求其中相邻的距离最近和最远的素数对 . 其中 1 <= l < r < ...
- poj 2689 Prime Distance(大区间筛素数)
http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- 大区间素数筛选(POJ 2689)
/* *POJ 2689 Prime Distance *给出一个区间[L,U],找出区间内容.相邻的距离最近的两个素数和距离最远的两个素数 *1<=L<U<=2147483647 ...
- POJ 2689.Prime Distance-区间筛素数
最近改自己的错误代码改到要上天,心累. 这是迄今为止写的最心累的博客. Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ - 2689 Prime Distance (区间筛)
题意:求[L,R]中差值最小和最大的相邻素数(区间长度不超过1e6). 由于非素数$n$必然能被一个不超过$\sqrt n$的素数筛掉,因此首先筛出$[1,\sqrt R]$中的全部素数,然后用这些素 ...
- 素数筛 poj 2689
素数筛 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; ...
- poj 2689 Prime Distance (素数二次筛法)
2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...
- lightoj1197 素数双筛,可以参考poj的那题双筛
/* 判断一个数是否是素数,只要判断这个数有没有在[2,sqrt(n)]区间的因子 同样,对于大数短区间的筛选,同样可以用这种判断方式, 先筛出sqrt(n)范围内的素数,然后用这些素数去筛出区间内的 ...
- POJ 2689 筛法求素数
DES:给出一个区间[L, U].找出这个区间内相邻的距离最近的两个素数和距离最远的两个素数.其中1<=L<U<=2147483647 区间长度不超过1000000. 思路:因为给出 ...
随机推荐
- 检测浏览器(BOM)以及地址栏网址的API
navigator.userAgent //检测浏览器的版本以及那个厂商的 (不怎么准,你比如360经常跟别人干架,所以别人检测到360浏览器就提示浏览器危险,所以360就自己修改了) //分解这个地 ...
- propsData 选项 全局扩展的数据传递
propsData 不是和属性有关,他用在全局扩展时进行传递数据,结合自定义属性获取属性值的props一起使用 html <div id="app"> <regi ...
- 以结算价交易TAS和以市价交易TAM
CME Group的合约规格中提到TAS和TAM交易,如:Gold Futures Contract Specs Gold 期货 合约规格 Trading at Settlement (TAS) is ...
- webpack插件配置(二)- HtmlWebpackPlugin
作用 简化Html文件的创建,以便为你的webpack bundle包提供服务.这对于在文件名中包含每次会随着编译而发生变化的hash的webpack bundle尤其有用.插件可以生成一个HTML文 ...
- 最受欢迎的前端框架 —— Bootstrap学习
Bootstrap是Twitter的Mark Otto和Jacob Thornton开发的,是目前最受欢迎的前端框架,它简单灵活,使得Web前端开发更加快捷方便. 首先,要基本掌握Bootstrap框 ...
- Python open 读写小栗子
1.样本内容 A.txt 2.上代码: f=open(r'E:\A.txt','r') boyA=[] boyB=[] count = for each_line in f: ]!='======': ...
- Abode Audition 的使用
讲一下音频的合并,音量放大,音频截取,音频删除等. 我下载的是Abode Audition 3.0的试用版本,可以免费使用30天. 1. 将抖音中小视频保存下来,成为mp4文件,然而Audition ...
- [osg]OSG使用更新回调来更改模型
使用回调类实现对场景图形节点的更新.本节将讲解如何使用回调来实现在每帧的更新遍历(update traversal)中进行节点的更新. 回调概览 用户可以使用回调来实现与场景 ...
- include
1. 自己写的文件都用:include "....." 2. 如果A类include了B,那么在主函数中,只用include A类,就可以使用B类了,但是此时不能再include ...
- 练习:将值是null的数据删除掉(剔除):com.fasterxml.jackson.annotation.JsonInclude;包
练习:将值是null的数据删除掉(剔除):com.fasterxml.jackson.annotation.JsonInclude;包 例如,有数据是null,不想展示 { "statusC ...