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.

InputThe 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 <= 150OutputFor 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 题意:给出两个整数ab 使a+x b+x均为素数 且ab之间没有素数 求最小的符合条件的x 思路:java数组开大了就爆空间,注意空间;一个潜在条件a+x-(b+x)=a-b<150,即两个素数之差小于150; 代码:
import java.util.Scanner;
public class Main {
static final int max=(int)16000000;
static int prime[]=new int[1031131];
static boolean is_prime[]=new boolean[max];
static int k=0;
public static void Prime(){
is_prime[0]=is_prime[1]=true;
for(int i=2;i<max;i++){
if(!is_prime[i]) prime[k++]=i;
for(int j=0;j<k&&prime[j]*i<max;j++){
is_prime[i*prime[j]]=true;
if(i%prime[j]==0) break;
}
}
}
public static void main(String[] args) {
Prime();
// System.out.println(k);
Scanner scan=new Scanner(System.in);
int t=scan.nextInt();
for(int i=1;i<=t;i++){
int a=scan.nextInt();
int b=scan.nextInt();
if(a>b) {
int tmp=a;
a=b;
b=tmp;
}
System.out.print("Case "+i+": ");
boolean flag=false;
int num=0;
for(int j=0;j<k-1;j++){
if(prime[j]>=a && prime[j+1]>=b &&prime[j]-a==prime[j+1]-b &&prime[j+1]-prime[j]<150){
num=prime[j]-a;
flag=true;
break;
}
}
if(flag) System.out.println(num);
else System.out.println("-1");
}
}
}

HDU 3823 Prime Friend(线性欧拉筛+打表)的更多相关文章

  1. POJ2909_Goldbach's Conjecture(线性欧拉筛)

    Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one p ...

  2. hdu 2824 The Euler function 欧拉函数打表

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. hdu3572线性欧拉筛

    用线性筛来筛,复杂度O(n) #include<bits/stdc++.h> #include<ext/rope> #define fi first #define se se ...

  4. BZOJ 2818 Gcd 线性欧拉筛(Eratosthenes银幕)

    标题效果:定整N(N <= 1e7),乞讨1<=x,y<=N和Gcd(x,y)素数的数(x,y)有多少.. 思考:推,. 建立gcd(x,y) = p,然后,x / p与y / p互 ...

  5. Goldbach's Conjecture POJ - 2262 线性欧拉筛水题 哥德巴赫猜想

    题意 哥德巴赫猜想:任一大于2的数都可以分为两个质数之和 给一个n 分成两个质数之和 线行筛打表即可 可以拿一个数组当桶标记一下a[i]  i这个数是不是素数  在线性筛后面加个装桶循环即可 #inc ...

  6. Sum of Consecutive Prime Numbers POJ - 2739 线性欧拉筛(线性欧拉筛证明)

    题意:给一个数 可以写出多少种  连续素数的合 思路:直接线性筛 筛素数 暴力找就行   (素数到n/2就可以停下了,优化一个常数) 其中:线性筛的证明参考:https://blog.csdn.net ...

  7. Dirichlet's Theorem on Arithmetic Progressions POJ - 3006 线性欧拉筛

    题意 给出a d n    给出数列 a,a+d,a+2d,a+3d......a+kd 问第n个数是几 保证答案不溢出 直接线性筛模拟即可 #include<cstdio> #inclu ...

  8. BZOJ 2190 SDOI 2008 仪仗队 线性欧拉筛

    标题效果:有一个格子组件图,假设三个人在一条直线上,那么第一个人将不会看到第三人.现在,有一个人站在(1,1)在.我问他是否能看到n*n的人数的矩阵. 思考:如果你想站(1,1)这名男子看到了一个立场 ...

  9. The Embarrassed Cryptographer POJ - 2635 同余模+高精度处理 +线性欧拉筛(每n位一起处理)

    题意:给出两数乘积K(1e100) 和 一个数L(1e6)  问有没有小于L(不能等于)的素数是K的因数 思路:把数K切割 用1000进制表示   由同余模公式知   k%x=(a*1000%x+b* ...

随机推荐

  1. pip问题:Traceback (most recent call last): File "/usr/bin/pip", line 9, in

    源作者blog https://blog.csdn.net/vmxhc1314/article/details/81869676 编辑提示的文件,进行更改即可. 解决方法: 将 /usr/bin/pi ...

  2. HTML连载64-a标签伪类选择器的注意点与练习

    一.a标签的伪类选择器注意点 (1)a标签的伪类选择器可以单独出现,也可以一起出现.也就是可以设置多个状态的样式. (2) a标签的伪类选择器如果一起出现,那么有严格的顺序要求,编写的顺序必须要遵守原 ...

  3. Chrome浏览器控制台报Refused to get unsafe header "XXX"的错误

    最近在调试后端下载的接口时在浏览器的控制台中发现了红色的错误信息,例如Refused to get unsafe header "XXX":前端是采用XMLHttpRequest对 ...

  4. Web Workers - (Worker(专有) and SharedWorker(共享))

    Web Worker为Web内容在后台线程中运行脚本提供了一种简单的方法 线程可以执行任务而不干扰用户界面 可以使用XMLHttpRequest执行 I/O (尽管responseXML和channe ...

  5. Win7最后一天,微软开始慌了!

    就在昨天(2020年1月14日),服役十年的Win 7正式退出了微软舞台,从2009推出到2019,这十年也是很多90后的青春. 当然微软官方也做了送别,当然其目的也是为了推广Win10! 甚至面对痛 ...

  6. JS高级---正则表达式练习身份证号码

    写正则表达式, 根据字符串来写正则表达式进行匹配 经验: 1.找规律  2.不要追求完美   身份证的正则表达式 15位或者18位 ([1-9][0-9]{14})|([1-9][0-9]{16}[0 ...

  7. MySQL启动和停止

    MySQL视为大仓库,关闭的时候有权限有身份都无法进入使用的时候要保持启用状态 方式一: 计算机游击找到“管理”打开 左侧栏目中找到“服务和应用程序”打开 打开服务 找到MySQL...,右击.... ...

  8. JavaDay1(上)

    Java learning_Day1(上) 一切准备工作已经做好,虽然自己之前也零零碎碎学了一些Java的基础知识,貌似现在忘得差不多了,趁寒假契机从头开始学习吧 本人学习视频用的是马士兵的,也在这里 ...

  9. php设计模式之装饰模式实例代码

    <?php header("Content-type:text/html;charset=utf-8"); /** * 文章编辑类 */ class Article { pr ...

  10. gets(), getline(), cin.getline()

    gets(str), getline(cin, s), cin.getline(str, len),这三个函数都是读入一行字符串的函数,下面是这三个函数的区别 1. gets() 函数是 C 语言的函 ...