POJ 3581 Prime Gap(二分)
| Prime Gap |
|---|
| Time Limit: 5000MS Memory Limit: 65536K |
| Total Submissions: 11009 Accepted: 6298 |
| Description |
| The sequence of n − 1 consecutive composite numbers (positive integers that are not prime and not equal to 1) lying between two successive prime numbers p and p + n is called a prime gap of length n. For example, ‹24, 25, 26, 27, 28› between 23 and 29 is a prime gap of length 6. |
| Your mission is to write a program to calculate, for a given positive integer k, the length of the prime gap that contains k. For convenience, the length is considered 0 in case no prime gap contains k. |
| Input |
| The input is a sequence of lines each of which contains a single positive integer. Each positive integer is greater than 1 and less than or equal to the 100000th prime number, which is 1299709. The end of the input is indicated by a line containing a single zero. |
| Output |
| The output should be composed of lines each of which contains a single non-negative integer. It is the length of the prime gap that contains the corresponding positive integer in the input if it is a composite number, or 0 otherwise. No other characters should occur in the output. |
| Sample Input |
| 10 11 27 2 4921700 |
| Sample Output |
| 4 0 6 0 114 |
| Source Japan 2007 |
二分,稍微需要处理一点细节。
#include<iostream>
#include<cstdio>
using namespace std;
const int MAXN=1e7-100;
int prime[MAXN];
bool pri[MAXN+10];
int sieve(int n)
{
int p=0;
for(int i=0; i<=n; i++)
pri[i]=true;
pri[0]=pri[1]=false;
for(int i=2; i<=n; i++)
{
if(pri[i])
{
prime[++p]=i;
for(int j=2*i;j<=n;j+=i)
pri[j]=false;
}
}
return p;
}
int main()
{
int m=sieve(1e7-100);
int t;
while(~scanf("%d",&t)&&t){
int up=m;
// cout<<prime[m];
int lo=0;
while(1){
int tem=(up+lo)/2;
if(prime[tem]>t) up=tem;
else if(prime[tem]==t){
up=lo=0;
break;
}
else lo=tem;
if(lo+1==up)break;
}
printf("%d\n",prime[up]-prime[lo]);
}
}
POJ 3581 Prime Gap(二分)的更多相关文章
- POJ 3518 Prime Gap(素数)
POJ 3518 Prime Gap(素数) id=3518">http://poj.org/problem? id=3518 题意: 给你一个数.假设该数是素数就输出0. 否则输出比 ...
- POJ 3518 Prime Gap(素数题)
[题意简述]:输入一个数,假设这个数是素数就输出0,假设不是素数就输出离它近期的两个素数的差值,叫做Prime Gap. [分析]:这题过得非常险.由于我是打的素数表. 由于最大的素数是1299709 ...
- poj 3518 Prime Gap
Prime Gap Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7392 Accepted: 4291 Descrip ...
- 【UVA - 1644 / POJ - 3518】Prime Gap(水题)
Prime Gap 这里直接写中文了 Descriptions: 对于一个数n,若n为素数则输出0,否则找到距离n最小的两个素数,一个大于n,一个小于n,输出他们的差(正数) Input 多组输入 每 ...
- [暑假集训--数论]poj3518 Prime Gap
The sequence of n − 1 consecutive composite numbers (positive integers that are not prime and not eq ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- [POJ 3581]Sequence
[POJ 3581]Sequence 标签: 后缀数组 题目链接 题意 给你一串序列\(A_i\),保证对于$ \forall i \in [2,n],都有A_1 >A_i$. 现在需要把这个序 ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
随机推荐
- wireshark抓包实战(一),抓包原理
一.什么样的"包"能被wireshark抓住呢? 1.本机 即直接抓取进出本机网卡的流量包.这种情况下,wireshark会绑定本机的一块网卡. 2.集线器 用于抓取流量泛洪,冲突 ...
- Linux基础管理篇,软件管理程序,yum与rpm
一.RPM 一般来说,rpm类型的文件在安装的时候,会检测当前的系统是否满足当前软件需要的环境.若符合,则该软件就会被安装,并且会把软件的相关信息写入/var/lib/rpm/目录下的数据库文件中. ...
- Flask 入门 (十一)
上篇文章讲的是一对多,这篇文章应该说多对多了 但是多对多无法用两张表来实现,因为外键......,你懂,哈哈哈!,所以中间需要加一张表来实现 承接上文,修改main.py中的代码如下: #encodi ...
- Deep Dream模型与实现
Deep Dream是谷歌公司在2015年公布的一项有趣的技术.在训练好的卷积神经网络中,只需要设定几个参数,就可以通过这项技术生成一张图像. 本文章的代码和图片都放在我的github上,想实现本文代 ...
- 写日志 使用nnlog
import nnlog log=nnlog.Logger(r"/学习/接口自动化/BestTest/10.26/book_server.log",backCount=5,leve ...
- SpringBoot系列(六)集成thymeleaf详解版
SpringBoot系列(六)集成thymeleaf详解版 1. thymeleaf简介 1. Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎. 2. Thymeleaf ...
- kworkerds 挖矿木马简单分析及清理
公司之前的开发和测试环境是在腾讯云上,部分服务器中过一次挖矿木马 kworkerds,本文为我当时分析和清理木马的记录,希望能对大家有所帮助. 现象 top 命令查看,显示 CPU 占用 100%,进 ...
- 理解SVG的缩放 偏移的计算公式
SVG中DOM元素的偏移与缩放都是基于SVG元素的左上角,所以如何理解与计算SVG中元素的真实位置就比较难,下面的例子都以圆(circle)为例. 1.缩放假定缩放的比例为s,执行缩放后,圆的圆心坐标 ...
- Jmeter发送jdbc请求进行大批量造数
创建批量造数脚本,一个简单的结构如下图所示, 1.线程组(10个线程重复运行2次,相当于造20个数) 2.用户定义变量(这是全局变量,用于后面随机筛选用) 3.数据库连接配置 4.计数器(用于主键递增 ...
- E. 蚂蚁和斐波那契
单点时限: 1.0 sec 内存限制: 512 MB 聪明的小蚂蚁最近学习了斐波那契数列,但是它想到了一个问题:从L到R之间斐波那契数列和的奇偶是什么呢?其中Fib[1]=1,Fib[2]=1 . 输 ...