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; ...
随机推荐
- JAVA的作用
JAVA的用途广泛:1 .银行系统,政企信息系统,支付系统,大数据平台,网站平台. 2 .Saas云,手机app,云管理系统后台,电商系统后台,桌面工具. JAVA和PYTHON的抉择:JAVA是C语 ...
- 20203412马畅若《Python程序设计》实验四Python综合实践报告
作为初次接触程序设计的我在看到云班课中用python进行游戏编程后感到很有意思,所以我决定这次做一个最经典的小鸟管道游戏.虽然网上许多大佬都说这是最基础的一项游戏编码,但我还是用了许多时间去做成这个游 ...
- 新装Eclipse运行Java程序报错Exception in thread "main" java.lang.UnsupportedClassVersionError
错误现象: Exception in thread "main" java.lang.UnsupportedClassVersionError: views/LoginFram ...
- idea主题插件 ->Atom Material File Icons
Atom Material File Icons 插件名
- 剑指 Offer II 回溯法
086. 分割回文子字符串 用substr枚举 因为是连续的 不是放与不放的问题 class Solution { public: vector<vector<string>> ...
- SwiftUI笔记
@Published 标记对象定义的属性变更可以被监听,当对应字段变化时会触发对象 objectWillChange 通知,订阅了该属性的View也会收到值改变的通知 /// A type that ...
- main(调用一个公共组件)
app.vue <template> <div> <Student/> <School></School> </div> < ...
- 读后笔记 -- Python 全栈测试开发 Chapter11:Python + Requests 实现接口测试
11.1 Requests 框架 11.1.1 requests 请求 1. reqeusts 库 安装 pip install requests 2. requests 库 GET 方法,参数通过 ...
- macOS NSView改变frame后会出现黑色残留,应付的办法是不在drawRect上修改重新initWithFrame一下就行
黑色部分就是残留.是因为绘制后保留了轨迹. 解决办法是不在drawRect中做处理重新写NSView,新增方法 initWithFrame - (void)drawRect:(NSRect)dirty ...
- 一 MySQL的架构与历史1.1--1.4
1.1 MySQL逻辑架构 最上层的服务并不是 MySQL 所独有的,大多数基于网络的客户端/服务器的工具或者服务都有类似的架构.比如连接处理.授权认证.安全等等. 第二层架构是MySQL比较有意思的 ...