All problems

Problem 457

Domino and Tromino Tiling

Medium · 20 pts · Dynamic Programming, Math

Problem

Count the ways to tile a 2 x N board using dominoes (2 x 1 pieces) and L-shaped trominoes (pieces covering three cells in an L shape). Both kinds of piece may be rotated. Two tilings are different if some pair of edge-adjacent cells is covered by the same piece in one tiling but not in the other. Print the count modulo 1000000007. The counts satisfy the recurrence f(n) = 2·f(n-1) + f(n-3), with f(0) = 1, f(1) = 1 and f(2) = 2. Keep the counts in a 64-bit integer type (long long in C++, long in Java) and take the remainder after every addition and multiplication: adding or multiplying two remainders can be larger than a 32-bit int can hold (Python needs nothing special).

Input

A single integer N.

Output

Print the number of tilings modulo 1000000007.

Constraints

1 <= N <= 1000

Example input

3

Example output

5

Explanation

A 2 x 3 board can be tiled in 5 ways: three vertical dominoes, one vertical domino with two horizontal ones (two placements), or two L-trominoes (two orientations).

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Stock Trading with Cooldown Next: Integer Partitions