Difference Between Primes
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 402 Accepted Submission(s): 110
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
今天在无数次的wa了之后。。
我终于发现这样的一个问题。。
oj里。 我用 int is[N], is[1]=false ,is[1]=true. 这样在gcc会被判wa掉。。
可能不同编译器它不是当 1和0来处理的吧。。
下次编程还是要严格点。
代码:
//
// 2143.cpp
// ACM_HDU
//
// Created by ipqhjjybj on 13-9-8.
// Copyright (c) 2013年 ipqhjjybj. All rights reserved.
//
#include <cstdio>
#include <cstdlib>
#include <set>
#include <cmath>
using namespace std;
const int N=1111111;
int prm[N],k;
bool is[N];
int getprm(int n){
int i,j,k=0;
int s,e=(int)(sqrt(0.0+n)+1);
memset(is,true,sizeof(is));
prm[k++]=2,is[0]=is[1]=0;
for(i=4;i<n;i+=2)is[i]=0;
for(i=3;i<e;i+=2)if(is[i]){
prm[k++]=i;
for(s=i+i,j=i*i;j<n;j+=s)
is[j]=0;
}
for(;i<n;i+=2)if(is[i])prm[k++]=i;
return k;
}
/*
下面的算法效率不够高。
int getprm(int n){
int k=0;
prm[k++]=2;
for(int i=2;i<n;i++)is[i]=true;
for(int i=4;i<n;i+=2)is[i]=false;
for(int i=3;i<n;i+=2)
if(is[i]){
prm[k++]=i;
for(int j=i+i;j<n;j+=i){
is[j]=false;
}
}
return k;
}
*/
set<int> S;
int main(){
k=getprm(N);
S.clear();
for(int i=0;i<k;i++)
S.insert(prm[i]);
int t,n,i,b;
bool flag;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
flag=false;
for(i=0;i<prm[i];i++){
b=prm[i]+n;
if(S.find(prm[i]+n)!=S.end()){
flag=true;
break;
}
}
if(flag){
printf("%d %d\n",prm[i]+n,prm[i]);
}else printf("FAIL\n");
}
return 0;
}