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. Linux下修改efi启动项

    Linux下有一个efibootmgr工具可以编辑efi启动项,十分方便,简单介绍如下 直接运行efibootmgr会显示出当前所有efi启动项,每个启动项前都有相应编号, 可以使用efibootmg ...

  2. Pytest系列(17)- pytest-xdist分布式测试的原理和流程

    pytest-xdist分布式测试的原理 前言 xdist的分布式类似于一主多从的结构,master机负责下发命令,控制slave机:slave机根据master机的命令执行特定测试任务 在xdist ...

  3. tf.nn.relu 激活函数

    tf.nn.relu(features, name = None) 计算校正线性:max(features, 0) 参数: features:一个Tensor.必须是下列类型之一:float32,fl ...

  4. Jquery+php鼠标滚动到页面底部自动加载更多内容,使用分页

    1.index.php <style type="text/css"> #container{margin:10px auto;width: 660px; border ...

  5. EndNote自动修改导入文献标题

    今天使用EndNote导入文献,插入到word中时发现是标题的名字,导入的文献格式不标准,在百度后终于找到了解决办法,EndNote的强大可见一斑. 这个是我要导入的PDF文件的名字 导入后的名字 设 ...

  6. L15卷积神经网络基础

    卷积神经网络基础 本节我们介绍卷积神经网络的基础概念,主要是卷积层和池化层,并解释填充.步幅.输入通道和输出通道的含义. 二维卷积层 本节介绍的是最常见的二维卷积层,常用于处理图像数据. 二维互相关运 ...

  7. python与excel的关系;铁打的python流水的excel

    现在很多行业,都离不开用Excel: 做财务的,要用Excel做报表:做物流的,会用Excel来跟踪订单情况:做HR的,会用Excel算工资:做分析的,会用Excel计算数据做报表.不知道你有没有这样 ...

  8. vue2.x学习笔记(四)

    接着前面的内容:https://www.cnblogs.com/yanggb/p/12563162.html. 模板语法 vue使用了基于html的模板语法,允许开发者声明式地将dom绑定到底层vue ...

  9. linux CVE-2019-14287 Sudo提权漏洞

    CVE-2019-14287 sudo介绍 sudo,也就是以超级管理员身份运行(superuser do)的意思.sudo 是 Linux 中最常使用的重要实用程序之一,它功能十分强大,几乎安装在每 ...

  10. python数据结构之栈的构建

    class Stack(object): def __init__(self): self.stack=[] def pop(self): return self.stack.pop() def pu ...