Mr. Hacker’s Department of Administrative Affffairs (DAA) has infifinite civil servants. Every integer is used
as an id number by exactly one civil servant. Mr. Hacker is keen on reducing overmanning in civil service,
so he will only keep people with consecutive id numbers in
[
l, r
]
and dismiss others.
However, permanent secretary Sir Humphrey’s id number is
x
and he cannot be kicked out so there must
be
l
≤
x
≤
r
. Mr. Hacker wants to be Prime Minister so he demands that the sum of people’s id number
P
r
i
=
l
i
must be a prime number.
You, Bernard, need to make the reduction plan which meets the demands of both bosses. Otherwise, Mr.
Hacker or Sir Humphrey will fifire you.
Mr. Hacker would be happy to keep as few people as possible. Please calculate the minimum number of
people left to meet their requirements.
A prime number
p
is an integer greater than
1
that has no positive integer divisors other than
1
and
p
.
Input
The fifirst line contains an integer
T
(1
≤
T
≤
10
6
)
- the number of test cases. Then
T
test cases follow.
The fifirst and only line of each test case contains one integer
x
i
(
−
10
7
≤
x
i
≤
10
7
)
- Sir Humphrey’s id
number.
Output
For each test case, you need to output the minimal number of people kept if such a plan exists, output
−
1
otherwise.
![](https://img-blog.csdnimg.cn/20210817182453455.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUzNDAzMjM4,size_16,color_FFFFFF,t_70)
#include <bits/stdc++.h>
#define DB double
#define LL long long
#define MST(a,b) memset((a),(b),sizeof(a))
#ifdef _DEBUG_
#define MRK() cout<<"Mark"<<endl;
#define WRT(x) cout<<#x<<" = "<<(x)<<endl;
#else
#define MRK() ;
#define WRT(x) ;
#endif
#define MAXN 20100000
#define MAXM 2010000
#define MOD 998244353 //1000000007
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-5
#define _ 0
using namespace std;
int notprime[MAXN];
vector<int> rec1;
vector<int> rec2;
void init()
{
notprime[0]=notprime[1]=1;
for (int i=2;i<MAXN;i++)
{
if (notprime[i])
continue;
if (i&1)
rec2.push_back(i/2+1);
rec1.push_back(i);
for (int j=2*i;j<MAXN;j+=i)
notprime[j]=1;
}
}
int x;
void work()
{
scanf("%d",&x);
if (x>=0 && !notprime[x])
{
cout<<1<<endl;
return;
}
if (x>=1 && (!notprime[2*x-1] || !notprime[2*x+1]))
{
cout<<2<<endl;
return;
}
int ans=INF;
int t;
if (x>=0)
{
t=*lower_bound(rec1.begin(),rec1.end(),x);
ans=min(ans,2*t);
t=*lower_bound(rec2.begin(),rec2.end(),x);
ans=min(ans,2*t-1);
}
else
{
t=*lower_bound(rec1.begin(),rec1.end(),-x+1);
ans=min(ans,2*t);
t=*lower_bound(rec2.begin(),rec2.end(),-x+2);
ans=min(ans,2*t-1);
}
cout<<ans<<endl;
}
int main()
{
init();
int casenum=1;
scanf("%d",&casenum);
for (int testcase=1;testcase<=casenum;testcase++)
{
work();
}
return ~~(0^_^0);
}