题意:给出一个偶数(不论正负),求出两个素数a,b,能够满足 a-b=x,素数在1e6以内。

只要用筛选法打出素数表,枚举查询下就行了。

我用set储存素数,然后遍历set里面的元素,查询+x后是否还是素数。

注意,偶数有可能是负数,其实负数就是将它正数时的结果颠倒就行了。

代码:

/*
* Author: illuz <iilluzen[at]gmail.com>
* Blog: http://blog.csdn.net/hcbbt
* File: 10.cpp
* Create Date: 2013-09-08 12:55:09
* Descripton: 10
*/ #include <cstdio>
#include <cmath>
#include <set>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++) set<int> m;
#define MAX_P 1000001
int nList[MAX_P] = {0};
void Calc()
{
int n,p,t,sq=(int)sqrt(MAX_P*2+1);
for (n=3;n<=sq;n+=2)
{
if (nList[n>>1]) continue;
for (t=n*n;t<=MAX_P<<1;t+=n<<1) //筛选循环
nList[t>>1] = 1;
}
m.insert(2);
for (n=t=1;t<MAX_P;++t)
{
if (nList[t]) continue;
m.insert((t<<1)+1);
if (++n==10)
n=0;
}
}
/****** TEMPLATE ENDS ******/ const int MAXN = 0; int main() {
Calc();
int t, n;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
int nn = abs(n);
bool flag = false;
for (set<int>::iterator i = m.begin(); i != m.end(); i++) {
if (m.count(*i + nn) != 0) {
flag = true;
if (n < 0) printf("%d %d\n", *i, *i + nn);
else printf("%d %d\n", *i + nn, *i);
break;
}
}
if (!flag) printf("FAIL\n");
}
return 0;
}

hdu 4715 Difference Between Primes 2013年ICPC热身赛A题 素数水题的更多相关文章

  1. 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 ...

  2. hdu 4715 Difference Between Primes

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

  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. hdu 4715 Difference Between Primes(素数筛选+树状数组哈希剪枝)

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

  6. hdu 4706 Children's Day 2013年ICPC热身赛A题 模拟

    题意:按字母顺序排列成n型,简单的模拟题. 当字母排到z时从a开始重新排起. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * Blog: ...

  7. hdu 4708 Rotation Lock Puzzle 2013年ICPC热身赛A题 旋转矩阵

    题意:给出一个n*n的矩阵,旋转每一圈数字,求出对角线可能的最大值,以及转到最大时的最小距离. 只要分析每一层就可以了,本来想用地址传递二维数组,发现行不通,改了一下就行了. 这里有个坑,比如: 1 ...

  8. HDU 4772 Zhuge Liang's Password (2013杭州1003题,水题)

    Zhuge Liang's Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  9. HDU 4758——Walk Through Squares——2013 ACM/ICPC Asia Regional Nanjing Online

    与其说这是一次重温AC自动机+dp,倒不如说这是个坑,而且把队友给深坑了. 这个题目都没A得出来,我只觉得我以前的AC自动机的题目都白刷了——深坑啊. 题目的意思是给你两个串,每个串只含有R或者D,要 ...

随机推荐

  1. swf上传文件

    以下是自学it网--中级班上课笔记 网址:www.zixue.it 引入的js包自己在网上找一个,这里好像不支持上传文件,反正我没找到,呵呵~~ html文件 <!DOCTYPE html PU ...

  2. linux cmd: linux下解压命令大全

    linux下解压命令大全 .tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)———————————— ...

  3. 【转】[Mysql] Linux Mysql 日志专题

    原文链接:http://blog.csdn.net/xiaoxu0123/article/details/6258538 1, 设置存放的目录: [root@Linux etc]# more /etc ...

  4. nginx 解决400 bad request 的方法

    nginx的400错误比较难查找原因,因为此错误并不是每次都会出现的,另外,出现错误的时候,通常在浏览器和日志里看不到任何有关提示. 经长时间观察和大量试验查明,此乃request header过大所 ...

  5. 演练5-2:Contoso大学校园管理2

    一.添加列标题排序功能 我们将增加Student/Index页面的功能,为列标题添加超链接,用户可以点击列标题对那一列进行排序. 1.修改Index方法 public ActionResult Ind ...

  6. Android dialog 问题

    1.dialog.dismiss和dialog.cancel的区别 Cancel the dialog. This is essentially the same as calling dismiss ...

  7. docker学习笔记3:镜像操作(查找和下载)

    一.查看本地镜像 只有下载后,镜像才会保存在本地(docker环境所在的主机),通过如下命令可以查看本地已经存在的镜像. 命令:dokcer images 上面命令列出本地所有已经存在的镜像,显示的信 ...

  8. 在 Windows Azure 网站上使用 Django、Python 和 MySQL:创建博客应用程序

    编辑人员注释:本文章由 Windows Azure 网站团队的项目经理 Sunitha Muthukrishna 撰写. 根据您编写的应用程序,Windows Azure 网站上的基本Python 堆 ...

  9. HDU 2896 病毒侵袭 AC自己主动机题解

    本题是在text里面查找key word的增强版.由于这里有多个text. 那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术.我直接使用个vis数组记录已经訪问过的节点, ...

  10. Mac下显示隐藏文件 以及修改 hosts文件内容

    修改hosts 文件内容: 进入etc 文件夹,找到hosts 文件,把该文件复制出来,修改完里面的内容后,先把etc中的hosts 文件删除,然后在把修改后的文件脱机去 可能需要管理员的密码,你输入 ...