第 1120 场 Codeforces Round(Div. 2)
B
最小值矩阵
Min Matrices
Codeforces
900
原题
← AC1 →

最小值矩阵AI (CI翻译)

Farmer John heard from Elsie that Bessie's favorite number is $k$, so he wants to surprise her with a handcrafted present.

Farmer John 从 Elsie 那里听说 Bessie 最喜欢的数字是 $k$,于是想为她制作一份手工礼物,给她一个惊喜。AI (CI翻译)

For a 2D matrix $B$, let $f(B)$ denote the set of the minimum element of each row and each column of $B$.

对于一个二维矩阵 $B$,令 $f(B)$ 表示由 $B$ 的每一行和每一列的最小元素组成的集合。AI (CI翻译)

Farmer John wants you to show him an $n \times n$ matrix $A$ consisting of each number from $1$ to $n^2$ exactly once such that $|f(A)| = k$, or state that it is impossible.

Farmer John 想让你给他构造一个 $n \times n$ 矩阵 $A$,其中 $1$ 到 $n^2$ 的每个数字恰好出现一次,并且满足 $|f(A)| = k$;如果无法构造,则说明这一点。AI (CI翻译)

Input

输入格式

AI (CI翻译)

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.

每个输入包含多组测试用例。第一行包含测试用例数 $t$($1 \le t \le 1000$)。接下来是各测试用例的描述。AI (CI翻译)

The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 1000, 0 \le k \le 2n$) — the size of the matrix and the goal value of $|f(A)|$.

每个测试用例的第一行包含两个整数 $n$ 和 $k$($1 \le n \le 1000, 0 \le k \le 2n$)——矩阵的大小以及目标值 $|f(A)|$。AI (CI翻译)

It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$.

保证所有测试用例的 $n$ 之和不超过 $1000$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

If there is no such matrix, print $-1$. Otherwise, print $n$ lines with $n$ integers each — an $n \times n$ matrix that satisfies the conditions of the problem.

如果不存在这样的矩阵,输出 $-1$。否则,输出 $n$ 行,每行包含 $n$ 个整数,即一个满足题目条件的 $n \times n$ 矩阵。AI (CI翻译)

If there are multiple solutions, you may output any of them.

如果有多个解,可以输出任意一个。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first test case, we can see that it is impossible to construct such a $3 \times 3$ matrix where $f(A)$ is empty.

在第一个测试用例中,可以发现无法构造出一个使 $f(A)$ 为空的 $3 \times 3$ 矩阵。AI (CI翻译)

In the second test case, we can see the minimums of the rows are $[5, 3, 1]$ respectively, and the minimums of the columns are $[2, 1, 4]$ respectively. Therefore, $f(A) = \{1, 2, 3, 4, 5\}$, so $|f(A)| = 5$ as desired.

在第二个测试用例中,各行的最小值依次为 $[5, 3, 1]$,各列的最小值依次为 $[2, 1, 4]$。因此,$f(A) = \{1, 2, 3, 4, 5\}$,所以 $|f(A)| = 5$,符合要求。AI (CI翻译)

样例

Input #1
5
3 0
3 5
5 5
4 3
1 1
Output #1
-1
8 5 9
6 3 7
2 1 4
16 14 17 15 3
25 22 5 23 24
8 1 9 6 7
4 18 21 19 20
12 10 13 2 11
-1
1
InputCopy OutputCopy
题目信息
题号
2263B
时间限制
1.5 s
内存限制
256 MB
标签
构造

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。