Difference Between Primes

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 860 Accepted Submission(s): 278

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
 

思路 : 打表

总结: 不要忘了有负数的情况,

使用 Scanner sc = new Scanner(new BufferedInputStream(System.in)); 和

System.out.println(); 程序执行时间如下图

使用: BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); 和

PrintWriter pw=new PrintWriter(new OutputStreamWriter(System.out),true); 程序如下图

import java.io.*;
import java.util.*;
public class Main {
int max=(int)Math.pow(10, 6)+10;
boolean a[]=new boolean[max];
public static void main(String[] args) throws IOException{
new Main().work();
}
void work() throws IOException{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw=new PrintWriter(new OutputStreamWriter(System.out),true);
isPrime();
int n=Integer.parseInt(bu.readLine());
while(n--!=0){
int x=Integer.parseInt(bu.readLine());
int m=x>0?x:Math.abs(x);
boolean boo=true;
int i=2;
for(;i<max;i++){
if(a[i+m]&&a[i]){
boo=false;
break;
}
}
if(!boo){
if(x>0)
pw.println((i+m)+" "+i);
else
pw.println(i+" "+(i+m));
}
else
pw.println("FAIL");
}
}
//素数表
void isPrime(){
Arrays.fill(a,true);
for(int i=2;i<max;i++){
if(a[i]){
for(int j=2*i;j<max;j+=i){
a[j]=false;
}
}
}
}
}

HDU 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. hdu 4715 Difference Between Primes (打表 枚举)

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

  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. 修改默认的undo_retention参数设置

    昨天,一个朋友的数据库数据被误操作删除掉了,请求我帮忙进行恢复. 数据库版本是Oracle10g Release 2的,我首先想到的是使用Flashback Query进行闪回恢复,不幸的是ORA-0 ...

  2. WampServer下如何实现多域名配置

    原文:WampServer下如何实现多域名配置 之前在学习跨域的时候,我写过一篇叫做WampServer下使用多端口访问的文章,默认的 localhost 采用的是 80 端口,能使用多端口访问的核心 ...

  3. jdk7和8的一些新特性介绍

    jdk7和8的一些新特性介绍 本文是我学习了解了jdk7和jdk8的一些新特性的一些资料,有兴趣的大家可以浏览下下面的内容. 官方文档:http://www.oracle.com/technetwor ...

  4. getopt vs getopts

    getopt示例 #!/bin/bash aflag=no args=`getopt a: $@` ]; then echo 'Usage: ...' exit fi set -- $args ] d ...

  5. 短路与&&和按位与&的区别

    条件1&&条件2,短路与&&如果条件1为假则不判断条件2:而按位与&如果条件1为假仍旧判断条件2

  6. Android UI ActionBar功能-自定义 Action Bar 样式

    ActionBar的样式官方提供了三种: Theme.Holo Theme.Holo.Light Theme.Holo.Light.DarkActionBar 但不仅仅是这三种,我们还可以自己定义Ac ...

  7. oc 根据文件路径获取文件大小

    第一种封装: -(NSInteger)getSizeOfFilePath:(NSString *)filePath{ /** 定义记录大小 */ NSInteger totalSize = ; /** ...

  8. 查询最小未使用ID的SQL查询

    --每个都加一,以此来找出最小的未用ID SELECT Min(T1.ID)+1 FROM dbo.TestTable T1 -- 不用查询已经存在的ID WHERE (T1.ID+1) NOT IN ...

  9. asp.net 超链接 下载TEXT文件,而不是直接在IE中打开

    问题描述:后台生成了文本文件,用超链接提供给用户下载.点击超链接,下载Excel文件没问题,但文本文件会直接被打开,而不是弹出下载窗口. 解决方法:把HyperLink改为LinkButton,在Cl ...

  10. OCP-1Z0-053-V12.02-512题 【转】

    http://blog.csdn.net/gisinfo/article/details/8159875 1.Which two statements correctly describe the r ...