Problem 964
Number of Ways to Paint N × 3 Grid
Hard · 30 pts · Dynamic Programming
Problem
Paint every cell of an N × 3 grid with one of three colours so that no two cells sharing a side have the same colour. Count the colourings modulo 1000000007. Each row is either an ABA pattern (outer cells equal, 6 choices) or an ABC pattern (all different, 6 choices). From one row to the next: an ABA row allows 3 ABA rows and 2 ABC rows, and an ABC row allows 2 of each, so a two-number recurrence solves it. Keep the counts in a 64-bit integer type (long long in C++, long in Java) and take the remainder after every addition and multiplication: adding or multiplying two remainders can be larger than a 32-bit int can hold (Python needs nothing special).
Input
A single integer N.
Output
Print the number of colourings modulo 1000000007.
Constraints
1 <= N <= 5000
Example input
1
Example output
12
Explanation
A single row of 3 cells can be painted in 3 × 2 × 2 = 12 ways (each cell differs from its left neighbour). So for N = 1 the answer is 12.