7025 Yes, Prime Minister 杭电多校(2021“MINIEYE杯”中国大学生算法设计超级联赛6)

Posted 你可以当我没名字

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了7025 Yes, Prime Minister 杭电多校(2021“MINIEYE杯”中国大学生算法设计超级联赛6)相关的知识,希望对你有一定的参考价值。

https://acm.hdu.edu.cn/showproblem.php?pid=7025

Yes, Prime Minister

*Time Limit: 10000/10000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1 Accepted Submission(s): 0
*

Problem Description

Mr. Hacker’s Department of Administrative Affairs (DAA) has infinite 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 ∑ri=li 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 fire 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 first line contains an integer T(1≤T≤106) - the number of test cases. Then T test cases follow.

The first and only line of each test case contains one integer xi(−107≤xi≤107) - 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.

Sample Input

10
-2
-1
0
1
2
3
4
5
6
7

Sample Output

6
4
3
2
1
1
2
1
2
1

Source

2021“MINIEYE杯”中国大学生算法设计超级联赛(6)

代码与解释

解释一下

如果 l<=0 ,那么[l,r]的区间和=[-l+1,r]的区间和,且r>=-l+1>0

假设-2,那么[-2,3]的区间和=[3,3],(前面的3=-(-2)+1)

公式是怎么来的

在l<=0的情况,我们相当于求[-l+1,r]的区间和,根据等差数列求和公式na1+n*(n-1)/2*d(r+l-1)(-l+1)+(r+l-1)(r+l-2)/2,化简即可得到(l+r)(r-l+1)/2

l > 0 时,若 r − l ≥ 2,则 (l+r)/2(r−l+1)/2 之中必有一个是大于 1 的整数。区间和必然能被拆分成两个因数的乘积。所以这样的满足和为素数的区间长度必然不长于 2。

l>0时,公式变成(r-l)*l+(r-l)(r-l-1)/2,化简得(r-l)(r+l+1)/2,如果r-l>2,那么(r-l)(r+l+1)/2肯定可以由两个数相乘得到(这两个数都不是1),所以满足l>0的情况,r-l最大就是2,不会更多了

埃氏筛法和线性筛法略过

如果 x ≤ 0,则候选答案为:

  • 最小的 y(y ≥ 1 − x),满足 y 是质数,区间为 [−y + 1, y]。
  • 最小的 z(z ≥ 2 − x),满足 2z − 1 是质数,区间为 [−z + 2, z]。

如果 x > 0,则候选答案为:

  • [x, x]。
  • [x, x + 1]。
  • [x − 1, x]。
  • 最小的 y(y ≥ x),满足 y 是质数,区间为 [−y + 1, y]。
  • 最小的 z(z ≥ x),满足 2z − 1 是质数,区间为 [−z + 2, z]。

这些在数轴上画个图就懂了,就是要弄成左边是负数的形式

//TODO 明天再写

标程

#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);//x>0时的候选最后一个,满足2z-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);//[-y+1,y]之间差了2*y 
		t=*lower_bound(rec2.begin(),rec2.end(),x);
		ans=min(ans,2*t-1);//[-y+2,y]之间差了2*y-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);
}

以上是关于7025 Yes, Prime Minister 杭电多校(2021“MINIEYE杯”中国大学生算法设计超级联赛6)的主要内容,如果未能解决你的问题,请参考以下文章

P4752 Divided Prime

错误:error: failed to push some refs to 'https://github.com/pzq7025/KG.git'的解决办法

Tensorflow 实战Google深度学习框架 第五章 5.2.1Minister数字识别 源代码

angular-安装primeng

luogu 7月月赛 #A P4752 Divided Prime

Codeforces Round #231 (Div. 2) E.Lightbulb for Minister