异或排列矩阵AI (CI翻译)
You are given two integers $n$ and $x$ ($0 \le x \le n-1$).
Construct a matrix $A$ of size $n\times n$ satisfying all of the following conditions:
- For every
$1 \le i, j \le n$,$0 \le A_{i,j}\le n-1$; - Every row in
$A$forms a permutation of$0, 1, \ldots, n-1$; - Every column in
$A$forms a permutation of$0, 1, \ldots, n-1$; - For every
$1 \le i, j\le n-1$,$$ A_{i,j} \oplus A_{i+1,j} \oplus A_{i,j+1} \oplus A_{i+1,j+1} = x. $$Here,$\oplus$denotes the bitwise XOR operation.
Or determine that no such matrix exists.
Input
输入格式
AI (CI翻译)Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 180$). The description of the test cases follows.
每个测试包含多个测试用例。第一行包含测试用例数量 $t$($1 \le t \le 180$)。接下来是各个测试用例的描述。AI (CI翻译)
The only line of each test case contains two integers $n$ and $x$ ($2\le n\le 2500$, $0\le x \lt n$).
每个测试用例仅包含一行,其中有两个整数 $n$ 和 $x$($2\le n\le 2500$,$0\le x \lt n$)。AI (CI翻译)
It is guaranteed that the sum of $n$ over all test cases does not exceed $2500$.
保证所有测试用例的 $n$ 之和不超过 $2500$。AI (CI翻译)
Output
输出格式
AI (CI翻译)For each test case, output $-1$ if no such matrix exists. Otherwise, output any valid $n$ lines of the matrix.
对于每个测试用例,如果不存在满足条件的矩阵,输出 $-1$。否则,输出矩阵中任意一个合法的 $n$ 行。AI (CI翻译)
If several valid matrices exist, you may output any of them.
如果存在多个合法矩阵,可以输出其中任意一个。AI (CI翻译)
Note
说明
AI (CI翻译)In the first test case, the displayed matrix has both rows and columns equal to permutations of $0,1$, and its only adjacent $2\times2$ submatrix has XOR $0$.
在第一个测试用例中,给出的矩阵的行和列都构成 $0,1$ 的排列,并且它唯一的相邻 $2\times2$ 子矩阵的异或值为 $0$。AI (CI翻译)
The second and third test cases are impossible. In the last two test cases, every adjacent $2\times2$ submatrix has XOR, respectively, $1$ and $0$.
第二个和第三个测试用例无解。在最后两个测试用例中,每个相邻 $2\times2$ 子矩阵的异或值分别为 $1$ 和 $0$。AI (CI翻译)
样例
5
2 0
2 1
3 0
4 1
4 0
0 1
1 0
-1
-1
0 2 1 3
2 1 3 0
1 3 0 2
3 0 2 1
0 1 2 3
1 0 3 2
2 3 0 1
3 2 1 0
题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。