Difference Between Primes

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 832    Accepted Submission(s): 267

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
 
Recommend
liuyiding
 
快速打素数表:
代码:
代码敲上去较为匆忙!,请自己优化......62ms
 #include<iostream>
#include<cstdio>
#define maxn 1000000
using namespace std;
int prime[];
bool bo[maxn+];
int prime_table()
{
int i,j,flag=;
memset(bo,,sizeof bo);
bo[]=bo[]=;
for(i=; i*i<=maxn;i++)
{
if(!bo[i])
{
for(j=i*i;j<=maxn;j+=i)
bo[j]=;
}
}
for(i=;i<=maxn;i++)
if(!bo[i]) prime[flag++]=i;
return flag;
}
bool isprime(int a)
{
for(int i=;prime[i]*prime[i]<=a;i++)
{
if(a%prime[i]==)
return ;
}
return ;
} int main()
{
int i,t,b,num;
num=prime_table();
scanf("%d",&t);
while(t--)
{
scanf("%d",&b);
if(b>=)
{
for(i=;i<num;i++)
{ if(isprime(b+prime[i]))
{
printf("%d %d\n",prime[i]+b,prime[i]);
break;
}
}
}
else
{
for(i=;i<num;i++)
{ if(isprime(prime[i]-b))
{
printf("%d %d\n",prime[i],prime[i]-b);
break;
}
}
}
}
return ;
}
 

HDUOJ-----Difference Between Primes的更多相关文章

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

  2. HDU 4715:Difference Between Primes

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

  3. hdu 4715 Difference Between Primes

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

  4. HDU 4715 Difference Between Primes (打表)

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

  5. hdu 4715 Difference Between Primes (打表 枚举)

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

  6. Difference Between Primes

    Problem Description All you know Goldbach conjecture.That is to say, Every even integer greater than ...

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

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

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

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

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

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

  10. 【Difference Between Primes HDU - 4715】【素数筛法打表+模拟】

    这道题很坑,注意在G++下提交,否则会WA,还有就是a或b中较大的那个数的范围.. #include<iostream> #include<cstdio> #include&l ...

随机推荐

  1. 跟我学Spring3(9.1):Spring的事务之数据库事务概述

    原文出处: 张开涛 9.1 数据库事务概述 事务首先是一系列操作组成的工作单元,该工作单元内的操作是不可分割的,即要么所有操作都做,要么所有操作都不做,这就是事务. 事务必需满足ACID(原子性.一致 ...

  2. 【BZOJ】【1038】【ZJOI2008】瞭望塔

    计算几何/半平面交 说是半平面交,实际上只是维护了个下凸壳而已……同1007水平可见直线 对于每条线段,能看到这条线段的点都在这条线段的“上方”,那么对所有n-1条线段求一个可视区域的交,就是求一个半 ...

  3. Windows环境搭建Red5流媒体服务器

    Windows环境搭建Red5流媒体服务器指南 测试环境:Windows 7 一.   下载安装程序 red5-server 下载地址 https://github.com/Red5/red5-ser ...

  4. ASM下裸设备的路径更改是否会影响数据库的执行

    通过asm来存储数据库文件,在linux下能够通过asmlib的方式来管理块设备,也能够直接使用裸设备来建立asm磁盘.在asmlib方式下,磁盘设备启动顺序和名称的改变不会影响到asm的使用.但假设 ...

  5. Android -- ImageLoader简析

    图片的内存缓存实现 Image-Loader库有一个较完整的内存缓存实现,使用者可以根据需要选择已经实现的策略,也可以定制自己项目中需要的策略. 内存缓存实现代码在memory和memory.impl ...

  6. sql-的int和varchar类型拼接的问题

    将int类型转换为varchar ,如cast(1 as varchar(10)),再进行连接set @sql =@sql+'update User set Medal='+@count+' wher ...

  7. MySQL监控主要指标及采集方法

    MySQL监控属于DB监控的模块之一,包括采集.展示.监控告警.本文主要介绍MySQL监控的主要指标和采集方法. MySQL监控和Redis监控的逻辑类似,可参考文章<Redis监控主要指标及采 ...

  8. 在 Ubuntu 12.04 上通过源码安装 Open vSwitch (OVS)

    安装 Ubuntu 12.04, 而且更新系统 apt-getupdate; apt-getupgrade; 安装所需的package apt-get install automake autocon ...

  9. ImportError: No module named arcpy

    好久没写Python脚本了,今天一运行就报错:未找到名称为 arcpy 的模块(ImportError: No module named arcpy). 多半是环境变量出问题了,Python最令人讨厌 ...

  10. berkelydb学习

    http://www.oracle.com/technetwork/cn/java/seltzer-berkeleydb-sql-085418-zhs.html 官网中文学习网址