atcoder#AGC061D. [AGC061D] Almost Multiplication Table

[AGC061D] Almost Multiplication Table

Score : 10001000 points

Problem Statement

There are positive integers N,MN, M, and an N×MN \times M positive integer matrix Ai,jA_{i, j}. For two (strictly) increasing positive integer sequences X=(X1,,XN)X = (X_1, \ldots, X_N) and Y=(Y1,,YM)Y = (Y_1, \ldots, Y_M), we define the penalty D(X,Y)D(X, Y) as $\max_{1 \leq i \leq N, 1 \leq j \leq M} |X_i Y_j - A_{i, j}|$.

Find two (strictly) increasing positive integer sequences XX and YY such that D(X,Y)D(X, Y) is the smallest possible.

Constraints

  • 1N,M51 \leq N, M \leq 5
  • 1Ai,j1091 \leq A_{i, j} \leq 10^9 (1iN1 \leq i \leq N, 1jM1 \leq j \leq M)
  • All values in the input are integers.

Input

Input is given from Standard Input in the following format:

NN MM

A1,1A_{1,1} \ldots A1,MA_{1,M}

\vdots

AN,1A_{N,1} \ldots AN,MA_{N,M}

Output

Output your answer in the following format:

DminD_{min}

X1X_1 \ldots XNX_N

Y1Y_1 \ldots YMY_M

Here, DminD_{min} is the smallest possible penalty and the following conditions should be satisfied:

  • D(X,Y)D(X, Y) should be equal to DminD_{min}.
  • Xi<Xi+1X_i < X_{i + 1} (1iN11 \leq i \leq N - 1).
  • Yj<Yj+1Y_j < Y_{j + 1} (1jM11 \leq j \leq M - 1).
  • 1Xi21091 \leq X_i \leq 2\cdot 10^9 (1iN1 \leq i \leq N).
  • 1Yj21091 \leq Y_j \leq 2\cdot 10^9 (1jM1 \leq j \leq M).

One can show that there is an optimal solution satisfying the last two conditions.

1 1
853922530
0
31415
27182
3 3
4 4 4
4 4 4
4 4 4
5
1 2 3 
1 2 3
3 4
4674 7356 86312 100327
8737 11831 145034 167690
47432 66105 809393 936462
357
129 216 1208 
39 55 670 775