mr_dot_convict

Indicium-codejam-mr.convict

Apr 5th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.25 KB | None | 0 0
  1. #include      <bits/stdc++.h> /*{{{*/
  2. #include      <ext/pb_ds/assoc_container.hpp>
  3. #include      <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6.  
  7. #ifndef CONVICTION
  8. #pragma GCC       optimize ("Ofast")
  9. #pragma GCC       optimize ("unroll-loops")
  10. #pragma GCC       target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  11. #endif
  12.  
  13. #define IOS       ios_base::sync_with_stdio(false); cin.tie (nullptr)
  14. #define PREC      cout.precision (10); cout << fixed
  15. #define X         first
  16. #define Y         second
  17. #define sz(x)     (int)x.size()
  18. #define fr(i,x,y) for (int i = (int)x; i <= (int)y; ++i)
  19. #define rv(i,x,y) for (int i = (int)x; i >= (int)y; --i)
  20. #define bcnt(x)   __builtin_popcount(x)
  21. #define bcntll(x) __builtin_popcountll(x)
  22. #define bg(x)     " [ " << #x << " : " << (x) << " ] "
  23. #define un(x)     sort(x.begin(), x.end()), \
  24.   x.erase(unique(x.begin(), x.end()), x.end())
  25. using   ll  =     long long;
  26. using   ull =     unsigned long long;
  27. using   ff  =     long double;
  28. using   pii =     pair<int,int>;
  29. using   pil =     pair<int,ll>;
  30. using   pll =     pair<ll,ll>;
  31. using   vi  =     vector <int>;
  32. using   vl  =     vector<ll>;
  33. using   vvi =     vector <vi>;
  34. using   vvl =     vector <vl>;
  35. using   vp  =     vector <pii>;
  36. using   vvp =     vector <vp>;
  37. typedef tree
  38. < int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  39. ordered_set;
  40.  
  41. struct chash {
  42.   int operator () (pii x) const { return x.X*31 + x.Y; }
  43. };
  44. gp_hash_table <pii, int, chash> gmp;
  45.  
  46. #if __cplusplus > 201103L
  47. seed_seq seq{
  48.   (uint64_t) chrono::duration_cast<chrono::nanoseconds>
  49.     (chrono::high_resolution_clock::now().time_since_epoch()).count(),
  50.     (uint64_t) __builtin_ia32_rdtsc(),
  51.     (uint64_t) (uintptr_t) make_unique<char>().get()
  52. };
  53. mt19937 rng(seq); //uniform_int_distribution<int> (l, h)(rng); //[low, high]
  54. #else
  55. auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
  56. mt19937 rng(seed);
  57. #endif
  58.  
  59. #define debug(args...) { \
  60.   /* WARNING : do NOT compile this debug func calls with following flags: // \
  61.    * // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
  62.   string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  63.   stringstream _ss(_s); \
  64.   istream_iterator<string> _it(_ss); err(_it, args); \
  65. }
  66. void err(istream_iterator<string> it) {
  67.   it->empty(); cerr << " (Line : " << __LINE__ << ")" << '\n';
  68. }
  69. template<typename T, typename... Args>
  70. void err(istream_iterator<string> it, T a, Args... args) {
  71.   cerr << fixed << setprecision(15)
  72.     << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  73.   err(++it, args...);
  74. }
  75. //         __                                           __
  76. //        (**)                                         (**)
  77. //        IIII                                         IIII
  78. //        ####                                         ####
  79. //        HHHH     Madness comes, and madness goes     HHHH
  80. //        HHHH    An insane place, with insane moves   HHHH
  81. //        ####   Battles without, for battles within   ####
  82. //     ___IIII___        Where evil lives,          ___IIII___
  83. //  .-`_._"**"_._`-.      and evil rules         .-`_._"**"_._`-.
  84. // |/``  .`\/`.  ``\|    Breaking them up,      |/``  .`\/`.  ``\|
  85. // `     }    {     '  just breaking them in    `     }    {     '
  86. //       ) () (  Quickest way out, quickest way wins  ) () (
  87. //       ( :: )      Never disclose, never betray     ( :: )
  88. //       | :: |   Cease to speak or cease to breath   | :: |
  89. //       | )( |        And when you kill a man,       | )( |
  90. //       | || |          you're a murderer            | || |
  91. //       | || |             Kill many                 | || |
  92. //       | || |        and you're a conqueror         | || |
  93. //       | || |        Kill them all ... Ooh..        | || |
  94. //       | || |           Oh you're a God!            | || |
  95. //       ( () )                       -Megadeth       ( () )
  96. //        \  /                                         \  /
  97. //         \/                                           \/
  98. /*}}}*/
  99. /*****************************************************************************/
  100. //Don’t practice until you get it right. Practice until you can’t get it wrong
  101.  
  102. const int N = 50;
  103. int n, k;
  104. vector <ll> stk;
  105. int mat[N][N];
  106.  
  107. int rec (int x, int y, ll curmask, int trc) {
  108.   if (x == n) {
  109.     return trc == k;
  110.   }
  111.   for (int j = 0, down = y == n - 1, go = 0; j < n; ++j) {
  112.     if (((1ll << j) & curmask) || ((1ll << j) & stk[y])) continue;
  113.  
  114.     stk[y] ^= (1ll << j), curmask ^= (1ll << j), trc += (x == y ? j + 1 : 0);
  115.     if (rec(x + down, down ? 0 : y + 1, down ? 0 : curmask, trc)) {
  116.       mat[x][y] = j + 1;
  117.       return 1;
  118.     }
  119.     trc -= (x == y ? j + 1 : 0), curmask ^= (1ll << j), stk[y] ^= (1ll << j);
  120.   }
  121.   return 0;
  122. }
  123.  
  124. signed main() {
  125.   IOS; PREC;
  126.  
  127.   int tc = 50;
  128.   cin >> tc;
  129.   for (int T = 1; T <= tc; ++T) {
  130.     cout << "Case #" << T << ": ";
  131.     n = 5, k = 5 + T % 21;
  132.     cin >> n >> k;
  133.     stk.assign(n, 0);
  134.     if (rec(0, 0, 0, 0)) {
  135.       cout << "POSSIBLE\n";
  136.       for (int i = 0; i < n; ++i) {
  137.         for (int j = 0; j < n; ++j) {
  138.           cout << mat[i][j] << ' ';
  139.         }
  140.         cout << '\n';
  141.       }
  142.     }
  143.     else cout << "IMPOSSIBLE\n";
  144.   }
  145.  
  146.   return EXIT_SUCCESS;
  147. }
Add Comment
Please, Sign In to add comment