Difference Between Primes

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 528    Accepted Submission(s): 150
Problem Description
All you know Goldbach conjecture.That is to say, Every even integer greater than 2 can be expressed as the sum of two primes. Today, skywind present a new conjecture: every even integer can be expressed as the difference of two primes. To validate this conjecture, you are asked to write a program.
 
Input
The first line of input is a number nidentified the count of test cases(n<10^5). There is a even number xat the next nlines. The absolute value of xis not greater than 10^6.

 
Output
For each number xtested, outputstwo primes aand bat one line separatedwith one space where a-b=x. If more than one group can meet it, output the minimum group. If no primes can satisfy it, output 'FAIL'.

 
Sample Input
3
6
10
20
 
Sample Output
11 5
13 3
23 3
 
Source


思路:
打个素数表,然后枚举b,判断a就够了。
ps:汗,开始以为xat为正数,一直WA,原来题目说的是绝对值。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define maxn 10000005
using namespace std; int n,m,cxx,ans;
bool vis[maxn];
int prime[664580]; void sieve(int nn)
{
int i,j,mm;
mm=int(sqrt(nn+0.5));
memset(vis,0,sizeof(vis));
for(i=2;i<=mm;i++)
{
if(!vis[i])
{
for(j=i*i;j<=nn;j+=i)
{
vis[j]=1;
}
}
}
}
int get_prime(int nn)
{
int i,c=0;
sieve(nn);
for(i=2;i<=nn;i++)
{
if(!vis[i]) prime[c++]=i;
}
return c;
}
bool isprime(int x)
{
int i,j,t;
t=sqrt(x+0.5);
for(i=2;i<=t;i++)
{
if(x%i==0) return false ;
}
return true ;
}
int main()
{
int i,j,t,a,b,flag,sgn;
cxx=get_prime(10000000); // 664579
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
flag=0;
if(n==0)
{
printf("2 2\n");
continue ;
}
if(n>0) sgn=1;
else sgn=0,n=-n;
for(i=0;i<cxx;i++)
{
b=prime[i];
a=b+n;
if(isprime(a))
{
flag=1;
if(sgn) printf("%d %d\n",a,b);
else printf("%d %d\n",b,a);
break ;
}
}
if(!flag) printf("FAIL\n");
}
return 0;
}



 

hdu 4715 Difference Between Primes (打表 枚举)的更多相关文章

  1. HDU 4715 Difference Between Primes (打表)

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

  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(素数筛选+树状数组哈希剪枝)

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

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

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

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

  6. hdoj 4715 Difference Between Primes 素数筛选+二分查找

    #include <string.h> #include <stdio.h> const int maxn = 1000006; bool vis[1000006]; int ...

  7. HDU 4715:Difference Between Primes

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

  8. HDU 4548 美素数(打表)

    HDU  4548  美素数(打表)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88159#problem/H 题目 ...

  9. HDU 5487 Difference of Languages(BFS)

    HDU 5487 Difference of Languages 这题从昨天下午2点开始做,到现在才AC了.感觉就是好多题都能想出来,就是写完后debug很长时间,才能AC,是不熟练的原因吗?但愿孰能 ...

随机推荐

  1. Sublime 脚本 配置 (lua 和 JavaScript篇)

    { "cmd" :["C:/Lua/Lua.exe","$file"], "file_regex" :"^(? ...

  2. Windows API获取系统配置文件的配置参数

    在Windows平台下获取系统配置文件(如:System.ini)的配置参数. 系统配置文件System.ini的内容如下: [SYSTEM] ServiceIP = 10.128.11.99:600 ...

  3. Ganglia 权威指南-安装Ganglia过程

    转自于:http://blog.csdn.net/xxd851116/article/details/21527055 http://www.dataguru.cn/article-3816-1.ht ...

  4. day8 - isinstance--issubclass-异常处理-自定义异常

    一.isinstance(obj, cls) 检查是否obj是否是类 cls 的对象 __author__ = 'Administrator' class Foo(object): pass obj ...

  5. MUI 列表页面绑定接口数据

    1.我们先看效果 2.功能分析 主要分为3步 数据初始化 下拉刷新 上拉加载 接口数据示例: { "List": [ { "Id": 9, "Orde ...

  6. cocos2d-x环境的搭建之xcode-本人亲历成功搭建!

    cocos2d-x环境的搭建之xcode-本人亲历成功搭建! 写给大家的前言,在学习cocos2d-x的时候自己走了很多的弯路,也遇到了很多很多问题,不管是简单的还是困难的现在都慢慢的一步一步克服了, ...

  7. javascript笔记整理(对象遍历封装继承)

    一.javascript对象遍历 1.javascript属性访问 对象.属性名 对象['属性名'] var obj={name:'chen',red:function(){ alert(1);ret ...

  8. javascript笔记整理(变量作用域)

    变量的作用域(作用域:指的就是一段代码的作用范围) 一.变量的作用域 1.全局变量(在代码中任何地方都能够访问得到的变量,拥有全局的作用域) A.最外层函数外面定义的变量 var a=1; funct ...

  9. linux配置ssh+rsync

    ssh  远程登录 sftp    文件共享 类似ftp  ssh  secure file transfer client scp    文件共享 类似cp   ssh配置文件 /etc/ssh/s ...

  10. 在Windows上使用CodeLite+MinGW+Clang进行开发

    前几天听说clang 3.4已经release了,然后我又手痒就折腾一下,在这里记录一下折腾的经过. 在以前就试过clang-cl+VC的开发环境,编译代码到是没发现什么大问题,有不少警告而已,不过c ...