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.
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
2 3 2
2 2 2
1 2
3 2 1 1
3 1 1 2
2 83 14
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; } }