Mosaic
Time limit1sMemory limit128 MB
Count the tilings of an N x M grid with 2 x 2 squares and L-trominoes, modulo 1,000,000.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
An architect wants to decorate a building with a long, thin mosaic. The mosaic occupies a rectangular strip that is inches tall and inches wide. Think of the strip as an grid of unit cells, where each cell is one inch by one inch.
He has two kinds of tiles, and each tile fits inside a block of cells:

- A square tile that covers all four cells of a block.
- An L-shaped tile (an L-tromino) that covers exactly three cells of a block, leaving one corner cell empty. This tile may be rotated into any of its four orientations.
He wants to cover the whole strip so that every cell is covered exactly once, with no gaps and no overlapping tiles. He wonders how many different patterns he can make.
Two mosaics are considered the same only when they use the same kinds of tiles in exactly the same positions. If a rotation or reflection of a pattern places tiles in different cells, it counts as a different pattern. For example, the four mosaics below are rotations or reflections of one another, yet the architect counts them as four different mosaics:

Input
The input contains several test cases. Each test case is a single line with two integers and (, ): the height and the width of the strip, in inches. The input ends with a line containing two zeros; this terminating line is not a test case.
Output
For each test case, print one line containing a single integer: the number of distinct tilings of the strip, taken modulo . Print no extra whitespace, and print no blank lines between answers.