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. CC EAL认证

    国际通用准则(CC) CC(Common Criteria)是国际标准化组织统一现有多种准则的结果,是目前最全面的评价准则.1996年6月,CC第一版发布:1998年5月,CC第二版发布:1999年 ...

  2. Android第三方应用分享图文到微信朋友圈 & 微信回调通知分享状态

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWQAAAKUCAIAAAC8A9XzAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWX ...

  3. JavaScript实现定点圆周运动

    目是这样的:假设有一定点(400px,300px),通过JavaScript使一个直径20px的圆点以 r=180px 为半径围绕该点做匀速圆周运动. 这个问题的整体实现思想应该是这样的,看到“半径” ...

  4. C语言的本质(8)——副作用与顺序点

    C 语言中,术语副作用是指对数据对象或者文件的修改.例如以下语句 var = 99; 的副作用是把 var 的值修改成 99.对表达式求值也可能产生副作用,例如: se = 100 对这个表达式求值所 ...

  5. Magento WebServices SOAP API 创建和使用

    首先 SOAP 简介: http://baike.baidu.com/view/1695890.htm?fromtitle=SOAP 然后简单介绍下Magento API.Magento API干啥用 ...

  6. Cannot retrieve metalink for repository: epel.

    Error: Cannot retrieve metalink for repository: epel. Please verify its path and                     ...

  7. Android UI SurfaceView的使用-绘制组合图型,并使其移动

    绘制容器类: //图形绘制容器 public class Contanier { private List<Contanier> list; private float x=0,y=0; ...

  8. CSS滤镜让图片模糊(毛玻璃效果)实例页面

    <pre name="code" class="css">CSS代码: .blur { filter: url(blur.svg#blur); /* ...

  9. linux学习之(五)-linux文解压、压缩、安装

    查看一个文件的类型 命令:  file  文件名 创建一个.tar类型的压缩包使用命令:tar -cvf    [文件名].tar  目录   例:tar -cvf   a.tar    yasuo/ ...

  10. rem布局

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...