codeforces 665D Simple Subset


D. 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

一个考思路的题

题意:

   给你一个数列a,找出a的一个最长子序列  满足子序列的任意两个数相加都为素数   如果结果有多种输出任意一种即可

多写几组数据可以看出除1外任意3个数都不可能组成这样的数列    但是如果全为1任何两个相加都为2是素数  所以应该找到1的个数

1.如果1的个数大于等于2  输出所有1的 和 一个加1是素数的数   如果没有这样的数 输出所有的1

2.如果1的个数小于2  找两个相加为素数的数   如果没有就随便输出一个数(不能输出0)


代码:


#include<stdio.h>
int a[1005];
bool s[2000005]= {0};
int main()
{
    int n;
    s[0]=1;
    s[1]=1;
    for(int i=2; i<=2000000; i++)
    {
        for(int j=2*i; j<=2000000; j+=i)
        {
            s[j]=1;
        }
    }
    while(~scanf("%d",&n))
    {
        int k=0,pos,flag=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            if(a[i]==1)
            {
                k++;
            }
        }
        if(k>=2)
        {
            for(int i=1; i<=n; i++)
            {
                if(a[i]!=1&&s[a[i]+1]==0)
                {
                    pos=a[i];
                    flag=1;
                    break;
                }
            }
            if(flag==1)
            {
                printf("%d\n",k+1);
                for(int i=1;i<=k;i++)
                {
                    printf("1 ");
                }
                printf("%d\n",pos);
            }
            else
            {
                printf("%d\n",k);
                for(int i=1;i<k;i++)
                {
                    printf("1 ");
                }
                printf("1\n");
            }
        }
        else
        {
            int f;
            for(int i=1;i<=n;i++)
            {
                for(int j=i+1;j<=n;j++)
                {
                    if(s[a[i]+a[j]]==0)
                    {
                        pos=a[i];
                        f=a[j];
                        flag=1;
                        break;
                    }
                }
            }
            if(flag==1)
            {
                printf("2\n");
                printf("%d %d\n",pos,f);
            }
            else if(flag==0)
            {
                printf("1\n");
                printf("%d\n",a[1]);
            }
        }
    }
}




你可能感兴趣的:(codeforces 665D Simple Subset)