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. 获取properties文件的内容

    获取properties文件的内容 public void test() throws Exception{ String resource = "application.propertie ...

  2. POJ-3984-迷宫问题(bfs+记录路径)

    定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...

  3. shell-删除指定时间前的文件

    需要配合find和rm两个命令完成 显示20分钟前的文件: find /home/prestat/bills/test -type f -mmin +20 -exec ls -l {} \; 删除20 ...

  4. BZOJ4710: [Jsoi2011]分特产 组合数学 容斥原理

    题意:把M堆特产分给N个同学,要求每个同学至少分到一种特产,共有多少种分法? 把A个球分给B个人的分法种数:(插板法,假设A个球互不相同,依次插入,然后除以全排列去重) C(A,B+A) 把M堆特产分 ...

  5. spring项目启动报错

    个人博客 地址:http://www.wenhaofan.com/article/20180921134534 错误信息 ERROR [localhost-startStop-1] - Context ...

  6. JVM学习-环境构建

    想学习JVM,java虚拟机的底层原理.下面介绍下怎么将Java文件compiler成字节码,然后反编译为二进制查看分析. 一.JavaClass.java文件: package com.gqz.ja ...

  7. C# asp.net 连接Mysql 数据库

    首先添加 引用: using System.Data;using MySql.Data.MySqlClient; 代码: 这里只写了 后台代码 Mysql 连接和sql 连接代码几乎一样 只要把 My ...

  8. Qt VS Tools插件官方下载及安装

    下载 官方下载地址:https://download.qt.io/development_releases/vsaddin/(国外网站直接打开超级慢) 找到对应的VS版本下载 安装 下载完成后安装,打 ...

  9. 0004 继承django系统用户表

    1 创建基础模型 在本项目中,所有表都有两个自动产生的完全相同的字段,即创建时间和更新时间,因此,可以创建一个基础模型,让所有的表都来继承这个模型. 找到工程目录下的PublicMethod目录,创建 ...

  10. SQLServer导出查询结果带表头(标题行)

    SQLServer导出查询结果带表头(标题行) 平时我们经常会需要将SQLSERVER查询的结果复制到EXCEL文档中进行分析处理,但是有一件事很头痛,就是复制结果网格的数据到EXCEL之后,都是没有 ...