Problem - D - Codeforces
题意:
思路:
这种题一定要从小数据入手,不然很有可能走歪思路
先考虑n = 1的情况,直接输出即可
然后是n = 2的情况,如果相加是质数,就输出2个,否则就输出一个
然后是n = 3的情况
看这个集合能不能是3个数
2 3 5不行
4 5 8不行
注意到三个数一定是奇 偶 奇,只要没有1,就一定不行
想到这就很简单了,分类讨论即可
Code:
#include <bits/stdc++.h>#define int long longusing i64 = long long;constexpr int N = 2e3 + 10;
constexpr int M = 2e6 + 10;
constexpr int P = 2600;
constexpr i64 Inf = 1e18;
constexpr int mod = 1e9 + 7;
constexpr double eps = 1e-6;int n;
int len = 0;
int a[N];
int vis[M], prime[M];void p_init(int n) {for (int i = 2; i <= n; i ++) {if (!vis[i]) prime[++len] = i;for (int j = 1; prime[j] <= n / i; j ++) {vis[i * prime[j]] = 1;if (i % prime[j] == 0) break;}}
}
void solve() {std::cin >> n;int cnt1 = 0;for (int i = 1; i <= n; i ++) {std::cin >> a[i];if (a[i] == 1) cnt1 ++;}std::sort(a + 1, a + 1 + n);if (cnt1) {for (int i = cnt1 + 1; i <= n; i ++) {if (!vis[a[i] + 1]) {std::cout << cnt1 + 1 << "\n";for (int i = 1; i <= cnt1; i ++) std::cout << 1 << " ";std::cout << a[i] << "\n";return;}}if (cnt1 >= 2) {std::cout << cnt1 << "\n";for (int i = 1; i <= cnt1; i ++) std::cout << 1 << " \n" [i == cnt1];return;}}for (int i = 1; i <= n; i ++) {for (int j = i + 1; j <= n; j ++) {if (!vis[a[i] + a[j]]) {std::cout << 2 << "\n";std::cout << a[i] << " " << a[j] << "\n";return;}}}std::cout << 1 << "\n" << a[1] << "\n";
}
signed main() {std::ios::sync_with_stdio(false);std::cin.tie(nullptr);int t = 1;p_init(2e6);while (t--) {solve();}return 0;
}