[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,直接筛 ...
随机推荐
- windows系统-phpstudy升级mysql8.0.12安装教程及修改密码和安装注意事项
1.下载安装包,下载地址:mysql8.0.12 .如果你想要下载其它版本可以选择:mysql历史版本地址. 2.下载好,删除phpstudy的mysql目录.如果数据重要的,注意备份数据!同意把m ...
- Unity 为队伍设置不同颜色的shader
在魔兽争霸等一些游戏中,我们通过模型的颜色就能很轻松的区分队伍,如下: 实现的方法有很多,比如: 1,为不同队伍各出一张不同颜色的贴图(Hmmm,war3有的地图可以容纳12只队伍,美术大大们会很 ...
- JavaScript基础知识(数据类型)
数据类型 布尔:true/fasle console.log(typeof true);// "boolean" Number : true -->1 false --> ...
- HTML、CSS知识点,面试开发都会需要--No.6 设置背景
No.6 设置背景 1.background (1)如何设置背景:背景可通过color.image.gradient渐变或者组合方法设置. (2)background-color:颜色格式可以是十六进 ...
- Web前端开发推荐书籍
Web前端开发推荐书籍 前言 学校里没有前端的课程,那如何学习JavaScript,又如何使自己成为一个合格的前端工程师呢? 读 书吧~相对于在网上学习,在项目中学习和跟着有经验的同事学习,书中有着相 ...
- adjacent_diffenerce
版本1: template <class InputIterator,class OutputIterator> OutputIterator adjacent_diffenerce(In ...
- ZOJ 4062 - Plants vs. Zombies - [二分+贪心][2018 ACM-ICPC Asia Qingdao Regional Problem E]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4062 题意: 现在在一条 $x$ 轴上玩植物大战僵尸,有 $n$ ...
- HDFS,MapReduce,Hive,Hbase 等之间的关系
HDFS: HDFS是GFS的一种实现,他的完整名字是分布式文件系统,类似于FAT32,NTFS,是一种文件格式,是底层的. Hive与Hbase的数据一般都存储在HDFS上.Hadoop HDFS为 ...
- 3、jeecg 笔记之 模糊查询
1.前言 jeecg 考虑到默认模糊查询的话,会增加系统压力,导致查询慢,本来系统就挺那啥的... 2.方式一之实体赋值 实体重新赋值查询,用 * %% * 实现,我们知道 sql 中通常使用 % 去 ...
- docker+httpd的安装
docker pull docker.io/httpd //直接这么运行会报“没有conf/httpd.cong这个文件” docker run -d --name httpd2. -p : -p : ...