hdu 4715 Difference Between Primes
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=4715
Difference Between Primes
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
素数筛选+二分判断。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
using std::map;
using std::min;
using std::find;
using std::pair;
using std::vector;
using std::multimap;
using std::lower_bound;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1000000;
const int INF = 0x3f3f3f3f;
int tot, prime[N + 10];
bool is_prime[N + 10];
inline void init() {
tot = 0;
rep(i, N) is_prime[i] = true;
is_prime[0] = is_prime[1] = false;
for(int i = 2; i <= N; i++) {
if(is_prime[i]) {
prime[tot++] = i;
for(int j = 2 * i; j <= N; j += i) is_prime[j] = false;
}
}
}
void solve(int x) {
rep(i, tot) {
int p = lower_bound(prime, prime + tot, prime[i] + x) - prime;
if(prime[i] + x == prime[p]) {
printf("%d %d\n", prime[p], prime[i]);
return;
}
}
puts("FAIL");
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
init();
int t, n;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
solve(n);
}
return 0;
}
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/O ...
- hdu 4715 Difference Between Primes (打表 枚举)
Difference Between Primes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- hdu 4715 Difference Between Primes(素数筛选+树状数组哈希剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=4715 [code]: #include <iostream> #include <cstdio ...
- hdu 4715 Difference Between Primes 2013年ICPC热身赛A题 素数水题
题意:给出一个偶数(不论正负),求出两个素数a,b,能够满足 a-b=x,素数在1e6以内. 只要用筛选法打出素数表,枚举查询下就行了. 我用set储存素数,然后遍历set里面的元素,查询+x后是否还 ...
- 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 ...
- hdoj 4715 Difference Between Primes 素数筛选+二分查找
#include <string.h> #include <stdio.h> const int maxn = 1000006; bool vis[1000006]; int ...
- HDU 4715:Difference Between Primes
Difference Between Primes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- HDU 5487 Difference of Languages(BFS)
HDU 5487 Difference of Languages 这题从昨天下午2点开始做,到现在才AC了.感觉就是好多题都能想出来,就是写完后debug很长时间,才能AC,是不熟练的原因吗?但愿孰能 ...
- 【Difference Between Primes HDU - 4715】【素数筛法打表+模拟】
这道题很坑,注意在G++下提交,否则会WA,还有就是a或b中较大的那个数的范围.. #include<iostream> #include<cstdio> #include&l ...
随机推荐
- OpenStack和Redis
前言: 最近开始捣鼓OpenStack了,在用RDO部署OpenStack的时候,发现装了Redis, 遂决定看看OpenStack哪些地方(可以)用到Redis. Redis作为OpenStack ...
- 家业兴衰说传承(cc)
家业兴衰说传承 2011年10月30日 09:53 选稿:天潼 来源:东方网 作者:万润龙 浙商走笔之九 “富贵子生浪荡子,浪荡子生苦恼子,苦恼子生勤奋子,勤奋子生富贵子”.这“四子歌”是流传在浙 ...
- mac 下安装nginx
1,mac下的依赖: pcre-8.38.tar.gz nginx-1.4.7.tar.gz 2,解压pcre:进入器解压目录. EddydeMacBook-Pro:~ eddy$ cd /Users ...
- IIS7下,flush无效,解决方案
打开文件 C:\Windows\System32\inetsrv\config\applicationHost.config ,注意如果你的是64位系统,这个文件就必须用64位的编辑软件打开,32位的 ...
- 【Linux】Zabbix + MPM + msmtp + mutt 监控MySQL + 邮件报警
Zabbix部署参考博文 http://blog.sina.com.cn/s/blog_5611597901017oe0.html MPM安装配置参考博文和MPM官网下载地址 http://blog ...
- 图解Javascript上下文与作用域
原文网址:http://blog.rainy.im/2015/07/04/scope-chain-and-prototype-chain-in-js/ 本文尝试阐述Javascript中的上下文与作用 ...
- 无DLL远程注入
界面如下: 主要代码如下: #define STRLEN 20 typedef struct _DATA { DWORD dwLoadLibrary; DWORD dwGetProcAddress; ...
- C# 集合 — Hashtable 线程安全
基础知识重要吗?真的很重要. 就在笔者与同事聊天中突然同事提出一个问题,让笔都有点乱了手脚(有点夸张),题目是这样的: 问:Hashtable 是线程安全的吗? 答:…… (沉默中,Yes Or No ...
- rpm软件包
安装软件:rpm -i software.rpm卸载软件:rpm -e software升级形式安装:rpm -U software-new.rpmrpm支持通过http.ftp协议安装软件:rpm ...
- 如何解决谷歌浏览器下jquery无法获取图片的尺寸
代码如下: $(document).ready(function(){ var img_h=$img.height(); var img_w=$img.width(); }) 以上代码在IE和火狐中没 ...