XCopy
Time limit2sMemory limit1024 MB
Assign distinct non-negative integers to each cell of an N by M grid so that neighboring cells differ by exactly one bit, minimizing the largest value used.
- Level
Hard8 of 10
- Topics
- Bit manipulation, Matrix, Implementation
- Solved
- No attempts yet
Problem
Today, at the end of the programming class, the teacher gave very difficult homework, so the children decided to cheat and copy it from one another. However, they have to work smart so they do not get caught.
The class has students seated at benches arranged in rows and columns. Two children are neighbors if one sits at a bench directly to the left, right, above, or below the other. The homework asks for a non-negative integer. To avoid getting caught, all these integers must be different. The children are also lazy, so each child's answer differs from each neighbor's answer by exactly one bit in base . For example, and differ in exactly one bit, but and do not.
The children want the largest answer any of them gave to be as small as possible. Given and , create an arrangement of answers so that the teacher will not find out the children cheated.
Input
The input consists of and on a single line, separated by a single space.
Output
Print the optimal answers. Print rows, each containing non-negative integers separated by single spaces. Each integer is the answer of the child sitting at that position.
Constraints
Hint
In this section, a subscript after a number gives the base in which the number is written. For example, eight is written .
One set of optimal answers is shown in the table below:
Adjacent benches always have numbers that differ in exactly one bit. The maximum value of this solution is , which is optimal. Other optimal solutions also exist, such as this one flipped vertically or horizontally.
Another partial solution has a maximum of :
Under the scoring formula, this solution would receive of the test case score.