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. 利用ant的javac任务来编译程序使用ant的java任务来运行程序

    <?xml version="1.0" encoding="UTF-8"?> <project name="javaTest&quo ...

  2. C-KMP

    一.BF算法 --传统算法 BF算法是普通的模式匹配算法,BF算法的思想就是将目标串S的第一个字符与模式串P的第一个字符进行匹配,若相等,则继续比较S的第二个字符和P的第二个字符:若不相等,则比较S的 ...

  3. WebStorm 7.0 注册码

    User Name: EMBRACE License Key:===== LICENSE BEGIN =====24718-1204201000001h6wzKLpfo3gmjJ8xoTPw5mQvY ...

  4. ios内存管理2-对象之间的内存管理

    同之前一样,新建一个基于命令行的工程,在新建一个Student类和一个Book类 编写如下代码: Student.h // // Student.h // 内存管理2-对象之间的内存管理 // // ...

  5. c:foreach如何嵌套循环,求指教,求优化

    java类: public class PopedomItem { private String id; private String pid; private String name; privat ...

  6. encode_utf8 把字符编码成字节 微信例子

    ##µ¼Èë encode_json decode_json use JSON qw/encode_json decode_json/; print "1111111111111111-\$ ...

  7. 基于visual Studio2013解决面试题之1002公共子串

     题目

  8. 最简单的基于FFMPEG+SDL的视频播放器 ver2 (採用SDL2.0)

    ===================================================== 最简单的基于FFmpeg的视频播放器系列文章列表: 100行代码实现最简单的基于FFMPEG ...

  9. C/C++:多个.cpp文件包括同一个.h头文件定义方法

    本文解决multiple definition of `XX'的错误.[出于反爬虫的目的,你不是在http://blog.csdn.net/zhanh1218上看到的,肯定不是最新最全的.] 关于头文 ...

  10. [置顶] 让导入的Android项目,运行起来的方法。

    Eclipse里面直接import的代码,不能运行出现如下错误: [2013-12-12 12:58:55 - Dex Loader] Unable to execute dex: java.nio. ...