You have two kinds of tiles: a 2 x 1 domino and an L-shaped tromino, which covers three cells of a 2 x 2 square. You may rotate both shapes, so a domino can lie vertically or horizontally and a tromino can face any of four directions.
Given an integer n, return the number of ways to tile a 2 x n board. In a tiling every cell is covered by exactly one tile. Since the answer may be very large, return it modulo 10^9 + 7.
Function Signature
def num_tilings(n: int) -> int:
Rules
-
Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the tilings covers both cells with the same tile.
-
Return the count modulo
10^9 + 7
.
Constraints
Examples
Input: n = 3
Output: 5
+---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+
| A | B | C | | A | A | B | | A | B | B | | A | A | B | | A | B | B |
+---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+
| A | B | C | | C | C | B | | A | C | C | | A | B | B | | A | A | B |
+---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+
Cells with the same letter belong to one tile. Three tilings use only dominoes, and the other two use a pair of interlocking trominoes that fill the whole board in one of two mirror-image ways.
Input: n = 1
Output: 1
Only one vertical domino fits in a single column.
Input: n = 4
Output: 11
Five tilings use only dominoes. Four more put a two-tromino 2 x 3 block (in either mirror form) next to a vertical domino on the left or on the right, and the last two place a tromino at each end with one horizontal domino between them.