hdoj 4715 Difference Between Primes 素数筛选+二分查找

简介: hdoj 4715 Difference Between Primes 素数筛选+二分查找
#include <string.h>
#include <stdio.h>
const int maxn = 1000006;
bool vis[1000006];
int pr[1000005];
int cnt = 1;
int bs(int l, int r, int v)
{
    int mid=(l+r)>>1;
    while(l < r)
    {
        if(pr[mid] < v)
            l = mid+1;
        else
            r = mid;
        mid= (l+r) >> 1;
    }
    return l;
}
void getpr()
{
    int i,j;
    for(i=2;i*i<maxn;i++) if(!vis[i])
    {
        pr[cnt++]=i;
        for(j=i*i;j<maxn;j+=i) vis[j]=1;
    }
    for(;i<maxn;i++)if(!vis[i])
    {
        pr[cnt++]=i;
    }
}
int main()
{
    memset(vis, 0, sizeof(vis));
    getpr();
    int n, t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d", &n);
        int ans1 = 0;
        int ans2 = 0;
        for (int i = 1; i <= cnt; i++)
        {
            if (pr[i] <= n)
                continue;
            int x = bs(1, cnt-1, pr[i]-n);
            if (pr[i] - n == pr[x])
            {
                ans1 = pr[i];
                ans2 = pr[x];
                break;
            }
        }
        if (ans1)
            printf("%d %d\n",ans1, ans2);
        else
            puts("FAIL");
    }
    return 0;
}
目录
相关文章
HDOJ 1016 Prime Ring Problem素数环【深搜】
HDOJ 1016 Prime Ring Problem素数环【深搜】
90 0
HDOJ 1016 Prime Ring Problem素数环【深搜】
HDOJ(HDU) 2136 Largest prime factor(素数筛选)
HDOJ(HDU) 2136 Largest prime factor(素数筛选)
89 0
|
Go
HDOJ(HDU) 1977 Consecutive sum II(推导、、)
HDOJ(HDU) 1977 Consecutive sum II(推导、、)
87 0
HDOJ 1716 排列2 next_permutation函数
HDOJ 1716 排列2 next_permutation函数
106 0
|
Java
HDOJ 1716 排列2(next_permutation函数)
HDOJ 1716 排列2(next_permutation函数)
91 0
[LeetCode]--71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example, path = “/home/”, =&gt; “/home” path = “/a/./b/../../c/”, =&gt; “/c” click to show corner cases. Corner Cases:
1036 0
|
算法
[LeetCode]--47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [ [1,1,2], [1,2,1],
1031 0
[LeetCode]--46. Permutations
Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2],
1217 0