To solve this problem, we need to count the number of ways to tile an ( n \times m ) grid using ( 1 \times 2 ) and ( 2 \times 1 ) tiles. Due to the problem's constraints (( n ) up to 10 and ( m ) up to 1000), we need an efficient algorithm that uses dynamic programming combined with bit masking and matrix exponentiation.
Understanding the State Representation:
We represent each state as a bitmask of length ( n ). Each bit in the mask represents whether there is a 'dangling' tile from the previous column at that row. A bit value of 1 means there is a dangling tile that needs to be connected in the current column; a 0 means there is no such tile.
Generating State Transitions:
We create a function dfs(s, pos, cur_state) that recursively fills the current column based on the given state s, starting at position pos and building up the cur_state bitmask. The recursion:
- Base Case: If ( pos == n ), we've finished processing the current column. We increment the transition count from state
s to cur_state.
- Recursive Case:
- If the bit at
pos in s is set (there's a dangling tile), we have to connect it. The only option is to place a horizontal tile with the adjacent cell if possible.
- If the bit at
pos is not set, we have two choices:
- Place a vertical tile, which leads to a dangling tile in the next column (we set the bit at
pos in cur_state).
- Place a horizontal tile with the adjacent cell if possible.
Building the Transition Matrix:
We use the transitions to build a matrix A, where A[s][t] equals the number of ways we can transition from state s to state t in one column.
Matrix Exponentiation:
We raise the matrix A to the power of m using exponentiation by squaring. Since each transition represents moving from one column to the next, raising A to the power m gives us the total transitions over m columns.
Calculating the Final Answer:
After exponentiating the transition matrix, the entry at A^m[0][0] gives the total number of ways to tile an ( n \times m ) grid, starting and ending with state 0 (no dangling tiles).
Here's the Python code that implements the above algorithm:
mod = 10**9+7
def main():
import sys
import threading
def solve():
n, m = map(int, sys.stdin.readline().split())
max_state = 1 << n
transitions = [{} for _ in range(max_state)]
from collections import defaultdict
def dfs(s, pos, cur_state):
if pos == n:
# Completed filling the current column
# Record the transition from s to cur_state
transitions[s][cur_state] = transitions[s].get(cur_state, 0) + 1
return
if s & (1 << pos):
# Cell already filled due to dangling tile
dfs(s, pos + 1, cur_state)
else:
# Try placing horizontal domino
if pos + 1 < n and not (s & (1 << (pos + 1))):
dfs(s, pos + 2, cur_state)
# Try placing vertical domino (leaves a dangling tile)
dfs(s, pos + 1, cur_state | (1 << pos))
# Precompute transitions
for s in range(max_state):
dfs(s, 0, 0)
# Build the transition matrix
import numpy as np
A = np.zeros((max_state, max_state), dtype=object)
for s in range(max_state):
for t, count in transitions[s].items():
A[s][t] = count % mod
# Exponentiate the transition matrix
def matrix_power(matrix, power):
result = np.identity(len(matrix), dtype=object)
while power > 0:
if power % 2 == 1:
result = (result @ matrix) % mod
matrix = (matrix @ matrix) % mod
power //= 1
power >>= 1
return result
A_pow_m = matrix_power(A, m)
# The number of ways is the entry from state 0 to state 0
print(A_pow_m[0][0] % mod)
threading.Thread(target=solve).start()
main()
This code efficiently computes the number of possible ways by handling transitions between different states using bit masks and leveraging matrix exponentiation to account for the multiple columns in the grid.