HDU 3823 Prime Friend(线性欧拉筛+打表)
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(线性欧拉筛+打表)的更多相关文章
- POJ2909_Goldbach's Conjecture(线性欧拉筛)
Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one p ...
- hdu 2824 The Euler function 欧拉函数打表
The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu3572线性欧拉筛
用线性筛来筛,复杂度O(n) #include<bits/stdc++.h> #include<ext/rope> #define fi first #define se se ...
- BZOJ 2818 Gcd 线性欧拉筛(Eratosthenes银幕)
标题效果:定整N(N <= 1e7),乞讨1<=x,y<=N和Gcd(x,y)素数的数(x,y)有多少.. 思考:推,. 建立gcd(x,y) = p,然后,x / p与y / p互 ...
- Goldbach's Conjecture POJ - 2262 线性欧拉筛水题 哥德巴赫猜想
题意 哥德巴赫猜想:任一大于2的数都可以分为两个质数之和 给一个n 分成两个质数之和 线行筛打表即可 可以拿一个数组当桶标记一下a[i] i这个数是不是素数 在线性筛后面加个装桶循环即可 #inc ...
- Sum of Consecutive Prime Numbers POJ - 2739 线性欧拉筛(线性欧拉筛证明)
题意:给一个数 可以写出多少种 连续素数的合 思路:直接线性筛 筛素数 暴力找就行 (素数到n/2就可以停下了,优化一个常数) 其中:线性筛的证明参考:https://blog.csdn.net ...
- Dirichlet's Theorem on Arithmetic Progressions POJ - 3006 线性欧拉筛
题意 给出a d n 给出数列 a,a+d,a+2d,a+3d......a+kd 问第n个数是几 保证答案不溢出 直接线性筛模拟即可 #include<cstdio> #inclu ...
- BZOJ 2190 SDOI 2008 仪仗队 线性欧拉筛
标题效果:有一个格子组件图,假设三个人在一条直线上,那么第一个人将不会看到第三人.现在,有一个人站在(1,1)在.我问他是否能看到n*n的人数的矩阵. 思考:如果你想站(1,1)这名男子看到了一个立场 ...
- The Embarrassed Cryptographer POJ - 2635 同余模+高精度处理 +线性欧拉筛(每n位一起处理)
题意:给出两数乘积K(1e100) 和 一个数L(1e6) 问有没有小于L(不能等于)的素数是K的因数 思路:把数K切割 用1000进制表示 由同余模公式知 k%x=(a*1000%x+b* ...
随机推荐
- mysql 视图 触发器 存储过程 函数事务 索引
mysql 视图 触发器 存储过程 函数事务 索引 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当 ...
- Codeforces 577A - Multiplication Table
Let's consider a table consisting of n rows and n columns. The cell located at the intersection of i ...
- 1080 Graduate Admission
大致题意就是有N个学生,有M个学校,每个学校的名额都是正整数.每个学生可以填K个学校志愿,N个学生一起排名以后,排名高的学生先挑学校,不保护一志愿. 题目要求: 首先,把所有学生按总成绩SUM(GE+ ...
- jdk1.8的HashMap和ConcurrentHashMap
原文地址:https://my.oschina.net/pingpangkuangmo/blog/817973 本文针对jdk1.8的ConcurrentHashMap 1 1.8的HashMap设计 ...
- 跨域 node git
promise 异步回调地狱:就是多个异步请求嵌套的表现 瑕疵:后期维护难 解决:通过promise技术 什么是promise:就是一种异步编程的解决方案 有三个状态:进行中.成功了,失败了 var ...
- 将本地文件git到远程github仓库
使用git管理项目是开发人员必备的技能,下面就介绍如何使用git将本地文件管理并且同步到github上面. 小白可以参考 创建SSH-key并且在github中添加认证 在本地用户的.ssh文件下面生 ...
- HTML之<meta>标签全解
一.定义 元素可提供相关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词等等. 标签位于文档的头部<head></head>标签内 ...
- struts2中的constant常量配置
struts2中的constant配置详解 本文主要讲解一下struts2中的constant常量配置,内容主要来自于互联网的整理.<?xml version="1.0" e ...
- 一点点学习PS--实战二
本节实战,可以学到如何制作gif动图,制作搜狗输入法皮肤 1.工具使用 (1)滤镜--液化--膨胀:这里是制作出猫咪打呼时肚子和气泡胀大的效果 (2)图像--画布大小:可裁剪画布到指定像素,并且裁剪指 ...
- 如何在Word中排出漂亮的代码,去除回车符,去除拼写检查
这位博主写到很到位,这里补充一下在VBA里用模块的部分. https://blog.csdn.net/code4101/article/details/41802715 1.放代码的方式是贴纯文本. ...