Advertisement
Matrix_code

DP with profile

Mar 22nd, 2021
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. /**
  2.  * Author: Repon Kumar Roy
  3.  * Date: 2021-03-17
  4.  * Task: 2181
  5.  */
  6.  
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9.  
  10. #define ll        long long
  11. #define REP(i, n) for (int i = 0; i < (n); i++)
  12.  
  13. const int mod = 1e9 + 7;
  14. void add(int &a, int b) {
  15.     a += b;
  16.     if (a >= mod) a -= mod;
  17. }
  18. int n, m;
  19.  
  20. vector<int> expand(int mask) {
  21.     vector<int> v;
  22.     REP(i, n) v.push_back(mask % 3), mask /= 3;
  23.     reverse(v.begin(), v.end());
  24.     return v;
  25. }
  26. int compact(vector<int> &v) {
  27.     int sum       = 0;
  28.     REP(i, n) sum = sum * 3 + v[i];
  29.     return sum;
  30. }
  31.  
  32. /* int dfs(int mask, int lay) { */
  33. /*     if(lay == 0) return mask == 0; */
  34. /*     if(dp[mask][lay] != -1) return dp[mask][lay]; */
  35. /*     auto e = expand(mask); */
  36. /*     int ret = 0; */
  37. /*     int pivot = -1; */
  38. /*     REP(i,n) if(e[i] == 0) { pivot = i; break; } */
  39. /*     /1* dbg(e, pivot, lay); *1/ */
  40. /*     if(pivot == -1) { */
  41. /*         REP(i,n) e[i] --; */
  42. /*         return dfs(compact(e), lay - 1); */
  43. /*     } */
  44. /*     // 2 x 1 */
  45. /*     if(pivot + 1 < n && e[pivot + 1] == 0) { */
  46. /*         e[pivot] = 1; */
  47. /*         e[pivot + 1] = 1; */
  48. /*         add(ret, dfs(compact(e), lay)); */
  49. /*         e[pivot] = 0; */
  50. /*         e[pivot + 1] = 0; */
  51. /*     } */
  52. /*     // 1 x 2 */
  53. /*     e[pivot] = 2; */
  54. /*     add(ret, dfs(compact(e), lay)); */
  55. /*     e[pivot] = 0; */
  56.  
  57. /*     return dp[mask][lay] = ret; */
  58.  
  59. /* } */
  60.  
  61. void solve() {
  62.     cin >> n >> m;
  63.     int mx       = 1;
  64.     REP(i, n) mx = mx * 3;
  65.  
  66.     vector<vector<int>> dp(2, vector<int>(mx, 0));
  67.     dp[0][0] = 1;
  68.     vector<vector<pair<int, int>>> trans(mx);
  69.  
  70.     for (int i = mx - 1; i >= 0; i--) {
  71.         auto e = expand(i);
  72.  
  73.         int pivot = -1;
  74.         REP(i, n) if (e[i] == 0) {
  75.             pivot = i;
  76.             break;
  77.         }
  78.         if (pivot == -1) {
  79.             REP(i, n) e[i]--;
  80.             trans[i].push_back({1, compact(e)});
  81.             continue;
  82.         }
  83.  
  84.         // 2 x 1
  85.         if (pivot + 1 < n && e[pivot + 1] == 0) {
  86.             e[pivot]     = 1;
  87.             e[pivot + 1] = 1;
  88.             trans[i].push_back({0, compact(e)});
  89.             /* add(dp[now][i], dp[now][compact(e)]); */
  90.             e[pivot]     = 0;
  91.             e[pivot + 1] = 0;
  92.         }
  93.         // 1 x 2
  94.         e[pivot] = 2;
  95.         trans[i].push_back({0, compact(e)});
  96.         /* add(dp[now][i], dp[now][compact(e)]); */
  97.         e[pivot] = 0;
  98.     }
  99.  
  100.     for (int lay = 1; lay <= m; lay++) {
  101.         int now = lay & 1;
  102.         for (int i = mx - 1; i >= 0; i--) {
  103.             dp[now][i] = 0;
  104.             for (auto p : trans[i]) {
  105.                 add(dp[now][i], dp[now ^ p.first][p.second]);
  106.             }
  107.         }
  108.         /* dbg(now, dp[now]); */
  109.     }
  110.  
  111.     cout << dp[m & 1][0] << endl;
  112. }
  113.  
  114. int main() {
  115.     ios::sync_with_stdio(false);
  116.     cin.tie(0);
  117.     solve();
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement