Advertisement
Malinovsky239

D (Region 2012)

Jan 21st, 2012
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5.  
  6. #define N 205
  7. #define M int(1e6)
  8.  
  9. using namespace std;
  10.  
  11. int a[N], n, top = 1;
  12. bool was[M], dp[M];
  13. vector<int> ans;
  14.  
  15. bool dfs(int mask) {
  16.     if (was[mask])
  17.         return dp[mask];   
  18.  
  19.     was[mask] = true;
  20.    
  21.     for (int i = 0, j = 1; i < n; i++, j *= 2) {
  22.         if ((mask & j) == 0) {
  23.             int delta = 0;         
  24.             for (int k = 0, bit = 1; k < n; k++, bit *= 2) {
  25.                 if (mask & bit) {
  26.                     if (!delta)
  27.                         delta = abs(a[k] - a[i]);
  28.                     else
  29.                         delta = __gcd(delta, abs(a[k] - a[i]));
  30.                 }  
  31.             }
  32.             if (delta == 1) continue;
  33.  
  34.             if (!dfs(mask | j)) {                      
  35.                 dp[mask] = true;
  36.                 return true;
  37.             }          
  38.         }
  39.     }  
  40.     dp[mask] = false;
  41.     return false;
  42. }
  43.  
  44. int main() {
  45.     freopen("game.in", "r", stdin);
  46.     freopen("game.out", "w", stdout);
  47.  
  48.     cin >> n;
  49.  
  50.     for (int i = 0; i < n; i++)
  51.         cin >> a[i], top *= 2;
  52.  
  53.     if (n == 1) {
  54.         cout << 1 << endl << a[0] << endl;
  55.         return 0;
  56.     }
  57.  
  58.     if (n > 15) {
  59.         int gcd = a[0];
  60.         for (int i = 1; i < n; i++)
  61.             gcd = __gcd(a[i], gcd);
  62.  
  63.         if (gcd > 1) {
  64.             if (n % 2 == 0) {
  65.                 cout << 0 << endl;
  66.                 return 0;
  67.             }
  68.  
  69.             cout << n << endl;
  70.             for (int j = 0; j < n; j++)
  71.                 cout << a[j] << " ";
  72.                     cout << endl;
  73.             return 0;                  
  74.         }              
  75.  
  76.         cout << 0 << endl;
  77.         return 0;
  78.     }
  79.  
  80.     was[top - 1] = true;
  81.     dp[top - 1] = false;       
  82.  
  83.     for (int i = 1, j = 0; i < top; i *= 2, j++) {
  84.         if (!dfs(i)) {
  85.             ans.push_back(j);          
  86.         }
  87.     }
  88.  
  89.     cout << ans.size() << endl;
  90.     for (int i = 0; i < ans.size(); i++)
  91.         cout << a[ ans[i] ] << " ";
  92.     cout << endl;
  93.    
  94.  
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement