CF40E Number Table <计数>
Problem
Number Table
Description
As it has been found out recently, all the Berland’s current economical state can be described using a simple table in size. ― the number of days in each Berland month, ― the number of months. Thus, a table cell corresponds to a day and a month of the Berland’s year. Each cell will contain either , or , which means the state’s gains in a particular month, on a particular day. corresponds to profits, corresponds to losses. It turned out important for successful development to analyze the data on the state of the economy of the previous year, however when the treasurers referred to the archives to retrieve the data, it turned out that the table had been substantially damaged. In some table cells the number values had faded and were impossible to be deciphered. It is known that the number of cells in which the data had been preserved is strictly less than . However, there is additional information ― the product of the numbers in each line and column equaled . Your task is to find out how many different tables may conform to the preserved data. As the answer to the task can be quite large, you have to find it modulo .
Input
The first line contains integers and . The second line contains the integer ― the number of cells in which the data had been preserved. The next lines contain the data on the state of the table in the preserved cells. Each line is of the form a b c
, where ― the number of the table row, ― the number of the column, ― the value containing in the cell ( or ). They are numbered starting from . It is guaranteed that no two lines with same and values exist. The last line contains an integer .
Output
Print the number of different tables that could conform to the preserved data modulo .
Example
Input #1
1 | 2 2 |
Output #1
1 | 2 |
Input #2
1 | 2 2 |
Output #2
1 | 1 |
标签:计数
Translation
题目大意:有一个的矩阵,其每个位置上的值为或。已知其个位置的值,求有多少种填剩余位置的方案能使得其每行每列的乘积均为。输出答案模的值。
Solution
注意到重要的数据条件,可知其一定有一行或一列是不存在给定值的。如果,则交换行列,使得其一定剩一个空行。如果随意填除这一行外其他位置的值,保证其他行的乘积均为,一定能找到一种填这一行的方案使得所有列都是。这时,如果奇偶性相同,则这一行的乘积也为,符合所有条件;否则这一行的乘积一定为,不存在可行解。因此,对于除这一行外的所有行统计每行乘积为的方案数相乘,再用奇偶性判断一下有无可行解即可。特别地,如果某一行全都有给定值,且乘积为,则无解。
对于一个未填满给定值的行,选出第一个未填数的位置,对于其他空位随意填或,如果填完后乘积为,则第一个位置填,否则第一个位置填。因此对于一个已填了个数的行,若,则其可行解种数为;若,如果乘积为,则无可行解,否则有种可行解。用乘法原理统计方案数即可。
Code
1 |
|