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 ...
随机推荐
- python分析log
最近做的一个项目,系统log下会生成如下的log(部分): [-- ::] Processing File transfer configured from ship to shore.... [-- ...
- springMVC导入excel案例poi
直接上代码: 第一步,controller 引入 private static final String CHECK_FILE = "checkExceFile"; /** * 对 ...
- 跟我学 NHibernate (三)
在使用 NHibernate 时,一定要将Mapping 映射文件,也就是 xml 文件的编译方式设置成 嵌入式,这是因为在 NHibernate 启动时,它会主动的到项目的启动目录中寻找 被设置为嵌 ...
- java.lang.NumberFormatException: For input string: "Y"
nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. ...
- 关于MPEG2中的图像序列和图像组头GOP
图像序列 图像序列是由图像组构成的,是随机存取段落. sequence_header_code – The sequence_header_code is the bit string ‘000001 ...
- android + javascript 相互通信实例分析
1. AndroidManifest.xml中必须使用许可 "android.permission.INTERNET", 否则会出Web page not available错误 ...
- OpenLDAP 安装及配置 笔记
首先下载 OpenLdap(Ldap服务器) 和 LdapAdmin(客户端) 两个软件 OpenLDAPforWindows_2.4.39.part1.rar OpenLDAPforWindows_ ...
- CentOS6.5配置vim使支持Python
CentOS6.5下开启vim对python的支持,配置方法如下: 1.检查系统已经安装了RPM包vim-enhanced; 2.复制默认的.vimrc初始化文件: # cp /usr/share/v ...
- 打地鼠游戏ios源码
打地鼠游戏源码,游戏是一款多关卡基于cocos2d的iPad打地鼠游戏源码,这也是一款高质量的打地鼠游戏源码,可以拥有逐步上升的关卡的设置,大家可以在关卡时设置一些商业化的模式来盈利的,非常完美的一款 ...
- (转载)Android content provider基础与使用
android有一个独特之处就是,数据库只能被它的创建者所使用,其他的应用是不能访问到的,所以如果你想实现不同应用之间的数据共享,就不得不用content provider了.在Android中,co ...