#P3631. [APIO2011] 方格染色

    ID: 1419 远端评测题 3000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>数学2011并查集APIO枚举,暴力位运算,按位

[APIO2011] 方格染色

Description

Sam and his sister Sara have a grid with n×mn \times m cells. They want to color each cell either red or blue. Due to personal preference, they want every 2×22 \times 2 square in the grid to contain an odd number (11 or 33) of red cells. For example, the following is a valid coloring (R stands for red, B stands for blue):

B B R B R
R B B B B
R R B R B

However, last night, someone already colored some cells! Now Sam and Sara are very upset. Still, they want to know whether it is possible to color the remaining cells so that the entire grid still satisfies their requirement. If it is possible, how many such colorings are there?

Input Format

The first line contains three integers n,m,kn, m, k, representing the number of rows, the number of columns, and the number of precolored cells, respectively.

The next kk lines describe the precolored cells. The ii-th line contains three integers xi,yi,cix_i, y_i, c_i, representing the row index, the column index, and the color of the ii-th precolored cell, respectively. ci=1c_i = 1 means the cell is red, and ci=0c_i = 0 means the cell is blue.

Output Format

Output a single integer: the value of ww modulo 10910^9, where ww is the number of valid colorings.

3 4 3
2 2 1
1 2 0
2 3 1
8

Hint

For 20%20\% of the testdata, n,m,k5n, m, k \leqslant 5.

For 50%50\% of the testdata, n,m5000n, m \leqslant 5000, k25k \leqslant 25.

For 100%100\% of the testdata, 2n,m1052 \leqslant n, m \leqslant 10^5, 0k1050 \leqslant k \leqslant 10^5, 1xin1 \leqslant x_i \leqslant n, 1yim1 \leqslant y_i \leqslant m, ci{0,1}\forall c_i \in \{0, 1\}.

Translated by ChatGPT 5