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 ...
随机推荐
- MTK Android Camera新增差值
一. 计算需要的插值 如果原有的插值列表没有我们需要的插值的时候,要通过计算算出符合需求的插值,比如2700W的插值. 具体计算方法如下: 假设像素的长宽分别为X,Y,则插值为XY.由于MTK规定各参 ...
- word多级列表应用
- HAproxy 基础配置
基础配置详解 HAProxy 的配置文件haproxy.cfg由两大部分组成,分别是global和proxies部分 global:全局配置段 进程及安全配置相关的参数性能调整相关参数Debug参数 ...
- Codeup 25609 Problem I 习题5-10 分数序列求和
题目描述 有如下分数序列 2/1,3/2,5/3,8/5,13/8,21/13 - 求出次数列的前20项之和. 请将结果的数据类型定义为double类型. 输入 无 输出 小数点后保留6位小数,末尾输 ...
- 从CentOS 7.0升级到7.7版本
我平时都在VirtualBox上装虚拟机,方法是导入ova镜像文件,比如导入从网上下载的CentOS7-base.ova,该ova为CentOS 7.0版本,而现在最新版本为7.7,为此进入系统后第一 ...
- 小波变换在matlab中的使用
对信号进行一层分解 clc; clear; % 获取噪声信号 load('matlab.mat'); sig = M(1,1:1400); SignalLength = length(sig); %使 ...
- 用Python爬取大众点评数据,推荐火锅店里最受欢迎的食品
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:有趣的Python PS:如有需要Python学习资料的小伙伴可以加点 ...
- Unity 游戏框架搭建 2019 (三十二、三十三) 类的命名 & 代码文件命名
昨天我们完成了第八个示例的第二个 MenuItem 菜单顺序的调整. 我们今天再往下接着调整. 我们来看下接下来的 MenuItem 代码如下: [MenuItem("QFramework/ ...
- C++头文件问题
自己定义的头文件必须要用“***.h”系统头文件必须要用<***.h>stdafx.h 必须放在所有头文件的最前面(如果不放,debug版本没有问题:release版本有问题,会报错)
- layui table渲染和数据处理
最近在用layui开发管理系统,果然是"累"ui 实现功能:将之前选择的选项勾选,渲染备注信息(原数据为空的列) <table class="layui-hide& ...