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. js题

    function newStr(){ return "hi";}function turn(str){ str.toString = newStr;}var str1 = &quo ...

  2. structs常用的Action

    今天座右铭-----谦虚使人进步,骄傲使人落后. 除了基本的Action之外,structs还提供几个其他的类型Action,下面就简单的说一下: 1.DispatchAction:能同时完成多个Ac ...

  3. Python学习笔记5-字符串、bool、数值操作和数组字典排序

    1.字符串 # 字符串数字之间转换 # x = int("6") # print type(x) #<type 'str'> # y = str(6) # print ...

  4. Backward Digit Sums(暴力)

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5664   Accepted: 32 ...

  5. JScript中的prototype(原型)属性研究

    今天看到同事使用js中的Prototype,感觉很是新鲜.由此想深入学习一下prototype(英['prəʊtətaɪp] 美['protə'taɪp]n. 原型:标准,模范),在学习prototy ...

  6. 任意给定一个正整数N,求一个最小的正整数M(M>1),使得N*M的十进制表示形式里只含有1和0。

    题目:任意给定一个正整数N,求一个最小的正整数M(M>1),使得N*M的十进制表示形式里只含有1和0. 解法一:暴力求解.从1开始查找M,然后判断M*N=X这个数字是否只含有0,1. 解法二:由 ...

  7. UVA - 10098 - Generating Fast (枚举排列)

    思路:生成全排列,用next_permutation.注意生成之前先对那个字符数组排序. AC代码: #include <cstdio> #include <cstring> ...

  8. wikioi-1039-数的划分

    将整数n分成k份,且每份不能为空,任意两种划分方案不能相同(不考虑顺序). dp[i][j]:把数i分成k分的方案数 则:dp[i][j]=sum(dp[i-j][t])(t>=1&&a ...

  9. Mvc里查询商品页面

    /// <summary> /// 商品小类筛选页面 GoodsTypeName ----------------SelectGoods--商品筛选 --图文 /// Home/Selec ...

  10. easyui的样式easyui-textbox的一个bug

    easyui-testbox这个样式很恶心,用了这个就不能用传统的JQ来取值了,最近在使用上又发现了一个问题,就是赋值为0时,在输入框上会不显示,坑. <input class="ea ...