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. Sublime安装与配置

    1.到官网下载Sublime https://www.sublimetext.com/ 2.安装时选择添加到右键菜单 Add to explorer context menu 3.安装插件 菜单 to ...

  2. python基础知识13-迭代器与生成器,导入模块

    异常处理作业讲解 file = open('/home/pyvip/aaa.txt','w+') try: my_dict = {'name':'adb'} file.write(my_dict['a ...

  3. windows中Python多版本与jupyter notebook中使用虚拟环境

    本人电脑是windows系统,装了Python3.7版本,但目前tensorflow支持最新的python版本为3.6,遂想再安装Python3.6以跑tensorflow. 因为看极客时间的专栏提到 ...

  4. LeetCode(111) Minimum Depth of Binary Tree

    题目 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the s ...

  5. bat 获取命令执行后的多个返回值,并取最后一个

    最近在使用bat,遇到了这样的问题,获取adb shell cat命令之后的所有返回值,查了很长时间,才找到,现分享给大家. 举例如下: @for /f "tokens=*" %% ...

  6. 洛谷P3961 图的遍历

    题目来源 做这道题的方法不少. 在这里我只提一种 就是大法师. 可以采用反向建边,从最大的点开始dfs 我们考虑每次从所剩点中最大的一个点出发,我们暂且称它为i,而凡是i这个点所能到达的点,可以到达的 ...

  7. BeautifulSoup4系列四

    前言 很多时候我们无法直接定位到某个元素,我们可以先定位它的父元素,通过父元素来找子元素就比较容易 一.子节点 1.以博客园首页的摘要为例:<div class="c_b_p_desc ...

  8. WPF之DataAnnotations 注解说明

    参考:https://www.cnblogs.com/yaosuc/p/4527886.html 1.基础验证: using System.ComponentModel.DataAnnotations ...

  9. 九度oj 题目1367:二叉搜索树的后序遍历序列

    题目描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 输入: 每个测试案例包括2行: 第一行为1个整数 ...

  10. 封装自己DB

    DB.class.php <?php /** * Created by PhpStorm. * User: brady.wang * Date: 2017/11/10 * Time: 18:00 ...