POJ 3518 Prime Gap(素数)

id=3518">http://poj.org/problem?

id=3518

题意:

给你一个数。假设该数是素数就输出0. 否则输出比这个数大的素数与比这个数小的素数的差值。

分析:

明显本题先要用筛选法求出130W(严格的话应该是求第100001个素数)以内的全部素数。

然后推断给的数是否是素数就可以。

假设不是素数。那么就找出它在素数素组内的上界和下界,输出两个素数的差值就可以。

筛选法求素数可见:

http://blog.csdn.net/u013480600/article/details/41120083

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1300000; int prime[maxn+5];
int get_prime()
{
memset(prime,0,sizeof(prime));
for(int i=2;i<=maxn;i++)
{
if(!prime[i]) prime[++prime[0]]=i;
for(int j=1;j<=prime[0]&&prime[j]<=maxn/i;j++)
{
prime[prime[j]*i]=1;
if(i%prime[j]==0)break;
}
}
return prime[0];
} int main()
{
//生成maxn内的全部素数
get_prime(); int x;
while(scanf("%d",&x)==1 && x)
{
int bound=lower_bound(prime+1,prime+prime[0]+1,x)-prime;
if(prime[bound]==x) printf("0\n");
else printf("%d\n",prime[bound]-prime[bound-1]);
}
return 0;
}

POJ 3518 Prime Gap(素数)的更多相关文章

  1. POJ 3518 Prime Gap(素数题)

    [题意简述]:输入一个数,假设这个数是素数就输出0,假设不是素数就输出离它近期的两个素数的差值,叫做Prime Gap. [分析]:这题过得非常险.由于我是打的素数表. 由于最大的素数是1299709 ...

  2. poj 3518 Prime Gap

    Prime Gap Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7392   Accepted: 4291 Descrip ...

  3. POJ 3581 Prime Gap(二分)

    Prime Gap Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11009 Accepted: 6298 Descriptio ...

  4. POJ 3126 Prime Path 素数筛,bfs

    题目: http://poj.org/problem?id=3126 困得不行了,没想到敲完一遍直接就A了,16ms,debug环节都没进行.人品啊. #include <stdio.h> ...

  5. POJ - 3126 Prime Path 素数筛选+BFS

    Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Security s ...

  6. 【UVA - 1644 / POJ - 3518】Prime Gap(水题)

    Prime Gap 这里直接写中文了 Descriptions: 对于一个数n,若n为素数则输出0,否则找到距离n最小的两个素数,一个大于n,一个小于n,输出他们的差(正数) Input 多组输入 每 ...

  7. poj 2689 Prime Distance(大区间素数)

    题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...

  8. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

  9. POJ 2739 Sum of Consecutive Prime Numbers(素数)

    POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...

随机推荐

  1. 前端,字体图标,盒子显隐,2d形变,盒子阴影

    ---恢复内容开始--- 字体图标 1.将font-awesome-4.7.0文件夹放入项目内 2.在html head中连接 3.在body中导入 盒子显隐 1.使用高度显隐 <p>-- ...

  2. C++基本数据类型占字节数

    32位编译器 char :1个字节char*(即指针变量): 4个字节(32位的寻址空间是2^32, 即32个bit,也就是4个字节.同理64位编译器)short int : 2个字节int:  4个 ...

  3. cf 1029 C

    C. Maximal Intersection time limit per test 3 seconds memory limit per test 256 megabytes input stan ...

  4. PAT Basic 1050

    1050 螺旋矩阵 本题要求将给定的 N 个正整数按非递增的顺序,填入“螺旋矩阵”.所谓“螺旋矩阵”,是指从左上角第 1 个格子开始,按顺时针螺旋方向填充.要求矩阵的规模为 m 行 n 列,满足条件: ...

  5. Memcached特性及优缺点

    为了加快文件访问速度且提供多个使用者.需要在内存中建立内存缓存数据的管理减小读写磁盘的次数及保证数据的更新.因为需要使用cache缓存.   1.Memcached 主要特性 a.数据仅存在于内存中, ...

  6. skkyk:线段树浅谈

    推荐前辈学姐博客文章,写的很细 https://www.cnblogs.com/TheRoadToTheGold/p/6254255.html 学学半,此随笔主要是加深自己对线段树的理解 题目:洛谷P ...

  7. iOS 引入支付宝 缺少 #include <openssl/asn1.h>

    出现情况如上图: 解决方法如下:在你的Xcode里的header search paths 里添加支付宝SDK(openssl的路径):格式如下  $(PROJECT_DIR)/文件夹名        ...

  8. mysql无法启动,报错 Can't start server: can't create PID file: No space left on device

    然后看mysql日志文件 出现Can't start server: can't create PID file: No space left on device 这个错误. 提示磁盘空间不足 后用d ...

  9. WordPress添加前台注册功能

    一.添加注册表单 1.首先在当前主题的目录下新建一个php文件,命名为reg-page.php,然后将page.php中的所有代码复制到reg-page.php中: 2.删除reg-page.php开 ...

  10. [android 应用开发]android 分层

    1 应用层, 2 应用框架层(框架是所有开发人员共同使用和遵守的约定) 3 系统运行库层 4 linux内核层