#P5359. Coloring the 2×n Grid
Coloring the 2×n Grid
Coloring the 2×n Grid
Given a grid graph of dimensions . Some nodes have a pre-assigned color while others are uncolored. There are colors available, numbered from to . A valid coloring must ensure that any two adjacent nodes (i.e. nodes sharing an edge) have different colors. In the grid, vertices in the same column are adjacent (the top and bottom are connected), and vertices in neighboring columns in the same row are also adjacent.
You are given the current coloring of the grid: a 2×n matrix where each cell is either (indicating that the node is uncolored) or an integer between and (indicating a pre-assigned color). It is guaranteed that the pre-colored nodes do not violate the adjacent coloring condition.
Determine the number of valid coloring schemes for the uncolored nodes.
inputFormat
The first line contains two integers and . The next two lines each contain integers. The -th integer in each line denotes the color of the corresponding node in that row. A value of indicates the node is uncolored, while a value between and indicates that the node is pre-colored.
outputFormat
Output a single integer representing the number of valid coloring schemes for the uncolored nodes.
sample
1 3
0
0
6
</p>