HDU 4715 Difference Between Primes

Difference Between Primes

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 338    Accepted Submission(s): 78

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
2013 ACM/ICPC Asia Regional Online —— Warmup  

 

Recommend liuyiding     打出素数表, 枚举小的,   然后二分查找大的

 1 /* *******************************************
 2 Author       : kuangbin
 3 Created Time : 2013年09月08日 星期日 13时30分10秒
 4 File Name    : 1010.cpp
 5 ******************************************* */
 6 
 7 #include <stdio.h>
 8 #include <algorithm>
 9 #include <iostream>
10 #include <string.h>
11 #include <vector>
12 #include <queue>
13 #include <set>
14 #include <map>
15 #include <string>
16 #include <math.h>
17 #include <stdlib.h>
18 #include <time.h>
19 using namespace std;
20 
21 const int MAXN = 5000000;
22 int prime[MAXN];
23 void getPrime()
24 {
25     memset(prime,0,sizeof(prime));
26     for(int i = 2;i <= MAXN;i++)
27     {
28         if(!prime[i])prime[++prime[0]] = i;
29         for(int j = 1;j <= prime[0] && prime[j] <= MAXN/i;j++)
30         {
31             prime[prime[j]*i] = 1;
32             if(i % prime[j] == 0)break;
33         }
34     }
35 }
36 int main()
37 {
38     //freopen("in.txt","r",stdin);
39     //freopen("out.txt","w",stdout);
40     getPrime();
41     int T;
42     int n;
43     scanf("%d",&T);
44     while(T--)
45     {
46         scanf("%d",&n);
47         for(int i = 1;i < prime[0];i++)
48         {
49             int t = lower_bound(prime+1,prime + prime[0],prime[i]+n)- prime;
50             if(prime[t] == prime[i] + n)
51             {
52                 printf("%d %d\n",prime[t],prime[i]);
53                 break;
54             }
55         }
56     }
57     return 0;
58 }

 

       

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/p/3308696.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞