Codeforces1150C 构造尽可能多的素数前缀和

https://codeforces.com/contest/1150/problem/C
2 1 2 2 2 2 2 2 1 1 1 1 1
会发现第一个是2质数
第二前缀和是3
2 3 5 7 9 11 13 15 16 17 18 19 20
为什么呢??2和从3开始 到n的前缀和
奇+偶=奇得到第一个3
一直+2 对应值2的前缀和为奇数
到的1范围后 偶 奇 偶 奇交替
构造尽可能多的从3开始的奇数前缀和 即可能的素数前缀和
素数在2之后从奇数中产生

使前缀和奇数尽可能多意味着素数尽可能多

#include<bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false)
#define ll long long
#define mp make_pair
#define fi first
#define se second
#define pb emplace_back
#define pii pair<int,int>
#define all(n) (n).begin(),(n).end()
#define rep(i,a,n) for (int i=a;i<=n;i++)
typedef vector<int> vi;
typedef set<int> si;
const int maxn = 2e5+5;
const ll mod = 998244353;
const int inf = 0x3f3f3f3f;


int t;
int main(){		
	IO;
	int n,c1=0,c2=0;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>t;
		if(t==1)
			c1++;
		else
			c2++;
	}
	if(c1 && c2)	//1 2都出现 起码有1个 
	{
		cout<<2<<" ";
		c2--;
		cout<<1<<" ";
		c1--;
		rep(i,1,c2)
			cout<<2<<" ";
		
		rep(i,1,c1)
			cout<<1<<" ";
	}
	else if(c1==0)
	{
		rep(i,1,c2)
			cout<<2<<" ";
	}
	else	//c2==0
	{
		rep(i,1,c1)
			cout<<1<<" ";
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值