codeforces 655D (构造)

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



题意:给出n个数,问最多能够找到多少个数使得两两相加是质数.

因为奇数+奇数和偶数+偶数都不可能是质数,所以必然是1个奇数1个偶数,也就是最

多能够选2个.但是1的情况很特殊.所以先统计出1的数量.

如果1的数量大于等于2,直接都拿1;否则暴力枚举奇数和偶数,判断两个数相加是不

是质数.

注意全是奇数或者全是偶数的情况.

#include <bits/stdc++.h>
using namespace std;
#define maxn 1111

int odd[maxn], even[maxn];
int cnt1, cnt2;
int n;
bool is_prime[2111111];

void init () {
    memset (is_prime, 1, sizeof is_prime);
    is_prime[0] = is_prime[1] = 0;
    for (int i = 2; i < 2111111; i++) {
        if (is_prime[i]) {
            for (int j = i+i; j < 2111111; j += i)
                is_prime[j] = 0;
        }
    }
}


int main () {
    init ();
    //freopen ("in.txt", "r", stdin);
    while (cin >> n) {
        cnt1 = cnt2 = 0;
        int num;
        for (int i = 0; i < n; i++) {
            cin >> num;
            if (num&1) {
                odd[cnt1++] = num;
            }
            else
                even[cnt2++] = num;
        }
        int cnt = 0;
        sort (odd, odd+cnt1);
        sort (even, even+cnt2);
        for (int i = 0; i < cnt1; i++) if (odd[i] == 1)
            cnt++;
        int more = 0;
        if (cnt >= 2) {
            for (int i = 0; i < cnt2; i++) {
                if (is_prime[1+even[i]]) {
                    more = even[i];
                    cnt++;
                    break;
                }
            }
            cout << cnt << "\n";
            if (more) {
                cout << more << " ";
            }
            cnt -= (more > 0);
            for (int i = 0; i < cnt; i++) {
                cout << 1 << (i == cnt-1 ? "\n" : " ");
            }
            continue;
        }
        for (int i = 0; i < cnt1; i++) {
            for (int j = 0; j < cnt2; j++) {
                if (is_prime[odd[i]+even[j]]) {
                    cout << 2 << "\n" << odd[i] << " " << even[j] << "\n";
                    goto out;
                }
            }
        }
        cout << 1 << "\n";
        cout << (cnt1 ? odd[0] : even[0]) << "\n";
        out: ;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值