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 ...
随机推荐
- ssl & ike/ipsec
SSL/TLS
- 数据结构和算法(Golang实现)(2)简单入门Golang-包、变量和函数
包.变量和函数 一.举个例子 现在我们来建立一个完整的程序main.go: // Golang程序入口的包名必须为 main package main // import "golang&q ...
- AJ学IOS 之CoreLocation指南针小应用
AJ分享,必须精品 一:效果图示 简单的用到CoreLocation获取方位做的指南针小应用 二:制作思路 具体用到了CoreLocation相关的知识,请看上一篇博客有写 然后获取方向不需要进行授权 ...
- AJ学IOS(46)之网易彩票幸运大转盘
AJ分享,必须精品 效果 实现过程: 基础UI搭建 这里主要是用了xib搭建,首先我们分析,有中间的开始按钮,背景图片,还有星座按钮,这里能用xib做的事开始按钮和背景图片. 如图: 星座按钮的搭建: ...
- Ajax 简述与基础语法
目录 Ajax 1. 原生 JS 实现 Ajax 2. 使用 Ajax 实现异步通信 a. Ajax 的基础语法 b. 用 Ajax 传递数据 i. 传递字符串数据 ii. 传递 JSON 数据 3. ...
- 2020新Asp.NET敏捷快速开发框架7.0.5旗舰版源码asp.net mvc框架,工具类CRM,工作流
演示地址: http://frame3.diytassel.com 用户名:system 密码:0000 需要的联系QQ:22539134 一.新添加了 1.多语言功能: 2.代码生成器模版 ...
- 记录在腾讯云上搭建Ubuntu服务器
为了能让更多的比赛题复现,只好自己去手动搭建服务器 各种奇葩的操作以及很多的由于升级之后出现的问题变成了一个个坑. 写下这篇博客以此来记录我踩过的坑. 第一步 购买一个服务器,当然我购买的是学生版本的 ...
- 当td中文字过长时,显示为省略号
当表格中的文字过长时,可选择已省略号显示.这里是用js实现的.首先获取td中的文字长度(innerText.length),如果长度超过了设定的长度,则截取内容,加上省略号显示.示例代码如下: $(f ...
- Spring Cloud 系列之 Sleuth 链路追踪(一)
随着微服务架构的流行,服务按照不同的维度进行拆分,一次请求往往需要涉及到多个服务.互联网应用构建在不同的软件模块集上,这些软件模块,有可能是由不同的团队开发.可能使用不同的编程语言来实现.有可能布在了 ...
- JasperReports入门教程(四):多数据源
JasperReports入门教程(四):多数据源 背景 在报表使用中,一个页面需要打印多个表格,每个表格分别使用不同的数据源是很常见的一个需求.假如我们现在有一个需求如下:需要在一个报表同时打印所有 ...