#P3164. Harmonious Matrix Construction

    ID: 16421 Type: Default 1000ms 256MiB

Harmonious Matrix Construction

Harmonious Matrix Construction

A binary matrix (i.e. a matrix whose entries are either 0 or 1) is called harmonious if and only if for every element, the sum of all 1's among its adjacent cells is even. Here, the neighbors of an element include the element itself and (if they exist) its four immediate neighbors (up, down, left, right).

Your task is: Given two positive integers m and n representing the number of rows and columns respectively, output an harmonious matrix of size m × n that is not the all-zero matrix.

Note: It is guaranteed that both m and n will be at least 2. (For m = 1 or n = 1 the problem is degenerate.)

A cell at row i and column j (using 0-indexing) must satisfy \[ x_{i,j} + x_{i-1,j} + x_{i+1,j} + x_{i,j-1} + x_{i,j+1} \equiv 0 \pmod{2}, \] where any index that is out‐of‐bounds is simply ignored.

One valid harmonious matrix for a 4×4 case is shown below (each row is printed on a separate line):

0110
1001
1001
0110

For matrices with dimensions larger than 4×4, you may output a matrix where the upper‐left 4×4 block is as above and the remaining entries are 0. Similarly, for a 2×3 matrix one valid solution is:

101
101

inputFormat

The input consists of a single line containing two integers m and n (with m, n ≥ 2) separated by a space.

For example:

4 4

outputFormat

Output m lines. Each line should contain a string of length n consisting only of the characters '0' and '1'. This forms a harmonious matrix (i.e. every element, together with its non‐missing up/down/left/right neighbors, has an even number of 1’s) and the matrix must contain at least one 1.

For example, for the input above a valid output is:

0110
1001
1001
0110

sample

4 4
0110

1001 1001 0110

</p>