CF2263B.Min Matrices
入门
通过率:0%
时间限制:1.50s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Farmer John heard from Elsie that Bessie's favorite number is k, so he wants to surprise her with a handcrafted present.
For a 2D matrix B, let f(B) denote the set of the minimum element of each row and each column of B.
Farmer John wants you to show him an n×n matrix A consisting of each number from 1 to n2 exactly once such that ∣f(A)∣=k, or state that it is impossible.
农夫约翰听艾尔西说,贝茜最喜爱的数字是 k,因此他想亲手制作一份礼物来给她一个惊喜。
对于一个二维矩阵 B,令 f(B) 表示 B 的每一行与每一列的最小元素所构成的集合。
农夫约翰希望你为他构造一个 n×n 的矩阵 A,其中恰好包含从 1 到 n2 的每个整数各一次,且满足 ∣f(A)∣=k;若无法构造,则说明这是不可能的。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000). The description of the test cases follows.
The first line of each test case contains two integers n and k (1≤n≤1000,0≤k≤2n) — the size of the matrix and the goal value of ∣f(A)∣.
It is guaranteed that the sum of n over all test cases does not exceed 1000.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤1000)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 k(1≤n≤1000,0≤k≤2n)—— 分别表示矩阵的大小以及目标值 ∣f(A)∣。
保证所有测试用例中 n 的总和不超过 1000。
输出格式
If there is no such matrix, print −1. Otherwise, print n lines with n integers each — an n×n matrix that satisfies the conditions of the problem.
If there are multiple solutions, you may output any of them.
如果不存在这样的矩阵,输出 −1。否则,输出 n 行,每行 n 个整数——即一个满足题目条件的 n×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×3 matrix where f(A) is empty.
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.
在第一个测试用例中,我们可以看出,不可能构造出一个满足 f(A) 为空集的 3×3 矩阵。
在第二个测试用例中,各行的最小值分别为 [5,3,1],各列的最小值分别为 [2,1,4]。因此,f(A)={1,2,3,4,5},故 ∣f(A)∣=5,符合要求。
输入解题思路,AI测评打分。不知道怎么写?