QOJ.ac

QOJ

時間限制: 20 s 記憶體限制: 1024 MB 總分: 32

#12421. 拉丁方阵

统计

Indicium 在拉丁语中意为“迹”。在本题中,我们将研究拉丁方阵和矩阵的迹。

拉丁方阵是一个 $N \times N$ 的方阵,其中每个单元格包含 $N$ 个不同值中的一个,且每一行或每一列中没有任何值重复。在本题中,我们仅处理“自然拉丁方阵”,即 $N$ 个值为 $1$ 到 $N$ 之间的整数。

方阵的迹是主对角线(从左上角到右下角)上各数值之和。

给定 $N$ 和 $K$,请构造任意一个迹为 $K$ 的 $N \times N$ “自然拉丁方阵”,或者说明这是不可能的。例如,以下是 $N = 3, K = 6$ 时两个可能的答案。在每种情况下,对迹有贡献的数值都已加下划线。

$\underline{2} \ 1 \ 3 \quad \underline{3} \ 1 \ 2$ $3 \ \underline{2} \ 1 \quad 1 \ \underline{2} \ 3$ $1 \ 3 \ \underline{2} \quad 2 \ 3 \ \underline{1}$

输入格式

输入的第一行包含测试用例的数量 $T$。接下来是 $T$ 个测试用例。每个测试用例包含一行,其中有两个整数 $N$ 和 $K$:表示所需的矩阵大小和所需的迹。

输出格式

对于每个测试用例,输出一行 Case #x: y,其中 $x$ 是测试用例编号(从 $1$ 开始),如果对于给定的参数没有答案,则 $y$ 为 IMPOSSIBLE,否则为 POSSIBLE。如果是后者,则再输出 $N$ 行,每行包含 $N$ 个整数,表示一个迹为 $K$ 的有效“自然拉丁方阵”,如上所述。

数据范围

$N \le K \le N^2$。

测试集 1(可见判定) $T = 44$。 $2 \le N \le 5$。

测试集 2(隐藏判定) $1 \le T \le 100$。 $2 \le N \le 50$。

样例

样例输入 1

2
3 6
2 3

样例输出 1

Case #1: POSSIBLE
2 1 3
3 2 1
1 3 2
Case #2: IMPOSSIBLE

说明

样例 1 即为题目描述中给出的例子。

样例 2 没有答案。仅有的 $2 \times 2$ “自然拉丁方阵”如下:

$1 \ 2 \quad 2 \ 1$ $2 \ 1 \quad 1 \ 2$

它们的迹分别为 $2$ 和 $4$。无法得到迹为 $3$ 的情况。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.