Coloring the Table
InterviewTime limit2sMemory limit256 MB
Count colorings of an n by m grid, red or blue, where every 2 by 2 block has an odd number of red cells, respecting k fixed cells.
- Level
Medium7 of 10
- Topics
- Math, Combinatorics, Union-find, Implementation
- Solved
- No attempts yet
Problem
Sam and his younger sister Sara want to color every cell of an table either red or blue. Because of their personal belief, they want every square block of the table to contain an odd number of red cells (that is, exactly 1 or 3). For example, such a valid coloring also exists for a table.
Last night, however, someone painted some of the cells red and some blue in advance. Keeping the colors of those already-painted cells, Sam and Sara want to know whether they can color the remaining cells so that every square block contains an odd number of red cells. If it is possible, they also want to know how many different ways there are to do so.
Input
The first line contains three integers , , and : the number of rows, the number of columns, and the number of pre-colored cells, respectively. Each of the next lines describes one pre-colored cell with three integers , , and , where and are the row and column of the cell and is its color: if it is red and if it is blue. The positions of the pre-colored cells are all distinct.
Output
Let be the number of ways to color the table so that the condition holds. Print modulo on a single line.