POJ--2689-C++
#include<cstring>
#include<utility>
#include<cmath>
#define N 1000000
#define M 50000
using namespace std;
int primes[M];
bool judge[M];
bool p[N];
int b[N];
int cnt;
void getPrimes(int n);
int main()
{
int L,U;
while(cin>>L>>U)
{
getPrimes(sqrt(U));
memset(p,true,sizeof p);
if(L==1)
p[0]=false;
for(int i=0;i<cnt;i++)
for(int j=ceil(L/primes[i]);j<=floor(U/primes[i]);j++)
if(j!=1)
p[primes[i]*j-L]=false;
pair<int,int> p1,p2;
int cnt2=0;
for(int i=0;i<U-L+1;i++)
if(p[i])
b[cnt2++]=(L+i);
cout<<"There are no adjacent primes."<<endl;
else
{
int max_nu=0,min_nu=10000;
for(int i=0;i<cnt2-1;i++)
{
if(b[i+1]-b[i]>max_nu)
{
max_nu=b[i+1]-b[i];
p1.first=b[i];
p1.second=b[i+1];
}
if(b[i+1]-b[i]<min_nu)
{
min_nu=b[i+1]-b[i];
p2.first=b[i];
p2.second=b[i+1];
}
}
cout<<p2.first<<","<<p2.second<<" are closest, ";
cout<< p1.first<<","<<p1.second<<" are most distant."<<endl;
}
}
return 0;
}
void getPrimes(int n)//用欧拉筛法筛出2到根号n里面的素数
{
memset(judge,false,sizeof judge);
cnt=0;
for(int i=2;i<=n;i++)
{
if(!judge[i])
primes[cnt++]=i;
for(int j=0;j<cnt&&i*primes[j]<=n;j++)
{
judge[i*primes[j]]=true;
if(i%primes[j]==0)
break;
}
}
POJ--2689-C++的更多相关文章
- 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 (素数+两次筛选)
题目地址:http://poj.org/problem?id=2689 题意:给你一个不超过1000000的区间L-R,要你求出区间内相邻素数差的最大最小值,输出相邻素数. AC代码: #includ ...
- POJ 2689 Prime Distance(素数筛选)
题目链接:http://poj.org/problem?id=2689 题意:给出一个区间[L, R],找出区间内相连的,距离最近和距离最远的两个素数对.其中(1<=L<R<=2,1 ...
- POJ 2689 - Prime Distance - [埃筛]
题目链接:http://poj.org/problem?id=2689 Time Limit: 1000MS Memory Limit: 65536K Description The branch o ...
- 【POJ 2689】 Prime Distance
[题目链接] http://poj.org/problem?id=2689 [算法] 我们知道,一个在区间[l,r]中的合数的最小质因子必然不超过sqrt(r) 那么,先暴力筛出1-50000中的质数 ...
- poj 2689 Prime Distance(大区间筛素数)
http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...
- 数学#素数筛法 HDU 4548&POJ 2689
找素数本来是很简单的问题,但当数据变大时,用朴素思想来找素数想必是会超时的,所以用素数筛法. 素数筛法 打表伪代码(用prime数组保存区间内的所有素数): void isPrime() vis[]数 ...
- poj 2689 Prime Distance (素数二次筛法)
2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...
- 素数筛 poj 2689
素数筛 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; ...
随机推荐
- Ubuntu20.04 LTS更新源
Ubuntu 20.04 LTS Focal Fossa于2020年04月23日发布. 备份sudo cp /etc/apt/sources.list /etc/apt/sources.list.ba ...
- 【BOOK】数据存储--MongoDB
MongoDB存储 1.链接MongoDB 指定数据库 指定集合 import pymongo ## 连接数据库 client = pymongo.MongoClient( ...
- TP开发项目时遇到的问题记录
1.下载功能. TP自带Http下载类,使用时new一个就行,示例代码: public function download(){ //接收公文id $id = I('get.fid'); //根据公文 ...
- 16.SharedPreferences存储
1.SharedPreferences存储 不同于文件的存储方式,SharedPreferences是使用键值对的方式来存储数据的,保存为.xml文件. 也就是说当保存一条数据的时候,需要给这条数据提 ...
- XMind8破解,详细版(亲测)
废话不多说,直接上料: 一.资源 百度网盘XMind8: 链接:https://pan.baidu.com/s/1h2DXkni4-POEpHyBqLxjwA 提取码:kqgy 破解补丁: 链接:ht ...
- go 的internal 目录
Go 语言中的软件包推荐按照:组织名/项目名 的形式安排软件包的文件目录结构,一般「项目名」文件目录下还会按照功能.抽象约定.具体实现等维度再划分一些子目录.在 Go 语言里包的导入路径不同则被判定为 ...
- VS2019使用Qt4.8.7
取消系统变量中的Qt_INCLUDEPATH_. C:\Users\octob\AppData\Local\QtMsBuild中添加qt4.natvis.xml,qt4.natvis for visu ...
- HTML中javascript的<script>标签使用方法详解
只要一提到把JavaScript放到网页中,就不得不涉及Web的核心语言--HTML.在当初开发JavaScript的时候,Netscape要解决的一个重要问题就是如何做到让JavaScript既能与 ...
- ROS-安装与开发速记
参考教程: B站视频(感谢赵老师):https://www.bilibili.com/video/BV1Ci4y1L7ZZ?p=14&spm_id_from=pageDriver 课程文件:h ...
- 14-K8S之helm入门到逃跑
目录 helm入门 1.helm介绍 2.helm核心术语 3.helm下载和安装 3.1以helm3.6为测试实例 3.2以helm3.7.2为例 helm v2版本在集群上部署Tiller 1.创 ...