zoukankan      html  css  js  c++  java
  • hdu 4715 Difference Between Primes

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=4715 

    Difference Between Primes

    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

    素数筛选+二分判断。。

    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<cstdio>
    #include<vector>
    #include<map>
    using std::map;
    using std::min;
    using std::find;
    using std::pair;
    using std::vector;
    using std::multimap;
    using std::lower_bound;
    #define pb(e) push_back(e)
    #define sz(c) (int)(c).size()
    #define mp(a, b) make_pair(a, b)
    #define all(c) (c).begin(), (c).end()
    #define iter(c) __typeof((c).begin())
    #define cls(arr, val) memset(arr, val, sizeof(arr))
    #define cpresent(c, e) (find(all(c), (e)) != (c).end())
    #define rep(i, n) for(int i = 0; i < (int)n; i++)
    #define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
    const int N = 1000000;
    const int INF = 0x3f3f3f3f;
    int tot, prime[N + 10];
    bool is_prime[N + 10];
    inline void init() {
        tot = 0;
        rep(i, N) is_prime[i] = true;
        is_prime[0] = is_prime[1] = false;
        for(int i = 2; i <= N; i++) {
            if(is_prime[i]) {
                prime[tot++] = i;
                for(int j = 2 * i; j <= N; j += i) is_prime[j] = false;
            }
        }
    }
    void solve(int x) {
        rep(i, tot) {
            int p = lower_bound(prime, prime + tot, prime[i] + x) - prime;
            if(prime[i] + x == prime[p]) {
                printf("%d %d
    ", prime[p], prime[i]);
                return;
            }
        }
        puts("FAIL");
    }
    int main() {
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w+", stdout);
    #endif
        init();
        int t, n;
        scanf("%d", &t);
        while(t--) {
            scanf("%d", &n);
            solve(n);
        }
        return 0;
    }
  • 相关阅读:
    uva624 CD (01背包+路径的输出)
    算法:全排列
    Android使用Intent实现拨打电话的动作
    Java并发编程从入门到精通 张振华.Jack --我的书
    《算法导论》— Chapter 12 二叉查找树
    Java中arraylist和linkedlist源代码分析与性能比較
    Cg入门14:Vertex Shader
    Nucleus PLUS的启动、执行线程和中断处理
    Unity Shaders and Effects Cookbook (3-5) 金属软高光
    EasyDarwin开发出相似于美拍、秒拍的短视频拍摄SDK:EasyVideoRecorder
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4792991.html
Copyright © 2011-2022 走看看