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;
    }
  • 相关阅读:
    linux异步信号handle浅析
    数据库的基本操作增删改查
    POJ1789Truck History最小生成树两种做法(Kruskal+Prim)模板题
    POJ1113Wall求凸包周长
    POJ3565AntsKM变形
    HDU2150Pipe判断线段是否相交
    POJ1815Friendship最大流最小割点+拆点+枚举
    HDU3081 Marriage Match II 最大匹配+并查集+匈牙利算法
    POJ3348Cows求凸包面积
    HDU3277Marriage Match III并查集+二分+最大流
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4792991.html
Copyright © 2011-2022 走看看