#P9120. [春季测试 2023] 密码锁
[春季测试 2023] 密码锁
Problem Description
After the winter break, Little I returned to school and found that he had forgotten the password of his bike lock, so he asks you for help.
The combination lock on Little I’s bike has dials, and each dial has () positions. Each position on the lock contains a positive integer. The positive integer on the -th position of the -th dial is .

(An example of a lock, where . Each column represents one dial, and the positions on a dial are numbered from top to bottom.)
You may rotate each dial any number of times (possibly zero). Each time you rotate a dial once, its positions perform a cyclic shift. Formally, rotating the -th dial once moves the number on the -th position of the -th dial to position , while other dials do not change.

(An example of rotating a dial: rotating the second dial of the lock on the left once yields the lock on the right.)
To make it easier to remember, when setting the password, Little I wants the numbers in the same row to be as close as possible. Formally, for , define the looseness of row of the lock as
$$c(i) = \max \limits _ {j = 1} ^ n a _ {i, j} - \min \limits _ {j = 1} ^ n a _ {i, j}$$Also define the looseness of the entire lock as
Since an unlockable state is required to make as small as possible, Little I wants you to find the minimum possible value of .
Input Format
This problem contains multiple test cases. The problem guarantees that all test cases within the same test point have the same .
The first line contains two positive integers , representing the number of test cases and the number of positions on each dial.
Then there are test cases. Each test case is as follows:
The first line contains a positive integer , representing the number of dials.
The next lines each contain positive integers, where the -th integer in the -th line, , represents the number on the -th position of the -th dial.
Note that in the input matrix, each column corresponds to a dial, not each row.
Output Format
For each test case, output one line containing one integer, which is the minimum value of among all possible choices.
2 3
3
1 2 1
2 3 2
3 1 3
2
1 2
2 1
1 2
0
1
见选手目录下的 lock/lock2.in。
见选手目录下的 lock/lock2.ans。
见选手目录下的 lock/lock3.in。
见选手目录下的 lock/lock3.ans。
见选手目录下的 lock/lock4.in。
见选手目录下的 lock/lock4.ans。
见选手目录下的 lock/lock5.in。
见选手目录下的 lock/lock5.ans。
Hint
[Sample 1 Explanation]
The first sample corresponds to the example in the statement. After rotating the second dial once, each dial becomes , and the looseness is . It is easy to prove that the looseness cannot be smaller than anyway, so the output is .
The following four samples correspond to the cases respectively, and the values of in the samples have a certain gradient.
[Constraints]
Let be the sum of over all test cases within one test point.
For all data, it is guaranteed that , , .
This problem is divided into two types of test points.
The first type has twelve test points. It is guaranteed that , , .
| Test Point ID | |||
|---|---|---|---|
The second type has eight test points. It is guaranteed that , , .
| Test Point ID | |||
|---|---|---|---|
[Postscript]
After you finally managed to compute the value of , Little I tells you that he has already called a locksmith and used a hammer to brute-force it. After much persuasion, Little I promises that in the future he will not use a combination lock with at least ten thousand dials when locking his bike.
Translated by ChatGPT 5