Codeforces 665D 模拟

Simple Subset
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i,  j) (1  ≤ i  <  j ≤ k), xi  +  xj is a prime.

You are given an array a with n positive integers a1,  a2,  ...,  an (not necessary distinct). You want to find a simple subset of the array awith the maximum size.

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Let's define a subset of the array a as a tuple that can be obtained from a by removing some (possibly all) elements of it.

Input

The first line contains integer n (1 ≤ n ≤ 1000) — the number of integers in the array a.

The second line contains n integers ai (1 ≤ ai ≤ 106) — the elements of the array a.

Output

On the first line print integer m — the maximum possible size of simple subset of a.

On the second line print m integers bl — the elements of the simple subset of the array a with the maximum size.

If there is more than one solution you can print any of them. You can print the elements of the subset in any order.

Examples
input
2
2 3
output
2
3 2
input
2
2 2
output
1
2
input
3
2 1 1
output
3
1 1 2
input
2
83 14
output
2
14 83






题意:n个数,找出最多的数,使得任意两个数的和为素数。




  如果不考虑1——
1,最多只有1个偶数
2,最多只有1个奇数
3,和为素数

如果考虑1——
可以存在1,一旦存在1
,可以无限个1
可以最多加上一个偶数
不可以有其他奇数  



转化过来就是,如果1的个数大于1,那就把所有的1加进去,然后任意加一个和1相加之后为素数的偶数或者不加 。


如果1的个数不为1,那么随便找两个和为素数的奇数和偶数即可。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<set>
using namespace std;
multiset<int>sp;
int a[1005],b[1005],vis[2000005];
int main(){
	int n,i,num=1,j;
	for(i=2;i<=2000000;i++){
		if(vis[i])continue;
		for(j=2;j<=sqrt(i);j++){
			if(i%j==0){
				vis[i]=1;
				break;
			}
		}
		if(vis[i]){
			for(j=i*2;j<=2000000;j+=i)vis[j]=1;
		}
	}
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		scanf("%d",&a[i]);
		sp.insert(a[i]);
	}
	multiset<int>::iterator it,it1;
	if(sp.count(1)>1){
		for(i=1;i<=sp.count(1);i++)b[i]=1;
		num=sp.count(1);
		for(it=sp.begin();it!=sp.end();it++){
			if(*it>1&&!vis[*it+1]){
				b[++num]=*it;
				break;
			}
		}
	}
	else{
		int flag=0;
		num=0;
		for(it=sp.begin();it!=sp.end();it++){
			it1=sp.begin();
			it1++;
			for(;it1!=sp.end();it1++){
				if(!vis[*it+*it1]){
					b[1]=*it;
					b[2]=*it1;
					num=2;
					flag=1;
					break;
				}
			}
			if(flag)break;
		}
		if(flag==0){
			it=sp.begin();
			num=1;
			b[1]=*it;
		}
	}
	cout<<num<<endl;
	for(i=1;i<=num;i++){
		cout<<b[i];
		if(i!=num)cout<<" ";
		else cout<<endl;
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值