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(二分)的更多相关文章

  1. POJ 3518 Prime Gap(素数)

    POJ 3518 Prime Gap(素数) id=3518">http://poj.org/problem? id=3518 题意: 给你一个数.假设该数是素数就输出0. 否则输出比 ...

  2. POJ 3518 Prime Gap(素数题)

    [题意简述]:输入一个数,假设这个数是素数就输出0,假设不是素数就输出离它近期的两个素数的差值,叫做Prime Gap. [分析]:这题过得非常险.由于我是打的素数表. 由于最大的素数是1299709 ...

  3. poj 3518 Prime Gap

    Prime Gap Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7392   Accepted: 4291 Descrip ...

  4. 【UVA - 1644 / POJ - 3518】Prime Gap(水题)

    Prime Gap 这里直接写中文了 Descriptions: 对于一个数n,若n为素数则输出0,否则找到距离n最小的两个素数,一个大于n,一个小于n,输出他们的差(正数) Input 多组输入 每 ...

  5. [暑假集训--数论]poj3518 Prime Gap

    The sequence of n − 1 consecutive composite numbers (positive integers that are not prime and not eq ...

  6. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  7. poj 2689 Prime Distance(大区间素数)

    题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...

  8. [POJ 3581]Sequence

    [POJ 3581]Sequence 标签: 后缀数组 题目链接 题意 给你一串序列\(A_i\),保证对于$ \forall i \in [2,n],都有A_1 >A_i$. 现在需要把这个序 ...

  9. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

随机推荐

  1. 面向对象核心技术(java)

    一.类的封装详解 在“面向对象编程基础(java)”的时候讲过,封装是面向对象编程的核心思想.同时我们也知道类是载体,只不过我们把对象的属性和行为封装在载体中. 现我们用封装的方式来实现,一个顾客去一 ...

  2. CentOS 配置OOM监控报警

    由于程序设计不合理或者瞬间高并发访问时,很有可能会触发OOM(Out of memory),这里指的是操作系统级别的OOM.具体什么是OOM,以及怎样发生这里不在赘述,因为笔者认为这是IT从业工作者的 ...

  3. 浅谈ArrayList

    浅谈ArrayList 废话不多说(事实是不会说),让我们直接进入正题 首先讲一讲最基本的ArrayList的初始化,也就是我们常说的构造函数,ArrayList给我们提供了三种构造方式,我们逐个来查 ...

  4. 王者荣耀英雄全皮肤4K高清大图,python爬虫帮你保存下来

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取t.cn ...

  5. stand up meeting 12/22/2015 && 用户体验收录

    part 组员                工作              工作耗时/h 明日计划 工作耗时/h    UI 冯晓云  完善页面切换,尝试子页面设计    4  完善页面切换和子页面 ...

  6. Jar包一键重启的Shell脚本及新服务器部署的一些经验

    原文首发于博客园,作者:后青春期的Keats:地址:https://www.cnblogs.com/keatsCoder/ 转载请注明,谢谢! 前言 最近公司为客户重新部署了一套新环境,由我来完成了基 ...

  7. PHP Curl 请求https 60错误解决办法

      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 

  8. RocketMQ存储机制与确认重传机制

    引子 消息队列之前就听说过,但一直没有学习和接触,直到最近的工作流引擎项目用到,需要了解学习一下.本文主要从一个初学者的角度针对RocketMQ的存储机制和确认重传机制做一个浅显的总结. 存储机制 我 ...

  9. #4018. 统计n! 尾部零

    题目出处: http://www.51cpc.com/problem/4018 题目描述 试统计正整数n的阶乘n!=1×2×3×…×n尾部连续零的个数. 输入格式 输入正整数n 输出格式 输出个数 样 ...

  10. 排序1 - 选择排序 & 插入排序

    请原谅我没有按照之前图片的分类来介绍排序算法,先说最简单的两种排序算法(冒泡略过),选择排序和插入排序,之前老是容易记混.默认输出升序的序列啊,哈哈. 选择排序 对于输入长度为n的数组,一共比较n-1 ...