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 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

题意:求两个素数的差为一个偶数的素数组合,如果存在多个,输出小的

分析:打出素数表,然后从小的开始枚举,二分查找就行。不过很奇怪的是,我一开始没有看到要加FAIL竟然也过了,也是醉的不行。

#pragma comprint(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<string>
#include<iostream>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<stdlib.h>
#include<ctime>
#include<algorithm>
#define LL __int64
#define FIN freopen("in.txt","r",stdin)
using namespace std;
const int MAXN=5000000;
int pri[MAXN],vis[MAXN];
int x,cnt;
void sieve(int n)
{
    int m=(int)sqrt(n+0.5);
    memset(vis,0,sizeof(vis));
    for(int i=2;i<=m;i++) if(!vis[i])
        for(int j=i*i;j<=n;j+=i) vis[j]=1;
}
int init(int n)
{
    sieve(n);
    cnt=0;
    for(int i=2;i<=n;i++) if(!vis[i])
        pri[++cnt]=i;
}
int main()
{
    //FIN;
    init(MAXN);
    int kase;
    scanf("%d",&kase);
    while(kase--)
    {
        bool flag=false;
        scanf("%d",&x);
        for(int i=1;i<cnt;i++)
        {
            int id=lower_bound(pri+1,pri+cnt,pri[i]+x)-pri;
            if(pri[id]==pri[i]+x)
            {
                printf("%d %d\n",pri[id],pri[i]);
                flag=true;
                break;
            }
        }
        if(!flag) printf("FAIL\n");
    }
    return 0;
}

时间: 2024-10-24 18:13:01

HDU 4715 Difference Between Primes (素数表+二分)的相关文章

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 pr

HDU2098分拆素数和【打素数表+二分】

大素数表+二分 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 7 const int maxn = 10005; 8 int prm[maxn / 3 + 1], is[maxn / 32 + 1]; 9 int n; 10 int k; 11 void get() { 12 int N =

hdu 4715 素数打表

先利用筛法完成素数打表 再从小到大判断即可 #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> using namespace std; const int Max = 1e6 + 50; int n; int isPrime[Max]; int tblPrime[Max];

素数筛选 HDU 4715

打表,把所有的素数找出来,并且还要把那些数是素数标记下 Difference Between Primes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2281    Accepted Submission(s): 642 Problem Description All you know Goldbach conjecture.Th

HDU 5878 I Count Two Three (打表+二分查找) -2016 ICPC 青岛赛区网络赛

题目链接 题意:给定一个数n,求大于n的第一个只包含2357四个因子的数(但是不能不包含其中任意一种),求这个数. 题解:打表+二分即可. #include <iostream> #include <math.h> #include <stdio.h> #include<algorithm> using namespace std; long long data[1000000],tot=0; int main() { long long maxn = 10

hdu 5407 CRB and Candies(组合数+最小公倍数+素数表+逆元)暨2015暑期多校赛第10场

题意: 输入n,求c(n,0)到c(n,n)的所有组合数的最小公倍数. 输入: 首行输入整数t,表示共有t组测试样例. 每组测试样例包含一个正整数n(1<=n<=1e6). 输出: 输出结果(mod 1e9+7). 感觉蛮变态的,从比赛开始我就是写的这道题,比赛结束还是没写出来…… 期间找到了逆元,最小公倍数,组合数的各种公式,但是爆了一下午tle. 比赛结束,题解告诉我,公式秒杀法…… 但是公式看不懂,幸好有群巨解说,所以有些听懂了,但还是需要继续思考才能弄懂. 题解: 设ans[i]表示i

POJ 1007 Difference Between Primes(线性筛法求N以内的素数表)

Difference Between Primes Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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, sk

[HDOJ6154] CaoHaha&#39;s staff(规律, 打表, 二分)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6154 有些题一辈子只会做一次,比如这个题.. 题意:炒鸡难懂,学弟读明白的.懒得描述,反正这题以后不会再做. f(i)表示i个线段能围成的最大面积,画画图就会发现一个规律. 然后查询的时候二分最小的大于等于s的即可. 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛

HDU 3641 Treasure Hunting (素数拆分)

题意:有N个ai,bi,M=a1^b1*a2^b2*a3^b3-*an^bn ,求最小的 x 使得 x! % M ==0. 思路:把M分成多个素数相乘,num[i] 记录素数 i 的个数,然后二分找到x,若 x! 中所有 i 的个数满足>=num[i] 即为答案. #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #in