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.
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.
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.
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个偶数
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;
}