Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #define N 205
- #define M int(1e6)
- using namespace std;
- int a[N], n, top = 1;
- bool was[M], dp[M];
- vector<int> ans;
- bool dfs(int mask) {
- if (was[mask])
- return dp[mask];
- was[mask] = true;
- for (int i = 0, j = 1; i < n; i++, j *= 2) {
- if ((mask & j) == 0) {
- int delta = 0;
- for (int k = 0, bit = 1; k < n; k++, bit *= 2) {
- if (mask & bit) {
- if (!delta)
- delta = abs(a[k] - a[i]);
- else
- delta = __gcd(delta, abs(a[k] - a[i]));
- }
- }
- if (delta == 1) continue;
- if (!dfs(mask | j)) {
- dp[mask] = true;
- return true;
- }
- }
- }
- dp[mask] = false;
- return false;
- }
- int main() {
- freopen("game.in", "r", stdin);
- freopen("game.out", "w", stdout);
- cin >> n;
- for (int i = 0; i < n; i++)
- cin >> a[i], top *= 2;
- if (n == 1) {
- cout << 1 << endl << a[0] << endl;
- return 0;
- }
- if (n > 15) {
- int gcd = a[0];
- for (int i = 1; i < n; i++)
- gcd = __gcd(a[i], gcd);
- if (gcd > 1) {
- if (n % 2 == 0) {
- cout << 0 << endl;
- return 0;
- }
- cout << n << endl;
- for (int j = 0; j < n; j++)
- cout << a[j] << " ";
- cout << endl;
- return 0;
- }
- cout << 0 << endl;
- return 0;
- }
- was[top - 1] = true;
- dp[top - 1] = false;
- for (int i = 1, j = 0; i < top; i *= 2, j++) {
- if (!dfs(i)) {
- ans.push_back(j);
- }
- }
- cout << ans.size() << endl;
- for (int i = 0; i < ans.size(); i++)
- cout << a[ ans[i] ] << " ";
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement