Yes, Prime Minister

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.
#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);
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值