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. Source Insight使用技巧

    1. source insight必设: option-->key assignments-->    Edit: Delete Line ---- Assign New Key: Ctr ...

  2. js获取网页屏幕可见区域高度

    document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.docume ...

  3. [Windows Phone]常用类库&API推荐

    原文 [Windows Phone]常用类库&API推荐 简介: 把自己的应用程序搭建在稳定的API之上,这会使得我们在开发时能把精力都集中在程序的业务逻辑之上,避免重复造轮子,并且使得程序结 ...

  4. 在JS中,一个自定义函数如何调用另一个自定义函数中的变量

    function aa1511() { var chengshi="马鞍山"; var shengfen="安徽省"; return shengfen+&quo ...

  5. Android ListView条目全选功能,不用checkbox实现!

    大家好,翻了翻曾经的笔记,发现了一个我特别标记的功能,那就是ListView全选功能,顿时想起了我那个时候苦逼的生涯,因为我大学机械出身,大学毕业了都不知道什么叫代码,在58干了一段销售.实在是干不下 ...

  6. graph driver-device mapper-04libdevmapper基本操作

    // 创建thin pool // 调用路径:NewDeviceSet->initDevmapper->createPool 1.1 func createPool(poolName st ...

  7. POJ - 1006 Biorhythms 周期相遇 两个思路程序

    Description Some people believe that there are three cycles in a person's life that start the day he ...

  8. C#超级有用的一种类型—匿名类型

    顾名思义 匿名类型就是没有名字的类型.当一个新的匿名对象定义与前面已经存在的类型定义的内部变量类型同样时,编译器就会仅仅生成一个类定义,而不是各一个. 匿名类型对象中仍然能够再包括匿名对象. 在C#3 ...

  9. 时间戳timestamp

    1 时间戳 数据库中自动生成的 唯一的 二进制的数据,通常用作给数据表的行添加版本戳的机制. timestamp与时间和日期无关. timestamp存储大小为8字节. 一个数据表只能有一个times ...

  10. 三篇IMO的文章

    http://column.iresearch.cn/b/201411/687499.shtml?from=singlemessage&isappinstalled=1 http://colu ...