#P10692. Minimum Weight Expression Matrix

    ID: 12722 Type: Default 1000ms 256MiB

Minimum Weight Expression Matrix

Minimum Weight Expression Matrix

We are given two positive integers (n) and (m) representing the number of rows and columns of a character matrix (a_{ij}). The matrix is called a legal expression matrix if and only if:

  1. Every character is one of the following: '1', '+', or '*'.
  2. For each row, the string formed by reading from left to right is a legal expression.
  3. For each column, the string formed by reading from top to bottom is a legal expression.

A string (s) is defined to be a legal expression by the following rules:

  • If (s = 11\ldots1) (at least one 1), then (s) is a legal expression. Its value is defined as the number of characters (i.e. the count of ones).
  • If both (s) and (t) are legal expressions then (s * t) is also a legal expression and its value is (value(s) \times value(t)).
  • If both (s) and (t) are legal expressions then (s + t) is also a legal expression and its value is (value(s) + value(t)).

A legal expression matrix has a total weight defined as the sum of the evaluated values of the (n) row expressions and the (m) column expressions. Among all (n \times m) legal expression matrices, our goal is to output one that minimizes the total weight. (If there are multiple answers, any one is acceptable.)

It turns out that a construction which always produces a legal matrix is as follows. We choose the characters for each cell (A[i][j]) (with 1-indexed rows and columns) according to these rules:

  1. Initially, set every cell to '1'.
  2. For every row with an even index (i.e. (i) even) and for every even column index (j) such that (j < m), change (A[i][j]) to '*'.
  3. To guarantee that every row ends with a digit, force the last column ((j = m)) to be '1' regardless.
  4. Similarly, for every column with an even index (i.e. (j) even) and for every even row (i) with (i < n), the cell is already set to '*'. (The effect is that every even-indexed column will, when read top to bottom, have a pattern of '1' in odd-indexed rows and '*' in even-indexed rows; with the last row automatically remaining '1' if (n) is even.)

Observe that:

  • A row with odd index remains as a string of all ones. Such a row is legal (interpreted as a base case) and its value is (m).
  • A row with even index becomes, for example, for (m = 3), the string "11"; for (m = 4), the string "111". In these cases the expression is legal and its evaluated value is less than the base value (for (m \ge 3)).
  • For every odd-indexed column the column string is a sequence of ones (value (n)), and for an even-indexed column the column string (for example, "11" when (n = 3) or "111" when (n = 4)) is legal and evaluates to a value lower than (n) when possible.

This construction does not always achieve the theoretical minimum for each individual row or column but it yields a legal expression matrix with a lower total weight than the trivial all-ones matrix.

Your task is to implement this construction.

inputFormat

The input consists of a single line containing two integers (n) and (m) ((1 \le n, m \le 1000)).

outputFormat

Output (n) lines. Each line should be a string of length (m) representing the corresponding row of the matrix that is a legal expression matrix with (hopefully) minimum total weight.

sample

3 3
111

1*1 111

</p>