CF1868A.Fill in the Matrix

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There is an empty matrix MM of size n×mn\times m.

Zhongkao examination is over, and Daniel would like to do some puzzle games. He is going to fill in the matrix MM using permutations of length mm. That is, each row of MM must be a permutation of length m†m^\dagger.

Define the value of the ii-th column in MM as vi=MEX⁡(M1,i,M2,i,…,Mn,i)‡v_i=\operatorname{MEX}(M_{1,i},M_{2,i},\ldots,M_{n,i})^\ddagger. Since Daniel likes diversity, the beauty of MM is s=MEX⁡(v1,v2,⋯ ,vm)s=\operatorname{MEX}(v_1,v_2,\cdots,v_m).

You have to help Daniel fill in the matrix MM and maximize its beauty.

†^\dagger A permutation of length mm is an array consisting of mm distinct integers from 00 to m−1m-1 in arbitrary order. For example, [1,2,0,4,3][1,2,0,4,3] is a permutation, but [0,1,1][0,1,1] is not a permutation (11 appears twice in the array), and [0,1,3][0,1,3] is also not a permutation (m−1=2m-1=2 but there is 33 in the array).

‡^\ddagger The MEX⁡\operatorname{MEX} of an array is the smallest non-negative integer that does not belong to the array. For example, MEX⁡(2,2,1)=0\operatorname{MEX}(2,2,1)=0 because 00 does not belong to the array, and MEX⁡(0,3,1,2)=4\operatorname{MEX}(0,3,1,2)=4 because 00, 11, 22 and 33 appear in the array, but 44 does not.

存在一个大小为 n×mn\times m 的空矩阵 MM。

中考结束,Daniel 想玩一些益智游戏。他打算用长度为 mm 的排列来填充矩阵 MM,即 MM 的每一行都必须是长度为 m†m^\dagger 的一个排列。

定义矩阵 MM 中第 ii 列的值为 vi=MEX⁡(M1,i,M2,i,…,Mn,i)‡v_i=\operatorname{MEX}(M_{1,i},M_{2,i},\ldots,M_{n,i})^\ddagger。由于 Daniel 喜欢多样性,矩阵 MM 的“美观度”定义为 s=MEX⁡(v1,v2,⋯ ,vm)s=\operatorname{MEX}(v_1,v_2,\cdots,v_m)。

你需要帮助 Daniel 填充矩阵 MM,使其美观度最大化。

†^\dagger 长度为 mm 的排列是指由 00 到 m−1m-1 这 mm 个互不相同的整数以任意顺序组成的数组。例如,[1,2,0,4,3][1,2,0,4,3] 是一个排列,但 [0,1,1][0,1,1] 不是排列(数字 11 在数组中出现了两次),[0,1,3][0,1,3] 也不是排列(此时 m−1=2m-1=2,但数组中却出现了 33)。

‡^\ddagger 一个数组的 MEX⁡\operatorname{MEX}(minimum excluded value)是指不属于该数组的最小非负整数。例如,MEX⁡(2,2,1)=0\operatorname{MEX}(2,2,1)=0,因为 00 不在该数组中;而 MEX⁡(0,3,1,2)=4\operatorname{MEX}(0,3,1,2)=4,因为 00、11、22 和 33 都出现在该数组中,但 44 没有出现。

输入格式

The first line of input contains a single integer tt (1≤t≤10001\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 nn and mm (1≤n,m≤2⋅1051\le n,m\le 2\cdot 10^5) — the size of the matrix.

It is guaranteed that the sum of n⋅mn\cdot m over all test cases does not exceed 2⋅1052\cdot 10^5.

输入的第一行包含一个整数 tt(1≤t≤10001\le t\le 1000),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例仅一行,包含两个整数 nn 和 mm(1≤n,m≤2⋅1051\le n,m\le 2\cdot 10^5),表示矩阵的大小。

保证所有测试用例中 n⋅mn\cdot m 的总和不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, in the first line output a single integer — the maximum beauty of MM.

Then output the matrix MM of size n×mn\times m — the matrix you find.

If there are multiple solutions, you may output any of them.

对于每个测试用例,在第一行输出一个整数——MM 的最大美观度。

然后输出一个大小为 n×mn\times m 的矩阵 MM——即你找到的矩阵。

如果存在多个解,你可以输出其中任意一个。

输入输出样例

  • 输入#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:

  • v1=MEX⁡(1,0,1,0)=2v_1=\operatorname{MEX}(1,0,1,0)=2;
  • v2=MEX⁡(0,2,0,2)=1v_2=\operatorname{MEX}(0,2,0,2)=1;
  • v3=MEX⁡(2,1,2,1)=0v_3=\operatorname{MEX}(2,1,2,1)=0.

Therefore, s=MEX⁡(2,1,0)=3s=\operatorname{MEX}(2,1,0)=3.

It can be shown that 33 is the maximum possible beauty of MM.

In the second test case, any permutation will make s=2s=2.

In the third test case:

  • v1=MEX⁡(3,5,1,4,4,2)=0v_1=\operatorname{MEX}(3,5,1,4,4,2)=0;
  • v2=MEX⁡(0,2,3,1,2,4)=5v_2=\operatorname{MEX}(0,2,3,1,2,4)=5;
  • v3=MEX⁡(1,1,2,3,5,0)=4v_3=\operatorname{MEX}(1,1,2,3,5,0)=4;
  • v4=MEX⁡(4,0,4,2,3,5)=1v_4=\operatorname{MEX}(4,0,4,2,3,5)=1;
  • v5=MEX⁡(2,4,5,5,0,1)=3v_5=\operatorname{MEX}(2,4,5,5,0,1)=3;
  • v6=MEX⁡(5,3,0,0,1,3)=2v_6=\operatorname{MEX}(5,3,0,0,1,3)=2.

Therefore, s=MEX⁡(0,5,4,1,3,2)=6s=\operatorname{MEX}(0,5,4,1,3,2)=6.

在第一个测试用例中:

  • v1=MEX⁡(1,0,1,0)=2v_1=\operatorname{MEX}(1,0,1,0)=2;
  • v2=MEX⁡(0,2,0,2)=1v_2=\operatorname{MEX}(0,2,0,2)=1;
  • v3=MEX⁡(2,1,2,1)=0v_3=\operatorname{MEX}(2,1,2,1)=0。

因此,s=MEX⁡(2,1,0)=3s=\operatorname{MEX}(2,1,0)=3。

可以证明,33 是矩阵 MM 可能达到的最大美观度。

在第二个测试用例中,任意排列均使得 s=2s=2。

在第三个测试用例中:

  • v1=MEX⁡(3,5,1,4,4,2)=0v_1=\operatorname{MEX}(3,5,1,4,4,2)=0;
  • v2=MEX⁡(0,2,3,1,2,4)=5v_2=\operatorname{MEX}(0,2,3,1,2,4)=5;
  • v3=MEX⁡(1,1,2,3,5,0)=4v_3=\operatorname{MEX}(1,1,2,3,5,0)=4;
  • v4=MEX⁡(4,0,4,2,3,5)=1v_4=\operatorname{MEX}(4,0,4,2,3,5)=1;
  • v5=MEX⁡(2,4,5,5,0,1)=3v_5=\operatorname{MEX}(2,4,5,5,0,1)=3;
  • v6=MEX⁡(5,3,0,0,1,3)=2v_6=\operatorname{MEX}(5,3,0,0,1,3)=2。

因此,s=MEX⁡(0,5,4,1,3,2)=6s=\operatorname{MEX}(0,5,4,1,3,2)=6。

输入解题思路,AI测评打分。不知道怎么写?

首页