#include <string.h>
#include <stdio.h>
const int maxn = 1000006;
bool vis[1000006];
int pr[1000005];
int cnt = 1; int bs(int l, int r, int v)
{
int mid=(l+r)>>1;
while(l < r)
{
if(pr[mid] < v)
l = mid+1;
else
r = mid;
mid= (l+r) >> 1;
}
return l;
} void getpr()
{
int i,j;
for(i=2;i*i<maxn;i++) if(!vis[i])
{
pr[cnt++]=i;
for(j=i*i;j<maxn;j+=i) vis[j]=1;
}
for(;i<maxn;i++)if(!vis[i])
{
pr[cnt++]=i;
}
} int main()
{
memset(vis, 0, sizeof(vis));
getpr();
int n, t;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
int ans1 = 0;
int ans2 = 0;
for (int i = 1; i <= cnt; i++)
{
if (pr[i] <= n)
continue;
int x = bs(1, cnt-1, pr[i]-n);
if (pr[i] - n == pr[x])
{
ans1 = pr[i];
ans2 = pr[x];
break;
}
}
if (ans1)
printf("%d %d\n",ans1, ans2);
else
puts("FAIL");
}
return 0;
}

hdoj 4715 Difference Between Primes 素数筛选+二分查找的更多相关文章

  1. hdu 4715 Difference Between Primes

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Description All you kn ...

  2. hduoj 4715 Difference Between Primes 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Time Limit: 2000/1000 MS (J ...

  3. HDU 4715 Difference Between Primes (打表)

    Difference Between Primes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...

  4. hdu 4715 Difference Between Primes (打表 枚举)

    Difference Between Primes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  5. UVa 10539 (筛素数、二分查找) Almost Prime Numbers

    题意: 求正整数L和U之间有多少个整数x满足形如x=pk 这种形式,其中p为素数,k>1 分析: 首先筛出1e6内的素数,枚举每个素数求出1e12内所有满足条件的数,然后排序. 对于L和U,二分 ...

  6. hdoj 2141 Can you find it?【二分查找+暴力】

    Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others ...

  7. hdu 4715 Difference Between Primes(素数筛选+树状数组哈希剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=4715 [code]: #include <iostream> #include <cstdio ...

  8. hdu 4715 Difference Between Primes 2013年ICPC热身赛A题 素数水题

    题意:给出一个偶数(不论正负),求出两个素数a,b,能够满足 a-b=x,素数在1e6以内. 只要用筛选法打出素数表,枚举查询下就行了. 我用set储存素数,然后遍历set里面的元素,查询+x后是否还 ...

  9. 二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix

    题目传送门 /* 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 */ #include <cstdio> #include < ...

随机推荐

  1. Spark学习之路(二)—— Spark开发环境搭建

    一.安装Spark 1.1 下载并解压 官方下载地址:http://spark.apache.org/downloads.html ,选择Spark版本和对应的Hadoop版本后再下载: 解压安装包: ...

  2. Azkaban学习之路(四)—— Azkaban Flow 2.0的使用

    一.Flow 2.0 简介 1.1 Flow 2.0 的产生 Azkaban 目前同时支持 Flow 1.0 和 Flow2.0 ,但是官方文档上更推荐使用Flow 2.0,因为Flow 1.0会在将 ...

  3. form表单提交不刷新页面的方法

    方法1:给form表单加onsubmit事件,值为return false; <form action="" method="post"  onsubmi ...

  4. 【设计模式】行为型03观察者模式(Observer Pattern)

    记得16年初第一次学习了23种设计模式,但是除了少数几个简单的外,其他的很多都是学了个似懂非懂,以至于有人问起甚至说不上来,现在想想,其实就是没看懂而已.例如观察者模式,其实原理很简单,但是当时并没有 ...

  5. Node.js Windows Example

    Firstly, download the msi file from https://nodejs.org/en/ Second, click the msi file to install nod ...

  6. MxNet 模型转Tensorflow pb模型

    用mmdnn实现模型转换 参考链接:https://www.twblogs.net/a/5ca4cadbbd9eee5b1a0713af 安装mmdnn pip install mmdnn 准备好mx ...

  7. kafka源码分析(一)server启动分析

    1 启动入口Kafka.scala Kafka的启动入口是Kafka.scala的main()函数: def main(args: Array[String]): Unit = { try { //通 ...

  8. Tensorflow教程(2)Tensorflow的常用函数介绍

    1.tf.constant tf.constant用来定义一个常量,所谓常量,广义上讲就是“不变化的量”.我们先看下官方api是如何对constant函数来定义的: tf.constant( valu ...

  9. 使用gets函数常见问题

    C语言面试经常会考如下一道题,哪里有错误: #include <stdio.h>    int main()  {     char string[100] = {'\0'};       ...

  10. Q&A-Ray-20180710

    Q: 如果集群多个客户端订阅会不会重复接收消息? A: 集群环境用,有另外一个参数. NodeManager类没有在框架里面: public interface INodeManager : IGra ...