Harmonious Matrices
Time limit1sMemory limit128 MB
Given m and n (up to 40), output the lexicographically smallest nonzero matrix of bits where every cell sees an even number of 1s among itself and its orthogonal neighbors.
- Level
Hard8 of 10
- Topics
- Bit manipulation, Greedy, Brute force, Math
- Solved
- No attempts yet
Problem
Call an matrix of bits harmonious if every cell has an even number of bits among its neighbors. A cell counts as a neighbor of itself, and its neighbors also include the cells directly above, below, to the left, and to the right of it (whenever those cells exist). So a cell has at most five neighbors, and fewer near an edge or corner.
For example, the following matrix is harmonious:
0 1 0 0
1 1 1 0
0 0 0 1
1 1 0 1
Given and , produce an harmonious matrix of bits.
Input
The first line contains an integer (). Each of the next lines contains two space-separated positive integers and , each at most , describing one instance.
Output
For each instance, output its matrix, one row per line, with the entries of a row separated by single spaces. Print the instances in order, with no blank line between them.
The all-zero matrix is always harmonious, so an instance can have many harmonious matrices. To make the answer unique, output the lexicographically smallest non-zero harmonious matrix; if the all-zero matrix is the only harmonious matrix, output the all-zero matrix. Compare two matrices by reading their entries in row-major order (top to bottom, and left to right within each row), treating as smaller than .