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
题意:找两个素数相差为N的素数。
先个所有素数打表。然后对每个N进行二分查找。
注意lower_bound二分查找的用法。
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 int zj[5000000]; 7 int ssb[5000000],pn; 8 void f() 9 { 10 int i,j; 11 memset(zj,0,sizeof(zj)); 12 zj[0]=zj[1]=1; 13 pn=0; 14 for (i=2;i<=5000000;i++) 15 { 16 if (!zj[i]) {ssb[pn]=i;pn++;} 17 for (j=0;j<pn;j++) 18 { 19 if (i*ssb[j]>5000000) break; 20 zj[i*ssb[j]]=1; 21 if (i%ssb[j]==0) break; 22 } 23 } 24 } 25 int main() 26 { 27 int t,n,i,c; 28 f(); 29 scanf("%d",&t); 30 while (t--) 31 { 32 scanf("%d",&n); 33 for (i=0;i<pn;i++) 34 { 35 c=lower_bound(ssb,ssb+pn,ssb[i]+n)-ssb; 36 if (ssb[c]==ssb[i]+n) 37 { 38 printf("%d %d\n",ssb[c],ssb[i]); 39 break; 40 } 41 } 42 } 43 }