Educational Codeforces Round 12 D. Simple Subset

来源:互联网 发布:淘宝起名字 编辑:程序博客网 时间:2024/06/11 08:41

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 froma by removing some (possibly all) elements of it.

Input

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

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

Output

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

On the second line print m integers bl — the elements of the simple subset of the arraya 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
22 3
Output
23 2
Input
22 2
Output
12
Input
32 1 1
Output
31 1 2
Input
283 14
Output
214 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
原创粉丝点击