hdu 4715 Difference Between Primes (二分查找)

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 }
时间: 2024-08-28 11:26:08

hdu 4715 Difference Between Primes (二分查找)的相关文章

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): 2998    Accepted Submission(s): 850 Problem Description All you know Goldbach conjecture.That is to say, Every even inte

HDU 4614 线段树+二分查找

Vases and Flowers 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4614 Problem Description Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N-1. When she receive some flowers, she will try to put them

HDU 2446 Shell Pyramid(二分查找 数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2446 Problem Description In the 17th century, with thunderous noise, dense smoke and blazing fire, battles on the sea were just the same as those in the modern times. But at that time, the cannon ,were e

hdu 2141 Can you find it?(二分查找)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 题目大意:查找是否又满足条件的x值. 这里简单介绍一个小算法,二分查找. 1 /* 2 3 x^2+6*x-7==y 4 输入y 求x 精确度为10^-5 5 0=<x<=10000 6 7 */ 8 #include <iostream> 9 #include <cstdio> 10 using namespace std; 11 int main (void) 1

hdu 5592 ZYB&#39;s Premutation (线段树+二分查找)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=5592 Problem Description ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutation,now he ask you to  restore the premutation.Pair (i,j)(i<j) is considered as a reverse

STL之二分查找:hdu 5178 ( BestCoder Round #31 1001 )

STL包含四种不同的二分查找算法,binary_search    lower_bound  upper_bound   equal_range.他们的作用域是已经排序好的的数组. ★binary_search试图在已排序的[first, last)中寻找元素value.如果找到它会返回true,否则返回false,它不返回查找位置. ★iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素. ★iterat

hdu 5249 KPI 【二分查找】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5249 分析:这道题是2015百度之星初赛1的4题 这道题不算太难当时队友做出来了,不过费了老大劲,其实我从中能够吸取教训的, 原因是,我一看这道题就是数据结构的,然后和队友想的一样二分查找,但是从中 遇到了一系列的问题: 首先储存数据我们不能用带有下标的数组,因为题目中的数据是可删可添的这样如果用 数组的话时间复杂度是相当高的,因为如果按大小插入,就要移动其他数据,如果在 尾部添加,就要对其排序,因

hdu 5676 ztr loves lucky numbers(BC——暴力打表+二分查找)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5676 ztr loves lucky numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 594    Accepted Submission(s): 257 Problem Description ztr loves luck

hdu 4938 Seeing People 排序+二分查找

Seeing People Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 241    Accepted Submission(s): 61 Problem Description There are two kinds of people. If person i is the first kind of people, it