Filling a 4 × n Rectangle with Dominoes
Time limit1sMemory limit128 MB
Count the tilings of a 4 by n board with dominoes and print the count modulo 1000 without leading zeros.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Combinatorics
- Solved
- No attempts yet
Problem
You have dominoes of width 1 and length 2, where is an integer smaller than . These dominoes can be arranged without overlap so that they exactly cover a rectangle. Figure 1 covers a rectangle with 12 dominoes.

Figure 1: an example with .
For there is more than one arrangement. Figure 2 shows all 5 ways of covering a rectangle.

Figure 2: the 5 ways of covering a rectangle.
Let be the number of different ways to cover a rectangle with dominoes. Figure 2 gives . Even for small the value of becomes very large, so what you report is its last three digits, that is modulo .
Input
One integer on a single line. Remember that .
Output
Print modulo on one line, without leading zeros. For example , so gives , and , so gives and not . When the remainder is zero, print a single .