Advertisement
Guest User

Untitled

a guest
Dec 4th, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 1e9+7;
  5.  
  6. void generate_next_masks(int curr_mask, unordered_set<int>& next_masks, int i, int next_mask, int n) {
  7.     if(i==n) {
  8.         next_masks.insert(next_mask);
  9.         return;
  10.     }
  11.    
  12.     if((curr_mask&(1<<i)) != 0) {
  13.         generate_next_masks(curr_mask, next_masks, i+1, next_mask, n);          // can't put tile
  14.     }
  15.     else {
  16.         generate_next_masks(curr_mask, next_masks, i+1, next_mask+(1<<i), n);   // put a horizontal tile
  17.     }
  18.  
  19.     if(i!=n-1) {
  20.         if((curr_mask&(1<<i))==0 && (curr_mask&(1<<(i+1)))==0) {
  21.             generate_next_masks(curr_mask, next_masks, i+2, next_mask, n);  // put a vertical tile
  22.         }
  23.     }
  24. }
  25.  
  26. int main() {
  27.    
  28.     ios_base::sync_with_stdio(false); cin.tie(0);
  29.    
  30.     int n, m; cin>>n>>m;
  31.  
  32.     vector<vector<int>> dp(m+1, vector<int>(1<<n, 0));      // 1-indexed columns
  33.     dp[0][0] = 1;       // base case
  34.  
  35.     unordered_set<int> curr_masks; curr_masks.insert(0);
  36.     unordered_set<int> next_masks;
  37.     unordered_set<int> tmp;
  38.  
  39.     for (int i=1; i<m+1; i++) {
  40.         tmp.clear();
  41.         for(const int& curr_mask:curr_masks) {
  42.             next_masks.clear();
  43.             generate_next_masks(curr_mask, next_masks, 0, 0, n);
  44.             for(const int& next_mask:next_masks) {
  45.                 tmp.insert(next_mask);
  46.                 dp[i][next_mask] += dp[i-1][curr_mask];
  47.                 dp[i][next_mask] %= N;
  48.             }
  49.         }
  50.         curr_masks = tmp;
  51.     }
  52.  
  53.     cout << dp[m][0] << '\n';       // need 0 profile AFTER m-th column
  54.    
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement