CF2263B.Min Matrices

入门

通过率:0%

时间限制:1.50s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

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

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

农夫约翰听艾尔西说,贝茜最喜爱的数字是 kk,因此他想亲手制作一份礼物来给她一个惊喜。

对于一个二维矩阵 BB,令 f(B)f(B) 表示 BB 的每一行与每一列的最小元素所构成的集合。

农夫约翰希望你为他构造一个 n×nn \times n 的矩阵 AA,其中恰好包含从 11 到 n2n^2 的每个整数各一次,且满足 ∣f(A)∣=k|f(A)| = k;若无法构造,则说明这是不可能的。

输入格式

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

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

It is guaranteed that the sum of nn over all test cases does not exceed 10001000.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤10001 \le t \le 1000)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 kk(1≤n≤10001 \le n \le 1000,0≤k≤2n0 \le k \le 2n)—— 分别表示矩阵的大小以及目标值 ∣f(A)∣|f(A)|。

保证所有测试用例中 nn 的总和不超过 10001000。

输出格式

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

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

如果不存在这样的矩阵,输出 −1-1。否则,输出 nn 行,每行 nn 个整数——即一个满足题目条件的 n×nn \times n 矩阵。

如果存在多个解,你可以输出其中任意一个。

输入输出样例

  • 输入#1

    5
    3 0
    3 5
    5 5
    4 3
    1 1

    输出#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

说明/提示

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

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

在第一个测试用例中,我们可以看出,不可能构造出一个满足 f(A)f(A) 为空集的 3×33 \times 3 矩阵。

在第二个测试用例中,各行的最小值分别为 [5,3,1][5, 3, 1],各列的最小值分别为 [2,1,4][2, 1, 4]。因此,f(A)={1,2,3,4,5}f(A) = \{1, 2, 3, 4, 5\},故 ∣f(A)∣=5|f(A)| = 5,符合要求。

输入解题思路,AI测评打分。不知道怎么写?

首页