Codeforces Round 1024 (Div. 1)
A
Mex in the Grid
Codeforces
1300
原题
←B →
翻译中…

You are given $n^2$ cards with values from $0$ to $n^2-1$. You are to arrange them in a $n$ by $n$ grid such that there is exactly one card in each cell.

翻译中…

The MEX (minimum excluded value) of a subgrid$^{\text{∗}}$ is defined as the smallest non-negative integer that does not appear in the subgrid.

翻译中…

Your task is to arrange the cards such that the sum of MEX values over all $\left(\frac{n(n+1)}{2}\right)^2$ subgrids is maximized.

翻译中…

$^{\text{∗}}$A subgrid of a $n$ by $n$ grid is specified by four numbers $l_1, r_1, l_2, r_2$ satisfying $1\le l_1\le r_1\le n$ and $1\le l_2\le r_2\le n$. The element in the $i$-th row and the $j$-th column of the grid is part of the subgrid if and only if $l_1\le i\le r_1$ and $l_2\le j\le r_2$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($1\le n\le 500$) — the side length of the grid.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$.

翻译中…

Output

翻译中…

For each test case, output $n$ lines, each containing $n$ integers representing the elements of the grid.

翻译中…

If there are multiple answers, you can output any of them.

翻译中…

Note

翻译中…

In the first test case, one valid arrangement is:

翻译中…

0123

翻译中…

There are $9$ subgrids in total, and the $4$ of them with non-zero MEX are shown below:

翻译中…

0 values:$[0]$ — MEX: *$1$01 values:$[0, 1]$ * — MEX: *$2$02 values:$[0, 2]$ * — MEX: *$1$0123 values:$[0, 1, 2, 3]$ * — MEX: $4$

翻译中…

The sum of MEX over all subgrids would be $1+2+1+4 = 8$. It can be proven that no other arrangements have a larger sum of MEX values.

翻译中…

样例

Input #1
2
2
3
Output #1
0 1
2 3
8 4 5
6 0 1
7 2 3
InputCopy OutputCopy
题目信息
题号
2101A
时间限制
2 s
内存限制
256 MB
标签
构造
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。