Program to count number of ways we can fill 3 x n box with 2 x 1 dominos in Python
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
- 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
Related Articles
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++
- Program to count number of ways we can throw n dices in Python
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++
- Program to count number of configurations are there to fill area with dominos and trominos in C++
- Check whether the following are quadratic equations:(i) \( (x+1)^{2}=2(x-3) \)(ii) \( x^{2}-2 x=(-2)(3-x) \)(iii) \( (x-2)(x+1)=(x-1)(x+3) \)(iv) \( (x-3)(2 x+1)=x(x+5) \)(v) \( (2 x-1)(x-3)=(x+5)(x-1) \)(vi) \( x^{2}+3 x+1=(x-2)^{2} \)(vii) \( (x+2)^{3}=2 x\left(x^{2}-1\right) \)(viii) \( x^{3}-4 x^{2}-x+1=(x-2)^{3} \)
- Program to count number of ways we can distribute coins to workers in Python
- Determine which of the following polynomials has \( (x+1) \) a factor:(i) \( x^{3}+x^{2}+x+1 \)(ii) \( x^{4}+x^{3}+x^{2}+x+1 \)(iii) \( x^{4}+3 x^{3}+3 x^{2}+x+1 \)(iv) \( x^{3}-x^{2}-(2+\sqrt{2}) x+\sqrt{2} \)
- Count the number of ways to tile the floor of size n x m using 1 x m size tiles in C++
- Solve for x:$\frac{1}{( x-1)( x-2)} +\frac{1}{( x-2)( x-3)} =\frac{2}{3} \ ,\ x\neq 1,2,3$
- If \( x+\frac{1}{x}=3 \), calculate \( x^{2}+\frac{1}{x^{2}}, x^{3}+\frac{1}{x^{3}} \) and \( x^{4}+\frac{1}{x^{4}} \).
- Simplify each of the following products:\( (x^{3}-3 x^{2}-x)(x^{2}-3 x+1) \)
- Use the Factor Theorem to determine whether \( g(x) \) is a factor of \( p(x) \) in each of the following cases:(i) \( p(x)=2 x^{3}+x^{2}-2 x-1, g(x)=x+1 \)(ii) \( p(x)=x^{3}+3 x^{2}+3 x+1, g(x)=x+2 \)(iii) \( p(x)=x^{3}-4 x^{2}+x+6, g(x)=x-3 \)
- If \( x-\frac{1}{x}=3+2 \sqrt{2} \), find the value of \( x^{3}- \frac{1}{x^{3}} \).
- Simplify:$(x^3 - 2x^2 + 3x - 4) (x - 1) - (2x - 3) (x^2 - x + 1)$
- Solve for $x$:$\frac{x-1}{x-2}+\frac{x-3}{x-4}=3\frac{1}{3}, x≠2, 4$
Kickstart Your Career
Get certified by completing the course
Get Started