#P5359. Coloring the 2×n Grid

    ID: 18592 Type: Default 1000ms 256MiB

Coloring the 2×n Grid

Coloring the 2×n Grid

Given a grid graph of dimensions 2×n2 \times n. Some nodes have a pre-assigned color while others are uncolored. There are cc colors available, numbered from 11 to cc. 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 00 (indicating that the node is uncolored) or an integer between 11 and cc (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 nn and cc. The next two lines each contain nn integers. The ii-th integer in each line denotes the color of the corresponding node in that row. A value of 00 indicates the node is uncolored, while a value between 11 and cc 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>