[POJ268] Prime Distance(素数筛)
/*
* 二次筛素数
* POJ268————Prime Distance(数论,素数筛)
*/
#include<cstdio>
#include<vector>
using namespace std;
const int maxn = 1000005;
typedef long long LL;
bool is_prime_small[maxn];
bool is_prime[maxn];
vector <int> res;
int main() {
LL l,u;
while(scanf("%lld%lld",&l,&u) != EOF) {
for(LL i = 0;i*i < u;i++) {
is_prime_small[i] = true;
}
for(LL i = 0;i <= u-l;i++) {
is_prime[i] = true;
}
is_prime_small[1] = false;
if(l == 1)
is_prime[0] = false;
for(LL i = 2;i*i <= u;i++) {
if(is_prime_small[i]) {
for(LL j = 2*i;j*j <= u;j+=i) {
is_prime_small[j] = false;
}
for(LL j = max(2LL,(l+i-1)/i) * i;j <= u;j+=i) {
is_prime[j-l] = false;
}
}
}
res.clear();
for(LL i = l;i <= u;i++) {
if(is_prime[i - l]) {
res.push_back(i);
}
}
if(res.size() <= 1) {
printf("There are no adjacent primes.\n");
continue;
}
//避免这样写,可能会导致某些变量没有被赋值
// LL maxx = res[1] - res[0],b1,b2;
// LL minn = res[1] - res[0],a1,a2;
LL b1 = res[0],b2 = res[1],maxx = b2 - b1;
LL a1 = res[0],a2 = res[1],minn = a2 - a1;
for(LL i = 2;i < res.size();i++) {
if(maxx < res[i] - res[i-1]) {
maxx = res[i] - res[i - 1];
b2 = res[i];
b1 = res[i-1];
} else if(minn > res[i] - res[i-1]) {
minn = res[i] - res[i-1];
a2 = res[i];
a1 = res[i-1];
}
}
printf("%lld,%lld are closest, %lld,%lld are most distant.\n",a1,a2,b1,b2);
}
return 0;
}
[POJ268] Prime Distance(素数筛)的更多相关文章
- Prime Path素数筛与BFS动态规划
埃拉托斯特尼筛法(sieve of Eratosthenes ) 是古希腊数学家埃拉托斯特尼发明的计算素数的方法.对于求解不大于n的所有素数,我们先找出sqrt(n)内的所有素数p1到pk,其中k = ...
- ZOJ 1842 Prime Distance(素数筛选法2次使用)
Prime Distance Time Limit: 2 Seconds Memory Limit: 65536 KB The branch of mathematics called nu ...
- POJ - 2689 Prime Distance (区间筛)
题意:求[L,R]中差值最小和最大的相邻素数(区间长度不超过1e6). 由于非素数$n$必然能被一个不超过$\sqrt n$的素数筛掉,因此首先筛出$[1,\sqrt R]$中的全部素数,然后用这些素 ...
- POJ 2689 - Prime Distance - [埃筛]
题目链接:http://poj.org/problem?id=2689 Time Limit: 1000MS Memory Limit: 65536K Description The branch o ...
- POJ2689 - Prime Distance(素数筛选)
题目大意 给定两个数L和U,要求你求出在区间[L, U] 内所有素数中,相邻两个素数差值最小的两个素数C1和C2以及相邻两个素数差值最大的两个素数D1和D2,并且L-U<1,000,000 题解 ...
- POJ 3126 Prime Path 素数筛,bfs
题目: http://poj.org/problem?id=3126 困得不行了,没想到敲完一遍直接就A了,16ms,debug环节都没进行.人品啊. #include <stdio.h> ...
- POJ-2689 Prime Distance (两重筛素数,区间平移)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13961 Accepted: 3725 D ...
- POJ2689:Prime Distance(大数区间素数筛)
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- poj 2689 Prime Distance(大区间筛素数)
http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...
随机推荐
- vue2中使用transition
最终效果为 div元素从右向左出现, 然后从左向右消失. transition标签包裹要移动的元素: css 样式: 其中: 1: 为div元素显示时的状态 2: 为div元素移动的过程 (进入 ...
- css之高度塌陷及其解决方法
浅谈 清除浮动 的多种方式(clearfix) 1.什么是浮动 ? 是否脱离文档流? 1.普通流定位 static(默认方式) 普通流定位,又称为文档流定位,是页面元素的默认定位方式 页面中的块级元素 ...
- laravel5.4将excel表格中的信息导入到数据库中
本功能是借助 Maatwebsite\Excel 这个扩展包完成的,此扩展包的安装过程请参考上篇博文:http://www.cnblogs.com/zhuchenglin/p/7122946.html ...
- python 链表表达式 map、filter易读版
链表推导式 [x for x in x] 链表推导式提供了一个创建链表的简单途径,无需使用 map(), filter() 以及 lambda.返回链表的定义通常要比创建这些链表更清晰.每一个链表推导 ...
- PAT甲级1068 Find More Coins【01背包】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976 题意: n个硬币,每一个有一个特有的价 ...
- 电子产品使用感受之———我用过的最昂贵的手机壳:otter box 和 Apple 原装清水壳的对比
2014年9月27日,我买到了我所使用的第一部 iPhone — iPhone 5C 蓝色.今天,2019年3月2日,我手里拿的是iPhoneXR 蓝色,两款手机如出一辙的设计和手感,让我充满了无限的 ...
- JS常见的字符串操作
1.charAt() 获取字符串指定位置的字符 用法:strObj是字符串对象,index是指定的位置,(位置从0开始数) strObj.charAt(index) 2. indexOf() 方 ...
- 程序员调 Bug 的样子,非常真实
程序员调 Bug 的样子,非常真实
- Python2与python3 文件操作关于打开文件
#首先在python3中操作文件只有一种选择,那就是open() #而在python2中则有两种方式:file()与open() 两者都能够打开文件,对文件进行操作,也具有相似的用法和参数,但是,这两 ...
- [js]作用域链查找规则获取值和设置值
作用域链查找规则获取值和设置值 <script> /** 1.作用域链查找规则 私有作用域出现的一个变量不是私有的,则往上一级作用域查找,上级作用域没有则继续向上级查找,一直找到windo ...