A16251 | Fill in the Matrix
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
There is an empty matrix $M$ of size $n\times m$ .
Zhongkao examination is over, and Daniel would like to do some puzzle games. He is going to fill in the matrix $M$ using permutations of length $m$ . That is, each row of $M$ must be a permutation of length $m^\dagger$ .
Define the value of the $i$ -th column in $M$ as $v_i=\operatorname{MEX}(M_{1,i},M_{2,i},\ldots,M_{n,i})^\ddagger$ . Since Daniel likes diversity, the beauty of $M$ is $s=\operatorname{MEX}(v_1,v_2,\cdots,v_m)$ .
You have to help Daniel fill in the matrix $M$ and maximize its beauty.
$^\dagger$ A permutation of length $m$ is an array consisting of $m$ distinct integers from $0$ to $m-1$ in arbitrary order. For example, $[1,2,0,4,3]$ is a permutation, but $[0,1,1]$ is not a permutation ( $1$ appears twice in the array), and $[0,1,3]$ is also not a permutation ( $m-1=2$ but there is $3$ in the array).
$^\ddagger$ The $\operatorname{MEX}$ of an array is the smallest non-negative integer that does not belong to the array. For example, $\operatorname{MEX}(2,2,1)=0$ because $0$ does not belong to the array, and $\operatorname{MEX}(0,3,1,2)=4$ because $0$ , $1$ , $2$ and $3$ appear in the array, but $4$ does not.
Zhongkao examination is over, and Daniel would like to do some puzzle games. He is going to fill in the matrix $M$ using permutations of length $m$ . That is, each row of $M$ must be a permutation of length $m^\dagger$ .
Define the value of the $i$ -th column in $M$ as $v_i=\operatorname{MEX}(M_{1,i},M_{2,i},\ldots,M_{n,i})^\ddagger$ . Since Daniel likes diversity, the beauty of $M$ is $s=\operatorname{MEX}(v_1,v_2,\cdots,v_m)$ .
You have to help Daniel fill in the matrix $M$ and maximize its beauty.
$^\dagger$ A permutation of length $m$ is an array consisting of $m$ distinct integers from $0$ to $m-1$ in arbitrary order. For example, $[1,2,0,4,3]$ is a permutation, but $[0,1,1]$ is not a permutation ( $1$ appears twice in the array), and $[0,1,3]$ is also not a permutation ( $m-1=2$ but there is $3$ in the array).
$^\ddagger$ The $\operatorname{MEX}$ of an array is the smallest non-negative integer that does not belong to the array. For example, $\operatorname{MEX}(2,2,1)=0$ because $0$ does not belong to the array, and $\operatorname{MEX}(0,3,1,2)=4$ because $0$ , $1$ , $2$ and $3$ appear in the array, but $4$ does not.
输入格式
The first line of input contains a single integer $t$ ( $1\le t\le 1000$ ) — the number of test cases. The description of test cases follows.
The only line of each test case contains two integers $n$ and $m$ ( $1\le n,m\le 2\cdot 10^5$ ) — the size of the matrix.
It is guaranteed that the sum of $n\cdot m$ over all test cases does not exceed $2\cdot 10^5$ .
The only line of each test case contains two integers $n$ and $m$ ( $1\le n,m\le 2\cdot 10^5$ ) — the size of the matrix.
It is guaranteed that the sum of $n\cdot m$ over all test cases does not exceed $2\cdot 10^5$ .
输出格式
For each test case, in the first line output a single integer — the maximum beauty of $M$ .
Then output the matrix $M$ of size $n\times m$ — the matrix you find.
If there are multiple solutions, you may output any of them.
Then output the matrix $M$ of size $n\times m$ — the matrix you find.
If there are multiple solutions, you may output any of them.
输入输出样例
输入 #1
4 4 3 1 16 6 6 2 1
输出 #1
3 1 0 2 0 2 1 1 0 2 0 2 1 2 14 7 15 4 10 0 8 6 1 2 3 5 9 11 12 13 6 3 0 1 4 2 5 5 2 1 0 4 3 1 3 2 4 5 0 4 1 3 2 5 0 4 2 5 3 0 1 2 4 0 5 1 3 0 0 0
In the first test case:
- $v_1=\operatorname{MEX}(1,0,1,0)=2$ ;
- $v_2=\operatorname{MEX}(0,2,0,2)=1$ ;
- $v_3=\operatorname{MEX}(2,1,2,1)=0$ .
Therefore, $s=\operatorname{MEX}(2,1,0)=3$ .
It can be shown that $3$ is the maximum possible beauty of $M$ .
In the second test case, any permutation will make $s=2$ .
In the third test case:
- $v_1=\operatorname{MEX}(3,5,1,4,4,2)=0$ ;
- $v_2=\operatorname{MEX}(0,2,3,1,2,4)=5$ ;
- $v_3=\operatorname{MEX}(1,1,2,3,5,0)=4$ ;
- $v_4=\operatorname{MEX}(4,0,4,2,3,5)=1$ ;
- $v_5=\operatorname{MEX}(2,4,5,5,0,1)=3$ ;
- $v_6=\operatorname{MEX}(5,3,0,0,1,3)=2$ .
Therefore, $s=\operatorname{MEX}(0,5,4,1,3,2)=6$ .
- $v_1=\operatorname{MEX}(1,0,1,0)=2$ ;
- $v_2=\operatorname{MEX}(0,2,0,2)=1$ ;
- $v_3=\operatorname{MEX}(2,1,2,1)=0$ .
Therefore, $s=\operatorname{MEX}(2,1,0)=3$ .
It can be shown that $3$ is the maximum possible beauty of $M$ .
In the second test case, any permutation will make $s=2$ .
In the third test case:
- $v_1=\operatorname{MEX}(3,5,1,4,4,2)=0$ ;
- $v_2=\operatorname{MEX}(0,2,3,1,2,4)=5$ ;
- $v_3=\operatorname{MEX}(1,1,2,3,5,0)=4$ ;
- $v_4=\operatorname{MEX}(4,0,4,2,3,5)=1$ ;
- $v_5=\operatorname{MEX}(2,4,5,5,0,1)=3$ ;
- $v_6=\operatorname{MEX}(5,3,0,0,1,3)=2$ .
Therefore, $s=\operatorname{MEX}(0,5,4,1,3,2)=6$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted