Prime Friend

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5025 Accepted Submission(s): 1035

Problem Description

Besides the ordinary Boy Friend and Girl Friend, here we define a more academic kind of friend: Prime Friend. We call a nonnegative integer A is the integer B’s Prime Friend when the sum of A and B is a prime.

So an integer has many prime friends, for example, 1 has infinite prime friends: 1, 2, 4, 6, 10 and so on. This problem is very simple, given two integers A and B, find the minimum common prime friend which will make them not only become primes but also prime neighbor. We say C and D is prime neighbor only when both of them are primes and integer(s) between them is/are not.

Input

The first line contains a single integer T, indicating the number of test cases.

Each test case only contains two integers A and B.

Technical Specification

  1. 1 <= T <= 1000
  2. 1 <= A, B <= 150

Output

For each test case, output the case number first, then the minimum common prime friend of A and B, if not such number exists, output -1.

Sample Input

2
2 4
3 6

Sample Output

Case 1: 1
Case 2: -1

Author

iSea@WHU

Source

The 6th Central China Invitational Programming Contest and 9th Wuhan University Programming Contest Final


先线性筛出所有质数,然后对所有邻质数对进行处理。为了方便描述,我们设一对邻质数的差为Ki,很明显,Ki > 150时是毫无用处的(因为输入中两个数最极端的差为150),并且,对于每个小于等于150的Ki,只用保留较大质数小于等于150的所有邻质数与大于150的第一对邻质数。

有点拗口 (语文没学好T-T) 也就是说,Ki大于150的,除了第一个,其他都不要了,因为它们肯定不及第一个优(求的是x的最小值嘛),但是留下的都有价值 。

思路

#include<cstdio>
#include<vector>
using namespace std;
#define si 13626407
//最大的有效质数是这个,输出max(a[i][j])得到的(节约空间,人人有责) int T;
int A, B, sum;//sum为 已取完所有 有效素数 的Ki有几个
bool p[si + 5];//加5免得越界
int v[887319], tot; // 1~si范围内质数的个数+5 tot存储目前素数个数
bool c[155];//判断第一个大于150的并且差为Ki的有没有出现
vector<int> a[155];//a[Ki]存储所有差为Ki有效的邻质数(较小的那个) inline void init(){//线性筛素数
p[1] = 1;//1不是素数
int t;
for ( int i = 2; i <= si; ++i ){
if ( !p[i] ){
v[++tot] = i;
if ( i <= 150 ){//开始时没加,但也可以过,数据比较水
a[0].push_back(i);
}else{
if ( !c[0] ){
c[0] = 1;
a[0].push_back(i);
}
}
if ( i != 2 ){
if ( i <= 150 ) a[i - t].push_back(t);
else{
if ( i - t <= 150 && !c[i - t] ){
c[i - t] = 1;
a[i - t].push_back(t);
sum++;
}
}
}
t = i;
}
if ( sum >= 75 ) break; // Ki只可能是偶数(因为质数除2外都是奇数,奇数减奇数为偶数) 因此只有 150 个(当然了,除2、3外,但是这里2和3不计入sum)
for ( int j = 1; j <= tot; ++j )
if ( i * v[j] <= si ) p[i * v[j]] = 1;
else break;
}
} int main(){
init();
scanf( "%d", &T );
for ( int I = 1; I <= T; ++I ){
scanf( "%d%d", &A, &B );
if ( A > B ){
int t(A); A = B; B = t;//如果A > B 就交换 使 A <= B
}
int ans(-1);
for ( int i = 0; i < a[B - A].size(); ++i ){//在所有差为B - A的质数对中找第一个满足条件的数
if ( A <= a[B - A][i] ){
ans = a[B - A][i] - A;
break;
}
}
printf( "Case %d: %d\n", I, ans );
}
return 0;
}

撒花(^-^)

「HDU3823」 Prime Friend 解题报告的更多相关文章

  1. 「FJOI2016」神秘数 解题报告

    「FJOI2016」神秘数 这题不sb,我挺sb的... 我连不带区间的都不会哇 考虑给你一个整数集,如何求这个神秘数 这有点像一个01背包,复杂度和值域有关.但是你发现01背包可以求出更多的东西,就 ...

  2. 「ZJOI2016」大森林 解题报告

    「ZJOI2016」大森林 神仙题... 很显然线段树搞不了 考虑离线操作 我们只搞一颗树,从位置1一直往后移动,然后维护它的形态试试 显然操作0,1都可以拆成差分的形式,就是加入和删除 因为保证了操 ...

  3. 「SCOI2016」背单词 解题报告

    「SCOI2016」背单词 出题人sb 题意有毒 大概是告诉你,你给一堆n个单词安排顺序 如果当前位置为x 当前单词的后缀没在这堆单词出现过,代价x 这里的后缀是原意,但不算自己,举个例子比如abc的 ...

  4. 「NOI2015」寿司晚宴 解题报告

    「NOI2015」寿司晚宴 这个题思路其实挺自然的,但是我太傻了...最开始想着钦定一些,结果发现假了.. 首先一个比较套路的事情是状压前8个质数,后面的只会在一个数出现一次的再想办法就好. 然后发现 ...

  5. 「SCOI2015」国旗计划 解题报告

    「SCOI2015」国旗计划 蛮有趣的一个题 注意到区间互不交错,那么如果我们已经钦定了一个区间,它选择的下一个区间是唯一的,就是和它有交且右端点在最右边的,这个可以单调队列预处理一下 然后往后面跳拿 ...

  6. 「JLOI2015」骗我呢 解题报告?

    「JLOI2015」骗我呢 这什么神仙题 \[\color{purple}{Link}\] 可以学到的东西 对越过直线的东西翻折进行容斥 之类的..吧? Code: #include <cstd ...

  7. 「JLOI2015」城池攻占 解题报告

    「JLOI2015」城池攻占 注意到任意两个人的战斗力相对大小的不变的 可以离线的把所有人赛到初始点的堆里 然后做启发式合并就可以了 Code: #include <cstdio> #in ...

  8. 「JLOI2015」管道连接 解题报告

    「JLOI2015」管道连接 先按照斯坦纳树求一个 然后合并成斯坦纳森林 直接枚举树的集合再dp一下就好了 Code: #include <cstdio> #include <cct ...

  9. 「JLOI2015」战争调度 解题报告

    「JLOI2015」战争调度 感觉一到晚上大脑就宕机了... 题目本身不难,就算没接触过想想也是可以想到的 这个满二叉树的深度很浅啊,每个点只会和它的\(n-1\)个祖先匹配啊 于是可以暴力枚举祖先链 ...

随机推荐

  1. 模板—e-dcc缩点

    int dfn[MAXN],low[MAXN],cnt; bool isbridge[MAXN]; void tarjan(int x,int edg) { low[x]=dfn[x]=++cnt; ...

  2. hdu 2807 The Shortest Path(矩阵+floyd)

    The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. 『PyTorch』第十一弹_torch.optim优化器 每层定制参数

    一.简化前馈网络LeNet 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 im ...

  4. 深入理解String、StringBuffer、StringBuilder(转)

    文章系转载,非原创,原地址: http://www.cnblogs.com/dolphin0520/p/3778589.html 相信String这个类是Java中使用得最频繁的类之一,并且又是各大公 ...

  5. python3在pycharm中为什么导入random模块不能用? TypeError: 'module' object is not callable

    新手学python求大神指导,也用sys导入了random.py的路径,仍然不行. 刚刚排错貌似找到了问题的原因...那是因为我在pycharm中新建的python文件名就是random,所以当前目录 ...

  6. 三星s5830刷机小记

    拿起好久没用的三星s5830手机,看了看手机内存所无几,运行十分缓慢,就想着收拾下,当个备机用,在刷机前我做了个小实验,先把手机root,安装RE管理器,把system/app下的所有东西都删了,因为 ...

  7. UVA 437 "The Tower of Babylon" (DAG上的动态规划)

    传送门 题意 有 n 种立方体,每种都有无穷多个. 要求选一些立方体摞成一根尽量高的柱子(在摞的时候可以自行选择哪一条边作为高): 立方体 a 可以放在立方体 b 上方的前提条件是立方体 a 的底面长 ...

  8. H3C 无类域间路由斜线表示法

  9. 乐视X3-40S智能电视的简化系统刷机

    步骤 USB2.0-U盘一个. 先把letv原厂包里的.bin文件放入U盘刷入电视 (U盘插在电视上方的USB2.0插口处,在电视待机状态下用遥控器依次按下[3].[6].[9].[5].[开机]键, ...

  10. es6笔记 day3---数组新增东西

    Array.from()的作用就是把类数组转成数组.所谓类数组,就是有长度的数组 ----------------------------------------------------------- ...