Prime Friend

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3914    Accepted Submission(s): 810

Problem Description
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

#没写过素数表,在微信上看见这个题的解题思路，先模仿写了素数表，后面的解题代码都类似 
 题意：  a+x b+x 满足都是素数，之间不能有素数存在  所以 isprimes[i]>=a&&isprimes[i+1]>=b 限制了这个条件的数据 反其道而行之
/*
HDU 3823
*/
#include <iostream>
#include <math.h>
#include <string.h>
#include <stdio.h>
typedef long long ll;
using namespace std;
const int max_value = 2e7;
static bool is_prime[max_value+1];
static int isprimes[max_value];

using namespace std;

int _getprimeTable()
{
int primesSize = 0;
memset(is_prime,true,sizeof(is_prime));
int t = sqrt((double)max_value)+1;
for(ll i = 2; i <= t; i++)
{
if(is_prime[i])
{
for(ll j = 2; j <= max_value/i; j++)
{
is_prime[i*j]=false;
}
}
}
for(ll i = 2; i<=max_value; i++)
{
if(is_prime[i])
{
isprimes[primesSize++] = i;
}
}
return primesSize;
}
int main()
{
int len = _getprimeTable();
int n,isprime,count_primes = 1;
scanf("%d",&n);
while(n--)
{
isprime = -1;
int m,k;
scanf("%d%d",&m,&k);
if(m>k)
{
m=m^k;
k=m^k;
m=m^k;
}
for(int i = 0; i<len-1; i++)
{
if(isprimes[i]>=m&&isprimes[i+1]>=k)
{
if(isprimes[i]-m == isprimes[i+1]-k)
{
isprime = isprimes[i]-m;
break;
}
}
}
printf("Case %d: %d\n",count_primes++,isprime);
}
return 0;
}