#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 nn dials, and each dial has kk (k4k \leq 4) positions. Each position on the lock contains a positive integer. The positive integer on the ii-th position of the jj-th dial is ai,ja _ {i, j}.

(An example of a lock, where k=n=3k = n = 3. 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 jj-th dial once moves the number on the ii-th position of the jj-th dial to position ((imodk)+1)((i \bmod k) + 1), 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 1ik1 \leq i \leq k, define the looseness of row ii 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

C=max1ikc(i)C = \max \limits _ {1 \leq i \leq k} c(i)

Since an unlockable state is required to make CC as small as possible, Little I wants you to find the minimum possible value of CC.

Input Format

This problem contains multiple test cases. The problem guarantees that all test cases within the same test point have the same kk.

The first line contains two positive integers T,kT, k, representing the number of test cases and the number of positions on each dial.

Then there are TT test cases. Each test case is as follows:

The first line contains a positive integer nn, representing the number of dials.

The next kk lines each contain nn positive integers, where the jj-th integer in the ii-th line, ai,ja _ {i,j}, represents the number on the ii-th position of the jj-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 CC 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 {1,2,3}\{1, 2, 3\}, and the looseness is 00. It is easy to prove that the looseness cannot be smaller than 00 anyway, so the output is 00.

The following four samples correspond to the cases k=1,2,3,4k = 1, 2, 3, 4 respectively, and the values of nn in the samples have a certain gradient.

[Constraints]

Let n\sum n be the sum of nn over all test cases within one test point.

For all data, it is guaranteed that 1T1 \leq T, 1k41 \leq k \leq 4, 1ai,j3×1041 \leq a _ {i ,j} \leq 3 \times 10 ^ 4.

This problem is divided into two types of test points.

The first type has twelve test points. It is guaranteed that k3k \leq 3, n5×104n \leq 5 \times 10 ^ 4, n1.5×105\sum n \leq 1.5 \times 10 ^ 5.

Test Point ID nn \leq n\sum n \leq k=k =
11 2020 100100 11
22 5×1045 \times 10 ^ 4 1.5×1051.5 \times 10 ^ 5
33 2020 100100 22
44 100100 10001000
55 20002000 10410 ^ 4
66 5×1045 \times 10 ^ 4 1.5×1051.5 \times 10 ^ 5
77 1010 5050 33
88 5050 500500
99 300300 30003000
1010 30003000 2×1042 \times 10 ^ 4
1111 3×1043 \times 10 ^ 4 1.2×1051.2 \times 10 ^ 5
1212 5×1045 \times 10 ^ 4 1.5×1051.5 \times 10 ^ 5

The second type has eight test points. It is guaranteed that k=4k = 4, n104n \leq 10 ^ 4, n3×104\sum n \leq 3 \times 10 ^ 4.

Test Point ID nn \leq n\sum n \leq k=k =
1313 1010 5050 44
1414 5050 500500
1515 200200 20002000
1616 500500 40004000
1717 25002500 10410 ^ 4
1818 50005000 2×1042 \times 10 ^ 4
1919 10410 ^ 4 3×1043 \times 10 ^ 4
2020

[Postscript]

After you finally managed to compute the value of CC, 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