310670: CF1868A. Fill in the Matrix

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

A. Fill in the Matrixtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

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.

Input

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$.

Output

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.

ExampleInput
4
4 3
1 16
6 6
2 1
Output
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
Note

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$.

Output

题目大意:
题目名为“填充矩阵”。有一个空的 n 行 m 列的矩阵 M。要求用长度为 m 的排列填充矩阵 M,即矩阵 M 的每一行必须是长度为 m 的排列。排列是指由 m 个从 0 到 m-1 的不同整数组成的任意顺序的数组。

定义矩阵 M 第 i 列的值为 v_i = MEX(M_{1,i}, M_{2,i}, ..., M_{n,i}),其中 MEX 是最小非负整数,该整数不在给定的数组中。例如,MEX(2,2,1)=0,因为 0 不在数组中。

矩阵 M 的美丽值 s 定义为 s = MEX(v_1, v_2, ..., v_m)。目标是填充矩阵 M,使其美丽值 s 最大化。

输入输出数据格式:
输入:
- 第一行包含一个整数 t(1≤t≤1000),表示测试用例的数量。
- 每个测试用例包含一行,有两个整数 n 和 m(1≤n,m≤2×10^5),表示矩阵的大小。

输出:
- 对于每个测试用例,首先输出一个整数,表示矩阵 M 的最大美丽值。
- 然后输出你找到的 n 行 m 列的矩阵 M。
- 如果有多个解决方案,可以输出其中任何一个。

示例输入输出:
输入:
```
4
4 3
1 16
6 6
2 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
```

注意:
- 在第一个测试案例中,v_1 = MEX(1,0,1,0) = 2;v_2 = MEX(0,2,0,2) = 1;v_3 = MEX(2,1,2,1) = 0。因此,s = MEX(2,1,0) = 3。可以证明 3 是 M 的最大可能美丽值。
- 在第二个测试案例中,任何排列都会使 s = 2。
- 在第三个测试案例中,v_1 = MEX(3,5,1,4,4,2) = 0;v_2 = MEX(0,2,3,1,2,4) = 5;v_3 = MEX(1,1,2,3,5,0) = 4;v_4 = MEX(4,0,4,2,3,5) = 1;v_5 = MEX(2,4,5,5,0,1) = 3;v_6 = MEX(5,3,0,0,1,3) = 2。因此,s = MEX(0,5,4,1,3,2) = 6。题目大意: 题目名为“填充矩阵”。有一个空的 n 行 m 列的矩阵 M。要求用长度为 m 的排列填充矩阵 M,即矩阵 M 的每一行必须是长度为 m 的排列。排列是指由 m 个从 0 到 m-1 的不同整数组成的任意顺序的数组。 定义矩阵 M 第 i 列的值为 v_i = MEX(M_{1,i}, M_{2,i}, ..., M_{n,i}),其中 MEX 是最小非负整数,该整数不在给定的数组中。例如,MEX(2,2,1)=0,因为 0 不在数组中。 矩阵 M 的美丽值 s 定义为 s = MEX(v_1, v_2, ..., v_m)。目标是填充矩阵 M,使其美丽值 s 最大化。 输入输出数据格式: 输入: - 第一行包含一个整数 t(1≤t≤1000),表示测试用例的数量。 - 每个测试用例包含一行,有两个整数 n 和 m(1≤n,m≤2×10^5),表示矩阵的大小。 输出: - 对于每个测试用例,首先输出一个整数,表示矩阵 M 的最大美丽值。 - 然后输出你找到的 n 行 m 列的矩阵 M。 - 如果有多个解决方案,可以输出其中任何一个。 示例输入输出: 输入: ``` 4 4 3 1 16 6 6 2 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 ``` 注意: - 在第一个测试案例中,v_1 = MEX(1,0,1,0) = 2;v_2 = MEX(0,2,0,2) = 1;v_3 = MEX(2,1,2,1) = 0。因此,s = MEX(2,1,0) = 3。可以证明 3 是 M 的最大可能美丽值。 - 在第二个测试案例中,任何排列都会使 s = 2。 - 在第三个测试案例中,v_1 = MEX(3,5,1,4,4,2) = 0;v_2 = MEX(0,2,3,1,2,4) = 5;v_3 = MEX(1,1,2,3,5,0) = 4;v_4 = MEX(4,0,4,2,3,5) = 1;v_5 = MEX(2,4,5,5,0,1) = 3;v_6 = MEX(5,3,0,0,1,3) = 2。因此,s = MEX(0,5,4,1,3,2) = 6。

加入题单

上一题 下一题 算法标签: