QOJ.ac

QOJ

実行時間制限: 1 s メモリ制限: 512 MB 満点: 100 ハック可能 ✓

#17358. Mex Grid

統計

Yuki 有一个 $n$ 行 $m$ 列的表格。

她希望在每个格子中填入一个不大于 $n\cdot m$ 的非负整数,使得表格每一行的 $\operatorname{mex}^\ast$ 与每一列的 $\operatorname{mex}$ 之和最大化。

形式化地,设 $a_{i,j}$ 表示表格第 $i$ 行第 $j$ 列中的数,则她希望最大化:

$$ \sum\limits_{i = 1}^{n} \operatorname{mex}\{a_{i,1}, \ldots, a_{i,m}\} + \sum\limits_{j = 1}^{m} \operatorname{mex}\{a_{1,j}, \ldots, a_{n,j}\} $$

遗憾的是,Yuki 并不知道应该如何构造这个表格,你能帮帮她吗?

$^\ast$:一个序列的 $\operatorname{mex}$ 为该序列中未出现过的最小非负整数,例如 $\operatorname{mex}\{0,1,2\}=3$,$\operatorname{mex}\{1,0,3,1\}=2$,$\operatorname{mex} \varnothing=0$。

输入格式

本题包含多组测试数据。

第一行包含一个正整数 $t\ (1 \le t \le 10^5)$,表示测试数据组数。

对于每组测试数据:

  • 共一行,包含两个正整数 $n,m\ (1 \le n \cdot m \le 5\cdot10^5)$。

保证所有测试数据的 $n \cdot m$ 之和不超过 $5\cdot10^5$。

输出格式

对于每组测试数据,输出 $n$ 行,每行包含 $m$ 个不大于 $n\cdot m$ 的非负整数,表示你构造的表格中的数。

样例输入

3
2 4
1 1
3 4

样例输出

1 2 0 3
0 3 1 2
0
1 2 0 1
0 1 2 0
2 0 1 3

样例解释

对于第 $1$ 组测试数据:

  • 给出的表格的第 $1$ 行和第 $2$ 行的 $\operatorname{mex}$ 为 $4$,第 $1$ 列和第 $3$ 列的 $\operatorname{mex}$ 为 $2$,第 $2$ 列和第 $4$ 列的 $\operatorname{mex}$ 为 $0$,每一行的 $\operatorname{mex}$ 与每一列的 $\operatorname{mex}$ 之和为 $12$。
  • 可以证明,在所有方案中,表格每一行的 $\operatorname{mex}$ 与每一列的 $\operatorname{mex}$ 之和的最大值为 $12$。

对于第 $2$ 组测试数据:

  • 显然 $a_{1,1}=0$ 可以使表格每一行的 $\operatorname{mex}$ 与每一列的 $\operatorname{mex}$ 之和最大。

对于第 $3$ 组测试数据:

  • 可以证明,在所有方案中,表格每一行的 $\operatorname{mex}$ 与每一列的 $\operatorname{mex}$ 之和的最大值为 $21$。

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.