Difference Between Primes

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 528    Accepted Submission(s): 150
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


思路:
打个素数表,然后枚举b,判断a就够了。
ps:汗,开始以为xat为正数,一直WA,原来题目说的是绝对值。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define maxn 10000005
using namespace std; int n,m,cxx,ans;
bool vis[maxn];
int prime[664580]; void sieve(int nn)
{
int i,j,mm;
mm=int(sqrt(nn+0.5));
memset(vis,0,sizeof(vis));
for(i=2;i<=mm;i++)
{
if(!vis[i])
{
for(j=i*i;j<=nn;j+=i)
{
vis[j]=1;
}
}
}
}
int get_prime(int nn)
{
int i,c=0;
sieve(nn);
for(i=2;i<=nn;i++)
{
if(!vis[i]) prime[c++]=i;
}
return c;
}
bool isprime(int x)
{
int i,j,t;
t=sqrt(x+0.5);
for(i=2;i<=t;i++)
{
if(x%i==0) return false ;
}
return true ;
}
int main()
{
int i,j,t,a,b,flag,sgn;
cxx=get_prime(10000000); // 664579
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
flag=0;
if(n==0)
{
printf("2 2\n");
continue ;
}
if(n>0) sgn=1;
else sgn=0,n=-n;
for(i=0;i<cxx;i++)
{
b=prime[i];
a=b+n;
if(isprime(a))
{
flag=1;
if(sgn) printf("%d %d\n",a,b);
else printf("%d %d\n",b,a);
break ;
}
}
if(!flag) printf("FAIL\n");
}
return 0;
}



 

hdu 4715 Difference Between Primes (打表 枚举)的更多相关文章

  1. HDU 4715 Difference Between Primes (打表)

    Difference Between Primes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...

  2. hdu 4715 Difference Between Primes

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Description All you kn ...

  3. hdu 4715 Difference Between Primes(素数筛选+树状数组哈希剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=4715 [code]: #include <iostream> #include <cstdio ...

  4. hdu 4715 Difference Between Primes 2013年ICPC热身赛A题 素数水题

    题意:给出一个偶数(不论正负),求出两个素数a,b,能够满足 a-b=x,素数在1e6以内. 只要用筛选法打出素数表,枚举查询下就行了. 我用set储存素数,然后遍历set里面的元素,查询+x后是否还 ...

  5. hduoj 4715 Difference Between Primes 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Time Limit: 2000/1000 MS (J ...

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

    #include <string.h> #include <stdio.h> const int maxn = 1000006; bool vis[1000006]; int ...

  7. HDU 4715:Difference Between Primes

    Difference Between Primes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  8. HDU 4548 美素数(打表)

    HDU  4548  美素数(打表)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88159#problem/H 题目 ...

  9. HDU 5487 Difference of Languages(BFS)

    HDU 5487 Difference of Languages 这题从昨天下午2点开始做,到现在才AC了.感觉就是好多题都能想出来,就是写完后debug很长时间,才能AC,是不熟练的原因吗?但愿孰能 ...

随机推荐

  1. 02-Foundation-NSMutableString、NSNumber、NSValue、NSDate、NSArray

    目录: 一.NSMutableString可变字符串 二.NSNumber数字对象 三.NSValue 四.NSDate日期对象 五.NSArray数组对象 回到顶部 一.NSMutableStrin ...

  2. C++界面库

    刚开始用C++做界面的时候,根本不知道怎么用简陋的MFC控件做出比较美观的界面,后来就开始逐渐接触到BCG  Xtreme ToolkitPro v15.0.1,Skin++,等界面库,以及一些网友自 ...

  3. c++11 新特性之 autokeyword

    C++11是对眼下C++语言的扩展和修正.C++11包含大量的新特性:包含lambda表达式,类型推导keywordauto.decltype,和模板的大量改进. g++编译c++11命令加上 -st ...

  4. 巧用test判断来写shell脚本

    感觉最近很忙啊,阿里巴巴和百度马上就要笔试了,算法神马的还没有看..还是安心学习linux吧,决定在接下来的一周里,每天写一个shell script #!/bin/bash #输出提示语句,请输入一 ...

  5. Android---60---Notification 通知栏的简单使用

    Notification是显示在手机状态栏的通知 通过Notification.Builder类创建Notification对象. Notification.Builder经常用法: setDefau ...

  6. NYOJ10,skiing

    skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描写叙述 Michael喜欢滑雪百这并不奇怪, 由于滑雪的确非常刺激.但是为了获得速度,滑的区域必须向下倾斜,并且 ...

  7. BZOJ 1529: [POI2005]ska Piggy banks( 并查集 )

    每一连通块砸开一个就可以拿到所有的钱, 所以用并查集求连通块数 ------------------------------------------------------------------- ...

  8. ean128与code128 条形码 算法分析

    [code128条形码组成] 除终止符(STOP)由13个模块组成外,其他字符均由11个模块组成 就是说,如果用‘1’表示黑线(实模块),用‘0’表示白线(空模块),那么每表示一个字符就需要11条线, ...

  9. C中嵌入SQL

    连接到SAMPLE数据库,查询LASTNAME为JOHNSON的FIRSTNAME信息. #include <stdio.h> #include <stdlib.h> #inc ...

  10. 安全威胁无孔不入:基于Linux系统的病毒(转)

    虽然在Linux里传播的病毒不多,但也是存在一些.我从一些安全网站搜集了一些资料. 1.病毒名称: Linux.Slapper.Worm 类别: 蠕虫 病毒资料: 感染系统:Linux 不受影响系统: ...