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. 3331: [BeiJing2013]压力

    3331: [BeiJing2013]压力 LCA+树上差分,和之前类似的题差不多,就是多了个v-dcc缩点,唯一要注意的就是判断是否是割点,对于不是割点的点,如果他是起点或重点,ans++,和差分没 ...

  2. Project Euler Problem 23-Non-abundant sums

    直接暴力搞就行,优化的地方应该还是计算因子和那里,优化方法在这里:http://www.cnblogs.com/guoyongheng/p/7780345.html 这题真坑,能被写成两个相同盈数之和 ...

  3. Samba服务器 安装

    yum -y install samba cp /etc/samba/smb.conf /etc/samba/smb.conf.bak cat >> /etc/samba/smb.conf ...

  4. array_map 用法

    array_map - 将回调函数作用到数组中的每一个元素上 function add2($value) { return $value + 2; } $arr = array(1, 2, 3, 4, ...

  5. 2019-8-15-win10-edge-打开闪退问题

    title author date CreateTime categories win10 edge 打开闪退问题 lindexi 2019-08-15 08:53:22 +0800 2019-8-1 ...

  6. Vue 设置class样式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Codevs 均分纸牌(贪心)

    题目描述 Description 有 N 堆纸牌,编号分别为 1,2,…, N.每堆上有若干张,但纸牌总数必为 N 的倍数.可以在任一堆上取若于张纸牌,然后移动. 移牌规则为:在编号为 1 堆上取的纸 ...

  8. 性能测试基础-HTTP用例设计

    JSON格式请求: web_custom_request("https://xx.xx.xx.xx:xx/pvcpappinf//msgcustomization/xinPowGenDay, ...

  9. Vscode 开发插件

    vs常用公共插件 Auto Close Tag 自动闭合标签 Auto Rename Tag 自动重命名标签 AutoFileName 自动联想文件名 Autoprefixer 自动兼容前缀 Auto ...

  10. AOP 事物连接,记忆连接数据库,连接池

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www ...