Guest User

Untitled

a guest
Sep 7th, 2014
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. #include <functional>
  2. #include <unordered_set>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <numeric>
  6. #include <cassert>
  7. #include <cstdlib>
  8. #include <string>
  9. #include <cstdio>
  10. #include <vector>
  11. #include <ctime>
  12. #include <queue>
  13. #include <set>
  14. #include <map>
  15. using namespace std;
  16. #define forn(i, n) for (int i = 0; i < (int)(n); ++i)
  17. #define fore(i, b, e) for (int i = (int)(b); i <= (int)(e); ++i)
  18. #define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
  19. #define mp make_pair
  20. #define pb push_back
  21. #define fi first
  22. #define se second
  23. #define all(x) (x).begin(), (x).end()
  24. typedef vector<int> vi;
  25. typedef pair<int, int> pii;
  26. typedef long long i64;
  27. typedef unsigned long long u64;
  28. const int inf = 1e9+100500;
  29.  
  30. int n, m;
  31. i64 setbit(i64 x, int i, int j) {
  32.     return x | 1ll<<(i*m+j);
  33. }
  34.  
  35. vector<i64> vm;
  36. vector<i64> vh;
  37. bool cz[1<<20];
  38.  
  39. void genmasks() {
  40.     vm.clear();
  41.     vh.clear();
  42.     forn(i, 1<<n) {
  43.         if (__builtin_popcount(i) <= 1) continue;
  44.         i64 m = 0;
  45.         forn(j, n) if (i&(1<<j)) m |= 1ll<<(j*::m);
  46.         vm.pb(m);
  47.     }
  48.  
  49.     forn(i, 1<<m) if (i) vh.pb(i);
  50.  
  51.     stable_sort(all(vm), [](int x, int y) { return __builtin_popcount(x) < __builtin_popcount(y); });
  52.     forn(i, vm.size()) {
  53.         if (i == 0 || __builtin_popcount(vm[i]) < __builtin_popcount(vm[i-1]))
  54.             cz[i] = 1;
  55.         else
  56.             cz[i] = 0;
  57.     }
  58.     sort(all(vh), [](int x, int y) { return __builtin_popcount(x) < __builtin_popcount(y); });
  59. }
  60.  
  61. void out(i64 x) {
  62.     forn(i, n) {
  63.         forn(j, m) cout << (0 != (x&1ll<<(i*m+j)));
  64.         cout << endl;
  65.     }
  66.     cout << endl;
  67. }
  68.  
  69. unordered_set<i64> win, lose;
  70.  
  71. int a[20];
  72.  
  73. i64 sort_cols(i64 mask) {
  74.     forn(i, m) a[i] = 0;
  75.     forn(i, n) {
  76.         forn(j, m) a[j] = a[j]*2+(0 != (mask&(1<<j)));
  77.         mask >>= m;
  78.     }
  79.     sort(a, a+m);
  80.     forn(i, n) {
  81.         mask <<= m;
  82.         forn(j, m) {
  83.             if (a[j] & 1) {
  84.                 mask |= 1<<j;
  85.             }
  86.             a[j] >>= 1;
  87.         }
  88.     }
  89.     return mask;
  90. }
  91.  
  92. i64 sort_mask(i64 mask) {
  93.     forn(i, n) {
  94.         a[i] = mask&((1ll<<m)-1);
  95.         mask >>= m;
  96.     }
  97.     assert(mask == 0);
  98.     sort(a, a+n, [](int x, int y) { return __builtin_popcount(x) < __builtin_popcount(y); });
  99.     forn(i, n) {
  100.         mask <<= m;
  101.         mask ^= a[i];
  102.     }
  103.     return sort_cols(mask);
  104. }
  105.  
  106. int go(i64 x) {
  107.     x = sort_mask(x);
  108.     if (win.count(x)) return 1;
  109.     if (lose.count(x)) return 0;
  110.  
  111.     forn(i, vh.size()) forn(j, n) {
  112.         if (x == 0) {
  113.             if (j) break;
  114.             if (vh[i]&(vh[i]+1)) break;
  115.         }
  116.         i64 t = (i64)vh[i]<<(j*m);
  117.         if (x&t) continue;
  118.         if (!go(x|t)) {
  119.             win.insert(x);
  120.             if (x == 0) {
  121.                 out(t);
  122.             } else {
  123.                 return 1;
  124.             }
  125.         }
  126.     }
  127.     forn(i, vm.size()) forn(j, m) {
  128.         if (x == 0) {
  129.             if (j) break;
  130.             if (!cz[i]) break;
  131.         }
  132.         i64 t = vm[i]<<j;
  133.         if (x&t) continue;
  134.         if (!go(x|t)) {
  135.             win.insert(x);
  136.             if (x == 0) {
  137.                 out(t);
  138.             } else {
  139.                 return 1;
  140.             }
  141.         }
  142.     }
  143.     if (x == 0) return win.count(x);
  144.     lose.insert(x);
  145.     return 0;
  146. }
  147.  
  148. int main() {
  149. #ifdef HOME
  150. //     freopen("input.txt", "r", stdin);
  151. #endif
  152.  
  153.     clock_t ct;
  154.     while (1) {
  155.         cin >> n >> m;
  156.         ct = clock();
  157.         genmasks();
  158.         win.clear();
  159.         lose.clear();
  160.         cout << go(0) << endl;
  161.         cout << "Time: " << (clock() - ct) / 1000 << " ms" << endl;
  162.     }
  163.  
  164. #ifdef HOME
  165.     cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl;
  166. #endif
  167.     return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment