Prime Distance POJ - 2689 (数学 素数)
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. 题意:找到l、r区间内,差值最大和最小的相邻质数。
思路:因为n很大,所以不可能直接找出所有的质数后遍历
对于区间1~n,我们只需要找出√n 范围内的素数,倍增标记剩余区间的合数,就可以得到区间的所有质数。
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std; const int maxn = 1e5;
int prime[maxn];
int tot;
void get_pri(int n)
{
bool v[n+];
memset(v,,sizeof(v));
for(int i=; i<=n; i++)
{
if(!v[i])
prime[++tot] = i;
for(int j=i; j<=n/i; j++)
{
v[j*i] = ;
}
}
}
int main()
{ int l,r;
while(~scanf("%d%d",&l,&r))
{
tot = ;
get_pri(sqrt(r));
bool vis[r-l+];
memset(vis,,sizeof(vis));
for(int i=; i<=tot; i++)
{
for(int j=ceil(l*1.0/prime[i]); j<=r/prime[i]; j++)
{
if(j == )continue;
vis[prime[i]*j-l] = ;
}
}
int ans[r-l+];
int cnt = ;
for(int i=; i<=r-l; i++)
{
if(!vis[i])
{
if(i+l == )continue;
ans[++cnt] = i+l;
}
}
if(cnt < )
printf("There are no adjacent primes.\n");
else
{
int minn = 0x3f3f3f3f;
int maxx = ;
int id1;
int id2;
for(int i=; i<cnt; i++)
{
int tmp = ans[i+]-ans[i];
if(tmp < minn)
{
minn = tmp;
id1 = i;
}
if(tmp > maxx)
{
maxx = tmp;
id2 = i;
}
}
printf("%d,%d are closest, %d,%d are most distant.\n",ans[id1],ans[id1+],ans[id2],ans[id2+]);
}
}
}
Prime Distance POJ - 2689 (数学 素数)的更多相关文章
- Prime Distance POJ - 2689 线性筛
一个数 $n$ 必有一个不超过 $\sqrt n$ 的质因子. 打表处理出 $1$ 到 $\sqrt n$ 的质因子后去筛掉属于 $L$ 到 $R$ 区间的素数即可. Code: #include&l ...
- [ACM] POJ 2689 Prime Distance (筛选范围大素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12811 Accepted: 3420 D ...
- POJ2689 Prime Distance(数论:素数筛选模板)
题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...
- poj 2689 Prime Distance(大区间筛素数)
http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...
- POJ2689:Prime Distance(大数区间素数筛)
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- poj 2689 区间素数筛
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- poj 2689 巧妙地运用素数筛选
称号: 给出一个区间[L,R]求在该区间内的素数最短,最长距离. (R < 2 * 10^9 , R - L <= 10 ^ 6) 由数论知识可得一个数的因子可在开根号内得到. 所以,我们 ...
- poj 2689 (素数二次筛选)
Sample Input 2 17 14 17 Sample Output 2,3 are closest, 7,11 are most distant. There are no adjacent ...
- [POJ268] Prime Distance(素数筛)
/* * 二次筛素数 * POJ268----Prime Distance(数论,素数筛) */ #include<cstdio> #include<vector> using ...
随机推荐
- 4.6 并发编程/IO模型
并发编程/IO模型 背景概念 IO模型概念 IO模型分类 阻塞IO (blocking IO) 特点: 两个阶段(等待数据和拷贝数据两个阶段)都被block 设置 server.setsockopt ...
- FreeNAS-9.10虚拟机测试安装
虚拟机安装NreeNAS-9.10步骤 需求:网络监控磁盘要扩容 测试环境: CPU 内存 系统盘 共享盘 网卡 2核 2G 20G 20G 桥接 系统版本:FreeNAS-9.10 一.配置虚拟机 ...
- <一>企业级开源仓库nexus实战应用–nexus的安装
1,Nexus 介绍. Nexus是什么? Nexus 是一个强大的maven仓库管理器,它极大地简化了本地内部仓库的维护和外部仓库的访问. 不仅如此,他还可以用来创建yum.pypi.npm.doc ...
- 第三节:SignalR之PersistentConnection模型详解(步骤、用法、分组、跨域、第三方调用)
一. 承上声明 在上一个章节里,啰里啰嗦写了一堆关于介绍SignalR的“废话”,从这一篇开始往后正式撸代码,这期间不少人(包括 张善友大哥)建议我直接用.Net Core下的SignalR,关于此简 ...
- 前端面试题整理—Vue篇
1.对vue的理解,有什么特点,vue为什么不能兼容IE8及以下浏览器 vue是一套用于构建用户界面的渐进式框架,核心是一个响应的数据绑定系统 vue是一款MVVM框架,基于双向绑定数据,当数据发生 ...
- 五十二、linux 编程——网络介绍
52.1 网络介绍 使用远程资源 共享信息.程序和数据 分布处理 52.1.1 协议的概念 计算机网络中实现通信必须有一些约定,如对速率.传输代码.代码结构.传输控制步骤和出错控制等约定,这些约定即被 ...
- 面试经验合集-Java后端<一>
面试一:CDKHXJSYJS 时间:2018-12-29 周六 地点:航天科技大厦32楼 一 技术题目 <回忆版> 1.上下转型 2.Java异常:分类.处理.设计 3.二叉排序树 ...
- UDP 单播、广播、多播
一.UDP广播 广播UDP与单播UDP的区别就是IP地址不同,广播使用广播地址255.255.255.255,将消息发送到在同一广播网络上的每个主机.值得强调的是:本地广播信息是不会被路由器转发.当然 ...
- OpenCV掩模mask的原理和作用
一.什么是掩模mask OpenCV中很多函数都带有一个mask参数,mask被称为掩模.图像掩模一般用来对处理的图像(全部或者局部)进行遮挡,来控制图像处理的区域或处理过程. 二.掩模原理 掩模一般 ...
- PHP -- 七牛云 在线视频 获取某一帧作为封面图
### 最近碰到视频处理,需要视频封面? 但用的是七牛云存储视频,索性搜了一下,怎么获取视频的某一帧作为视频的封面图... 发现了七牛官网又自身的接口 ### https://developer.qi ...