poj3518 Prime Gap
来源:互联网 发布:jquery 动态加载js 编辑:程序博客网 时间:2024/06/06 18:18
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.
线性筛出所有质数,然后二分查找最接近的。
#include<cstdio>#include<cstring>const int maxn=1300000;bool have[maxn+10];int prm[101000],tot;int main(){ int i,j,k,m,n,p,q,x,y,z,l,r,mid; for (i=2;i<=maxn;i++) { if (!have[i]) prm[++tot]=i; for (j=1;j<=tot&&(long long)i*prm[j]<=maxn;j++) { have[i*prm[j]]=1; if (i%prm[j]==0) break; } } while (scanf("%d",&n)&&n) if (!have[n]) printf("0\n"); else { l=1; r=tot; while (l<r) { mid=(l+r+1)/2; if (prm[mid]>=n) r=mid-1; else l=mid; } printf("%d\n",prm[l+1]-prm[l]); }}
- poj3518 Prime Gap
- (数论2.1.4)POJ3518 Prime Gap(埃拉托斯特尼筛法)
- Prime Gap
- Prime Gap
- Prime Gap
- Prime Gap
- Prime Gap
- Prime Gap
- Prime Gap
- poj3518(Prime Grap 欧拉筛+二分)
- poj3518
- poj3518
- POJ3518
- d613: Prime Gap
- 1500 Prime Gap
- poj 3518 Prime Gap
- 1500. Prime Gap
- sicily--1500. Prime Gap
- 输入一个年月,输出往前推5个月的年月,考虑跨年
- 马小妹的Android之旅——Android四大基本组件之Activity
- 防止表单重复提交
- cocos2dx碰撞检测算法
- 设计模式学习笔记-适配器模式
- poj3518 Prime Gap
- MAC XAMPP搭建php开发环境 mysql 中文乱码
- Parameter '__frch_item_0' not found. Available parameters are [list]
- 抽象类与接口
- VS2010,Edit contrl控件,鼠标放到控件上时,无文字显示,离开时显示显示默认文字。
- ural 1325 Dirt(bfs最短路+优先队列)
- Android多线程
- PHP面向对象——访问修饰符介绍
- 字符串数组(String []) 去掉重复值的方法