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. Query的选择器

    Query的选择器   一. 基本选择器   1. ID选择器            ID选择器#id就是利用DOM元素的id属性值来筛选匹配的元素,并以iQuery包装集的形式返回给对象.      ...

  2. while与do while

    一:循环结构 循环的概念:重复得做某一件事情 举例: 打印50份试卷 沿操场跑10圈 做100道编程题 循环结构的特点: 循环条件 (50,10,100) 循环操作 (打印试卷,沿操场跑圈,做编程题) ...

  3. vue里使用elementUI里的下拉树表格,如何定义个性化的子表格?

    最近项目写到一个业务,首先需要展示各类分组的基本信息,然后需要点击每个分组展示该分组下子的所有具体信息 一开始我是打算用tab来展示就是首先父分组的名称就是各个不同的tab按钮,然后点击按钮再展示不同 ...

  4. 0120 springboot集成Mybatis和代码生成器

    在日常开发中,数据持久技术使用的架子使用频率最高的有3个,即spring-jdbc , spring-jpa, spring-mybatis.详情可以看我之前的一篇文章spring操作数据库的3个架子 ...

  5. poj1321棋盘问题(dfs+摆放问题)

    在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. ...

  6. Java修饰符类型

    转自原文:http://www.yiibai.com/java/java_modifier_types.html 修饰符是添加到这些定义来改变它们的含义的关键词. Java语言有各种各样修饰词,其中包 ...

  7. repeater列表中直接修改状态

    <asp:Repeater ID="RepeaterArticleList" runat="server" onitemdatabound="R ...

  8. TC基础与自动化

    前言 互联网产品的测试常常要覆盖在不同网络下的表现,例如丢包,带宽受限,时延及抖动等网络较差的情况,为了测试场景的网络情况可定义及复现,就需要有个工具对弱网进行模拟.业界一些通用的弱网模拟工具,如NE ...

  9. IntelliJ IDEA构建多Module项目

    打开IDEA 创建完成项目后,我们创建子模块 可以看到common子模块创建成功,子模块的名字大家可以根据自己的实际需求来修改 下面我们再创建子模块 给子模块起个名字 现在已经创建好多模块的项目了,下 ...

  10. 回顾 Monty Hall (三门问题)

    一.问题描述 Monty Hall Problem 源于美国的一档电视节目<Let's Make a Deal>,其中Monty Hall 是这个节目的主持人. 节目中有三扇门1.2.3, ...