시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
4 초 | 512 MB | 4 | 4 | 4 | 100.000% |
You are browsing an online collection of $n$ items numbered from $1$ to $n$ arranged on a circle. The item to the right of each item $i$ is item $i + 1$, and the item to the right of item $n$ is item $1$. Similarly, the item to the left of each item $i$ is item $i - 1$, and the item to the left of item $1$ is item $n$.
The items have $m$ parameters numbered from $1$ to $m$. The value of parameter $j$ for item $i$ is an integer $a_{i, j}$.
While you are browsing, at any moment, there is a pointer directed at some item, called the current item.
Moreover, you can manage a set of filtering conditions. Each condition is a pair $(j, v)$, meaning that the $j$-th parameter of the item must be equal to $v$. The current item always satisfies all conditions in the set.
To browse through the collection, you can do operations. Each operation must have one of the following four kinds:
For each ordered pair of items $(i, j)$, answer the following question:
The first line contains two integers $n$ and $m$, denoting the number of items in the collection and the number of parameters each item has ($2 \le n \le 500$; $1 \le m \le 5$).
The $i$-th of the next $n$ lines contains $m$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}$, denoting the parameter values of item $i$ ($1 \le a_{i, j} \le n$).
Print $n$ lines containing $n$ integers each.
In the $i$-th line, the $j$-th integer must be equal to the smallest number of operations required to move the pointer from item $i$ to item $j$, starting with an empty set of filtering conditions.
9 3 5 3 7 5 3 4 5 3 7 5 3 2 5 3 4 5 3 7 2 3 7 5 3 7 2 3 7
0 1 2 1 2 3 1 2 1 1 0 1 1 2 2 1 3 2 2 1 0 1 1 2 1 3 2 3 2 1 0 1 1 1 3 2 3 2 2 1 0 1 1 3 2 3 1 2 1 1 0 1 2 2 2 1 3 1 2 1 0 1 2 2 1 3 1 2 2 1 0 1 1 1 3 1 2 3 2 1 0
In the example test, here is one possible fastest way to move from item $2$ to item $5$:
Here is one possible fastest way to move from item $8$ to item $3$: