# Program to count number of ways we can fill 3 x n box with 2 x 1 dominos in Python

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Suppose we have a number n, we have to find the number of ways we can fill a (3 x n) block with 1 x 2 dominos. We can rotate the dominos when required. If the answer is very large then return this mod 10^9 + 7.

So, if the input is like n = 4, then the output will be 11.

To solve this, we will follow these steps −

• m = 10^9 + 7
• if n is odd, then
• return 0
• cs := 1, os := 0
• for i in range 2 to n, increase by 2, do
• cs := 3 * cs + os
• os := 2 * cs + os
• return cs mod m

## Example (Python)

Let us see the following implementation to get better understanding −

Live Demo

class Solution:
def solve(self, n):
m = (10 ** 9 + 7)
if n % 2 == 1:
return 0
cs = 1
os = 0
for i in range(2, n + 1, 2):
cs, os = (3 * cs + os, 2 * cs + os,)
return cs % m
ob = Solution()
n = 4
print(ob.solve(n))

## Input

4

## Output

11