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
写之前先TM的
题意:求一个数num使它a+num==b+num,和为素数,并且之间没有素数
思路:先线性欧拉筛
再枚举,因为a+num==b+num 和都是素数,所以直接枚举素数,看prime[i]-a==prime[i+1]-b是否相等,
相等prime[i]-a就是结果
咱也不知道咱也不敢问,c++ 20000010就能过,java就是ML,MM的
所以啊咱一点一点缩小范围,就是目前的14000010
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static int k=0;
static final int max=14000010;
static boolean vis[]=new boolean[max];
static int prime[]=new int[910078];
public static void is_Prime(){
Arrays.fill(vis, true);
vis[0]=vis[1]=false;
for(int i=2;i<max;i++){
if(vis[i])
prime[k++]=i;
for(int j=0;j<k&&i*prime[j]<max;j++){
vis[i*prime[j]]=false;
if(i%prime[j]==0)
break;
}
}
}
public static void main(String[] args) {
is_Prime();
//System.out.println(k);
Scanner scan=new Scanner(System.in);
int t=scan.nextInt();
int j=1;
while(t!=0){
int a=scan.nextInt();
int b=scan.nextInt();
if(a>b){
int temp=0;
temp=a;
a=b;
b=temp;
}
int ans=-1;
for(int i=0;i<k-1;i++){
if(prime[i]>=a&&prime[i+1]>=b){
if(prime[i]-a==prime[i+1]-b){
ans=prime[i]-a;
break;
}
}
}
System.out.print("Case "+j+": ");
System.out.println(ans);
t--;
j++;
}
}
}