Given a 3 X n board, find the number of ways to color it using at most 4 colors such that no two adjacent boxes have the same color. Diagonal neighbors are not treated as adjacent boxes.

Output the ways%1000000007 as the answer grows quickly.

Constraints:

1<= n < 100000

**Examples :**

Input : 1 Output : 36 We can use either a combination of 3 colors or 2 colors. Now, choosing 3 colors out of 4 is and arranging them in 3! ways, similarly choosing 2 colors out of 4 is and while arranging we can only choose which of them could be at centre, that would be 2 ways. Answer = *3! + *2! = 36 Input : 2 Output : 588

We are going to solve this using dynamic approach because when a new column is added to the board, the ways in which colors are going to be filled depends just upon the color pattern in the current column. We can only have a combination of two colors and three colors in a column. All possible new columns that can be generated is given in the image. Please consider A, B, C and D as 4 colors.

From now, we will refer 3 colors combination for a Nth column of the 3*N board as W(n) and two colors as Y(n).

We can see that each W can generate 5Y and 11W, and each Y can generate 7Y and 10W. We get two equation from here

We have two equations now,

W(n+1) = 10*Y(n)+11*W(n); Y(n+1) = 7*Y(n)+5*W(n);

## C++

`// C++ program to find number of ways ` `// to color a 3 x n grid using 4 colors ` `// such that no two adjacent have same ` `// color ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `int` `solve(` `int` `A) ` `{ ` ` ` ` ` `// When we to fill single column ` ` ` `long` `int` `color3 = 24; ` ` ` `long` `int` `color2 = 12; ` ` ` `long` `int` `temp = 0; ` ` ` ` ` `for` `(` `int` `i = 2; i <= A; i++) ` ` ` `{ ` ` ` `temp = color3; ` ` ` `color3 = (11 * color3 + 10 * ` ` ` `color2 ) % 1000000007; ` ` ` ` ` `color2 = ( 5 * temp + 7 * ` ` ` `color2 ) % 1000000007; ` ` ` `} ` ` ` ` ` `long` `num = (color3 + color2) ` ` ` `% 1000000007; ` ` ` ` ` `return` `(` `int` `)num; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `num1 = 1; ` ` ` `cout << solve(num1) << endl; ` ` ` ` ` `int` `num2 = 2; ` ` ` `cout << solve(num2) << endl; ` ` ` ` ` `int` `num3 = 500; ` ` ` `cout << solve(num3) << endl; ` ` ` ` ` `int` `num4 = 10000; ` ` ` `cout << solve(num4); ` ` ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find number of ways to color ` `// a 3 x n grid using 4 colors such that no two ` `// adjacent have same color. ` `public` `class` `Solution { ` ` ` `public` `static` `int` `solve(` `int` `A) { ` ` ` `long` `color3 = ` `24` `; ` `// When we to fill single column ` ` ` `long` `color2 = ` `12` `; ` ` ` `long` `temp = ` `0` `; ` ` ` `for` `(` `int` `i = ` `2` `; i <= A; i++) ` ` ` `{ ` ` ` `long` `temp = color3; ` ` ` `color3 = (` `11` `* color3 + ` `10` `* color2 ) % ` `1000000007` `; ` ` ` `color2 = ( ` `5` `* temp + ` `7` `* color2 ) % ` `1000000007` `; ` ` ` `} ` ` ` `long` `num = (color3 + color2) % ` `1000000007` `; ` ` ` `return` `(` `int` `)num; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `num1 = ` `1` `; ` ` ` `System.out.println(solve(num1)); ` ` ` ` ` `int` `num2 = ` `2` `; ` ` ` `System.out.println(solve(num2)); ` ` ` ` ` `int` `num3 = ` `500` `; ` ` ` `System.out.println(solve(num3)); ` ` ` ` ` `int` `num4 = ` `10000` `; ` ` ` `System.out.println(solve(num4)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find number of ways ` `# to color a 3 x n grid using 4 colors ` `# such that no two adjacent have same ` `# color ` ` ` `def` `solve(A): ` ` ` ` ` `# When we to fill single column ` ` ` `color3 ` `=` `24` ` ` `color2 ` `=` `12` ` ` `temp ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(` `2` `, A ` `+` `1` `, ` `1` `): ` ` ` `temp ` `=` `color3 ` ` ` `color3 ` `=` `(` `11` `*` `color3 ` `+` `10` `*` `color2 ) ` `%` `1000000007` ` ` ` ` `color2 ` `=` `( ` `5` `*` `temp ` `+` `7` `*` `color2 ) ` `%` `1000000007` ` ` ` ` `num ` `=` `(color3 ` `+` `color2) ` `%` `1000000007` ` ` ` ` `return` `num ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `num1 ` `=` `1` ` ` `print` `(solve(num1)) ` ` ` ` ` `num2 ` `=` `2` ` ` `print` `(solve(num2)) ` ` ` ` ` `num3 ` `=` `500` ` ` `print` `(solve(num3)) ` ` ` ` ` `num4 ` `=` `10000` ` ` `print` `(solve(num4)) ` ` ` `# This code is contributed by ` `# Shashank_Sharma ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find number of ways ` `// to color a 3 x n grid using 4 ` `// colors such that no two adjacent ` `// have same color. ` `using` `System; ` ` ` `public` `class` `GFG { ` ` ` ` ` `public` `static` `int` `solve(` `int` `A) ` ` ` `{ ` ` ` ` ` `// When we to fill single column ` ` ` `long` `color3 = 24; ` ` ` `long` `color2 = 12; ` ` ` `long` `temp = 0; ` ` ` ` ` `for` `(` `int` `i = 2; i <= A; i++) ` ` ` `{ ` ` ` `temp = color3; ` ` ` `color3 = (11 * color3 + 10 ` ` ` `* color2 ) % 1000000007; ` ` ` ` ` `color2 = ( 5 * temp + 7 ` ` ` `* color2 ) % 1000000007; ` ` ` `} ` ` ` `long` `num = (color3 + color2) ` ` ` `% 1000000007; ` ` ` `return` `(` `int` `)num; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `num1 = 1; ` ` ` `Console.WriteLine(solve(num1)); ` ` ` ` ` `int` `num2 = 2; ` ` ` `Console.WriteLine(solve(num2)); ` ` ` ` ` `int` `num3 = 500; ` ` ` `Console.WriteLine(solve(num3)); ` ` ` ` ` `int` `num4 = 10000; ` ` ` `Console.WriteLine(solve(num4)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find number of ways ` `// to color a 3 x n grid using 4 colors ` `// such that no two adjacent have same ` `// color ` `function` `solve(` `$A` `) ` `{ ` ` ` ` ` `// When we to fill single column ` ` ` `$color3` `= 24; ` ` ` `$color2` `= 12; ` ` ` `$temp` `= 0; ` ` ` ` ` `for` `(` `$i` `= 2; ` `$i` `<= ` `$A` `; ` `$i` `++) ` ` ` `{ ` ` ` `$temp` `= ` `$color3` `; ` ` ` `$color3` `= (11 * ` `$color3` `+ ` ` ` `10 * ` `$color2` `) % ` ` ` `1000000007; ` ` ` ` ` `$color2` `= ( 5 * ` `$temp` `+ ` ` ` `7 * ` `$color2` `) % ` ` ` `1000000007; ` ` ` `} ` ` ` ` ` `$num` `= (` `$color3` `+ ` `$color2` `) % ` ` ` `1000000007; ` ` ` ` ` `return` `(int)` `$num` `; ` `} ` ` ` `// Driver code ` `$num1` `= 1; ` `echo` `solve(` `$num1` `) ,` `"\n"` `; ` ` ` `$num2` `= 2; ` `echo` `solve(` `$num2` `) ,` `"\n"` `; ` ` ` `$num3` `= 500; ` `echo` `solve(` `$num3` `),` `"\n"` `; ` ` ` `$num4` `= 10000; ` `echo` `solve(` `$num4` `); ` ` ` `// This code is contributed by m_kit. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

36 588 178599516 540460643

This article is contributed by **Panshul Garg**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of ways to place two queens on a N*N chess-board
- Check if quantities of 3 distinct colors can be converted to a single color by given merge-pair operations
- Ways to paint stairs with two colors such that two adjacent are not yellow
- Ways to paint N paintings such that adjacent paintings don't have same colors
- Number of ways to paint a tree of N nodes with K distinct colors with given conditions
- Expected number of moves to reach the end of a board | Dynamic programming
- Number of ways to color N-K blocks using given operation
- Ways to sum to N using array elements with repetition allowed
- Different ways to sum n using numbers greater than or equal to m
- Number of ways in which the substring in range [L, R] can be formed using characters out of the range
- Count ways to divide circle using N non-intersecting chord | Set-2
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Count ways to reach the nth stair using step 1, 2 or 3
- Number of ways to cut a stick of length N into K pieces
- Number of ways to make mobile lock pattern
- Count ways to spell a number with repeated digits
- Ways to represent a number as a sum of 1's and 2's
- Ways to select one or more pairs from two different sets
- Count ways to form minimum product triplets
- Count ways to express 'n' as sum of odd integers