CSES - Counting Tilings
Author: Andi Qu
Time Complexity: .
Let be the number of ways to tile the grid so that:
- All cells from cell to cell are covered.
- All cells from cell to cell are empty.
- represents whether each of the remaining cells are empty, with the -th bit corresponding to the cell in column .
For example, the following state would contribute toward :
We now have two cases when calculating :
- The -th bit of mask is 0, meaning that cell is covered.
- Case 1: we used a vertical tile to cover it.
- Cell must have been empty before this, so there are ways to do this.
- Case 2: we used a horizontal tile to cover it.
- This is only possible if .
- Cell must have been empty before this, while cells and must have been covered, so there are to do this.
- Case 1: we used a vertical tile to cover it.
- The -th bit of the mask is 1, meaning that cell is empty.
- Cell must have been covered before this, so there are ways to do this.
Note that the indices we need to use may become negative and will thus require wrapping. To simplify calculations and bypass this, simply drop the first two dimensions of the DP array, as depends only on and .
C++
#include <bits/stdc++.h>typedef long long ll;using namespace std;const ll MOD = 1e9 + 7;ll dp[1 << 10][3];int main() {cin.tie(0)->sync_with_stdio(0);
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!