Educational Codeforces Round 12 D. Simple Subset

65 篇文章 0 订阅
34 篇文章 0 订阅

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 a with 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


题意:给你一个序列,问你能保证序列中每两个元素和为质数的最长的子序列有多长。

分析:把1要单独考虑,如果有多于两个1就去找剩下有没有a[i]+1是质数的,如果有就加进来没有就输出所有的1;其他情况就看能不能找到两个数使其和为素数。


#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int n,tot,cnt,prim[200000],a[1005];
bool jud[2000005],f[2000005];
int main()
{
	jud[1] = true;
	for(int i = 2;i <= 2000000;i++)
	 if(!jud[i])
	 {
		for(int j = i*2;j <= 2000000;j+=i) jud[j] = true;
		prim[++tot] = i; 
	 }
	cin>>n;
	for(int i = 1;i <= n;i++) cin>>a[i];
	sort(a+1,a+1+n);
	for(int i = 1;i <= n;i++)
	{
		if(a[i] == 1) cnt++;
		f[a[i]] = true;
	}
	if(cnt > 1)
	{
		for(int i = 2;i <= tot;i++) 
		 if(f[prim[i] - 1]) 
		 {
		 	cout<<cnt+1<<endl;
		 	for(int j = 1;j <= cnt;j++) cout<<1<<" ";
		 	cout<<prim[i]-1<<endl;
		 	return 0;
		 }
		cout<<cnt<<endl;
		for(int j = 1;j <= cnt;j++) cout<<1<<" ";
		return 	0;
	} 
	else 
	{
		for(int i = 1;i <= n;i++)
		 for(int j = 2;j <= tot;j++)
		  if(prim[j] > a[i] && f[prim[j]-a[i]])
		  {
		  	  cout<<2<<endl;
			  cout<<a[i]<<" "<<prim[j]-a[i]<<endl;
			  return 0;				
		  }
	   for(int i = 1;i <= n;i++)
	    if(!jud[a[i]])
		{
			cout<<1<<endl;
			cout<<a[i]<<endl;
			return 0;
		}
	   cout<<1<<endl; 
	   cout<<a[1]<<endl;
	   return 0;
	}	
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值