Beautiful Odd Array
Time limit1sMemory limit128 MB
Fill empty grid cells with digits 1 to 9 so every run of H vertical and W horizontal cells sums to an odd number, and count the completions modulo 1e9+7.
- Level
Medium7 of 10
- Topics
- Math, Combinatorics
- Solved
- No attempts yet
Problem
There is a two dimensional array A with N rows and M columns. A[i][j] is the number written in row i, column j. (0 ≤ i < N, 0 ≤ j < M)
The array A is a beautiful odd array when it satisfies all three conditions below.
- For every pair of integers r, c with 0 ≤ r ≤ N-H and 0 ≤ c < M, the sum
A[r][c] + A[r+1][c] + ... + A[r+H-1][c]is odd. - For every pair of integers r, c with 0 ≤ r < N and 0 ≤ c ≤ M-W, the sum
A[r][c] + A[r][c+1] + ... + A[r][c+W-1]is odd. - For every pair of integers r, c with 0 ≤ r < N and 0 ≤ c < M,
A[r][c]is between 1 and 9.
You are given an unfinished array A in which some cells are empty. Write one number between 1 and 9 in each empty cell to complete the array. Count how many beautiful odd arrays you can build this way. Two completed arrays are different when at least one cell differs.
Input
The first line has four natural numbers N, M, H, W. (1 ≤ N, M ≤ 50, 1 ≤ H ≤ min(N, 10), 1 ≤ W ≤ min(M, 10))
Each of the next N lines describes the unfinished array A with M numbers separated by spaces. The (j+1)-th number on the (i+2)-th line is A[i][j], and an empty cell is given as 0. A cell that is not empty holds a number between 1 and 9.
Output
On the first line print the number of beautiful odd arrays you can build from the unfinished array A, modulo 1,000,000,007. Print 0 when no completion works.
Hint
An empty cell that has to hold an odd number is filled with one of 1, 3, 5, 7, 9, and an empty cell that has to hold an even number is filled with one of 2, 4, 6, 8.