CF2249D Xor Permutation Matrix

Description

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](https://en.wikipedia.org/wiki/Bitwise_operation#XOR). Or determine that no such matrix exists.

Input Format

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. The only line of each test case contains two integers $ n $ and $ x $ ( $ 2\le n\le 2500 $ , $ 0\le x \lt n $ ). It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 2500 $ .

Output Format

For each test case, output $ -1 $ if no such matrix exists. Otherwise, output any valid $ n $ lines of the matrix. If several valid matrices exist, you may output any of them.

Explanation/Hint

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