Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1e9+7;
- void generate_next_masks(int curr_mask, unordered_set<int>& next_masks, int i, int next_mask, int n) {
- if(i==n) {
- next_masks.insert(next_mask);
- return;
- }
- if((curr_mask&(1<<i)) != 0) {
- generate_next_masks(curr_mask, next_masks, i+1, next_mask, n); // can't put tile
- }
- else {
- generate_next_masks(curr_mask, next_masks, i+1, next_mask+(1<<i), n); // put a horizontal tile
- }
- if(i!=n-1) {
- if((curr_mask&(1<<i))==0 && (curr_mask&(1<<(i+1)))==0) {
- generate_next_masks(curr_mask, next_masks, i+2, next_mask, n); // put a vertical tile
- }
- }
- }
- int main() {
- ios_base::sync_with_stdio(false); cin.tie(0);
- int n, m; cin>>n>>m;
- vector<vector<int>> dp(m+1, vector<int>(1<<n, 0)); // 1-indexed columns
- dp[0][0] = 1; // base case
- unordered_set<int> curr_masks; curr_masks.insert(0);
- unordered_set<int> next_masks;
- unordered_set<int> tmp;
- for (int i=1; i<m+1; i++) {
- tmp.clear();
- for(const int& curr_mask:curr_masks) {
- next_masks.clear();
- generate_next_masks(curr_mask, next_masks, 0, 0, n);
- for(const int& next_mask:next_masks) {
- tmp.insert(next_mask);
- dp[i][next_mask] += dp[i-1][curr_mask];
- dp[i][next_mask] %= N;
- }
- }
- curr_masks = tmp;
- }
- cout << dp[m][0] << '\n'; // need 0 profile AFTER m-th column
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement