Difference Between Primes
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 832 Accepted Submission(s): 267
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
Recommend
liuyiding
快速打素数表:
代码:
代码敲上去较为匆忙!,请自己优化......62ms
1 #include<iostream> 2 #include<cstdio> 3 #define maxn 1000000 4 using namespace std; 5 int prime[78500]; 6 bool bo[maxn+5]; 7 int prime_table() 8 { 9 int i,j,flag=0; 10 memset(bo,0,sizeof bo); 11 bo[0]=bo[1]=1; 12 for(i=2; i*i<=maxn;i++) 13 { 14 if(!bo[i]) 15 { 16 for(j=i*i;j<=maxn;j+=i) 17 bo[j]=1; 18 } 19 } 20 for(i=2;i<=maxn;i++) 21 if(!bo[i]) prime[flag++]=i; 22 return flag; 23 } 24 bool isprime(int a) 25 { 26 for(int i=0;prime[i]*prime[i]<=a;i++) 27 { 28 if(a%prime[i]==0) 29 return 0; 30 } 31 return 1; 32 } 33 34 int main() 35 { 36 int i,t,b,num; 37 num=prime_table(); 38 scanf("%d",&t); 39 while(t--) 40 { 41 scanf("%d",&b); 42 if(b>=0) 43 { 44 for(i=0;i<num;i++) 45 { 46 47 if(isprime(b+prime[i])) 48 { 49 printf("%d %d ",prime[i]+b,prime[i]); 50 break; 51 } 52 } 53 } 54 else 55 { 56 for(i=0;i<num;i++) 57 { 58 59 if(isprime(prime[i]-b)) 60 { 61 printf("%d %d ",prime[i],prime[i]-b); 62 break; 63 } 64 } 65 } 66 } 67 return 0; 68 }