sicily 1500. Prime Gap
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.
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.
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.
10
11
27
2
492170
0
4
0
6
0
114
分析:首先不可能输入每个数都去找一遍其周围的素数,我比较喜欢的是直接求出所需要的最大范围的素数,然后从这里面再进行下一步的计算。这里使用的是筛选法来得到素数集。
#include <iostream> using namespace std; int main(int argc, char const *argv[])
{
int prime[];
for (int i = ; i != ; ++i) {
prime[i] = ;
}
for (int i = ; i != ; ++i) {
if (prime[i] == ) {
for (int j = * i; j < ; j += i)
prime[j] = ;
}
} // 筛选法找素数 int num;
while (cin >> num && num != ) {
int length = ;
if (prime[num] != ) {
int upperBound = , lowerBound = ;
for (lowerBound = num - ; lowerBound >= ; --lowerBound) {
if (prime[lowerBound] == )
break;
}
for (upperBound = num + ; upperBound < ; ++upperBound) {
if (prime[upperBound] == )
break;
}
length = upperBound - lowerBound;
}
cout << length << endl;
}
return ;
}
sicily 1500. Prime Gap的更多相关文章
- 1500. Prime Gap 11 月 11日
/*本篇为转载,在此申明,具体就是先设定从2以后所有的数都为质数,定为质数的数的倍数则不是质数,慢慢排除后面的数*/ #include<iostream>#include<cstri ...
- 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 ...
- [暑假集训--数论]poj3518 Prime Gap
The sequence of n − 1 consecutive composite numbers (positive integers that are not prime and not eq ...
- POJ 3518 Prime Gap(素数)
POJ 3518 Prime Gap(素数) id=3518">http://poj.org/problem? id=3518 题意: 给你一个数.假设该数是素数就输出0. 否则输出比 ...
- 【UVA - 1644 / POJ - 3518】Prime Gap(水题)
Prime Gap 这里直接写中文了 Descriptions: 对于一个数n,若n为素数则输出0,否则找到距离n最小的两个素数,一个大于n,一个小于n,输出他们的差(正数) Input 多组输入 每 ...
- POJ 3581 Prime Gap(二分)
Prime Gap Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11009 Accepted: 6298 Descriptio ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
- UVa 1644 (筛素数 + 二分) Prime Gap
题意: 给出一个整数n,如果n是素数输出0,否则输出它后一个素数与前一个素数的差值. 分析: 首先用筛法把前十万个素数都筛出来,然后放到数组里.用二分找到不大于n的最大的素数的下标,如果这个素数等于n ...
随机推荐
- 【转】Latex编译报错后中断编译并改正,然后重复出现不明原因报错的解决方法
转自:https://www.douban.com/note/419828344/ 目录: 一.问题描述 二.测试情况(可以跳过,直接看建议) 三.建议 四.参考资料 正文: 问题描述: 错漏某个符号 ...
- Backdooring a OS VM
Backdooring a OS VM 来源 https://www.cnblogs.com/studyskill/p/6524672.html 提示: 1.经过实验,fortios 5.4 be ...
- 【转】VS2012简单的使用感受+插件推荐
转自:http://blog.sina.com.cn/s/blog_58c506600101b34t.html ~PS:后面有博主自己补充的VS2013的哦~ 1.Visual Studio Achi ...
- 【2018ICPC青岛】
B 题意:给n个问题,每个问题有一个固定的答案ai(<=10^5).现在有m个约束关系,每个约束关系是一个二元组(ui,vi),表示你回答ui.vi问题的答案必须一样. 现在让你输出分别修复一个 ...
- 【Cf #502 H】The Films(莫队)
题面的简述:总共有$m$种书,书架上共有$n$本书,给出$n$本书的种类,并有$Q$个询问,每次询问给出$l, r, k$.每次询问时都会先出现$k * m$本书,每种书各$k$本,然后再加入书架上的 ...
- 洛谷 P2473 [SCOI2008]奖励关 解题报告
P2473 [SCOI2008]奖励关 题目描述 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出\(k\)次宝物,每次你都可以选择吃或者不吃(必须在抛出下一个宝 ...
- python之旅:面向对象之多态、多态性
一 多态 多态指的是一类事物有多种形态 eg:动物有多种形态:猫,狗,猪 class Animal: #动物类 def eat(self): #吃 pass def drink(self): #喝 p ...
- Kubernetes Downward API
目录 说明 环境变量方式 将pod信息注入为环境变量 将容器资源信息注入为环境变量 volume挂载方式 作用 说明 我们知道,每个Pod在成功创建出来之后,都会被系统分配唯一的名字.IP地址,并且处 ...
- Hadoop基础-Hadoop快照管理
Hadoop基础-Hadoop快照管理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.快照的作用 快照可以迅速对文件(夹)进行备份,不产生新文件,使用差值存储,默认是禁用状态. ...
- ASP.NET项目与IE10、IE11不兼容的解决办法
1.解决办法 机器级别修复, 服务器所有ASP.NET程序受益 需要去微软下载对应asp.NET版本的修补程序 .NET 4 -http://support.microsoft.com/kb/2600 ...