問題一覧 > 通常問題

No.3334 I hate Image Convolution

レベル : / 実行時間制限 : 1ケース 3.000秒 / メモリ制限 : 512 MB / スペシャルジャッジ問題 (複数の解が存在する可能性があります)
タグ : (解説公開後に AC するまで非表示) / 解いたユーザー数 23
作問者 : 👑 kencho / テスター : cn_449 👑 みうね
お気に入りにしたユーザー ProblemId : 12731 / 自分の提出
問題文最終更新日: 2025-11-08 01:58:21
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Show the original
yukicoder contest 487 (順位表) の他の問題:

Story

You, a genius hacker, have hacked an enemy robot and obtained secret image data.
However, you noticed that the image data had been subjected to a light two-dimensional convolution, and that the order of its pixels was randomly shuffled when it passed through security.
It has become difficult to uniquely restore the original image data, but constructing an example of what the original image could have been may still reveal something.
Write a program that constructs one example of the original image data.

Problem Statement

There is an $N \times N$ matrix $A$. Each element of the matrix $A$ is known to be a non-negative integer.

Now, define an $(N-1) \times (N-1)$ matrix $B$ as follows. $$ B_{i,j} = A_{i, j} + A_{i, j+1} + A_{i+1, j} + A_{i+1, j+1} $$ You are given an array $C$ obtained by arranging the $(N-1)^2$ elements of $B$ in random order. Find one example of a matrix that could be $A$.
If no such matrix exists, report that fact.

$T$ test cases are given; solve each of them.

Constraints

  • All input values are integers
  • $1 \leq T \leq 10^4$
  • $2 \leq N \leq 700$
  • $0 \leq C_i \leq 10^6 - 1$
  • The sum of $N^2$ in $1$ input file does not exceed $5 \times 10^5$

Input

The input is given from standard input in the following format. Here, $case_i$ denotes the $i$-th case.

$T$
$case_1$
$case_2$
$\vdots$
$case_T$

Each test case is given in the following format.

$N$
$C_1\ C_2\ \cdots\ C_{(N-1)^2}$

Output

For each case, output -1 if no matrix that could be $A$ exists; otherwise, output one example of such a matrix in the following format.

$A_{1, 1}\ A_{1, 2}\ \cdots\ A_{1, N}$
$A_{2, 1}\ A_{2, 2}\ \cdots\ A_{2, N}$
$\vdots$
$A_{N, 1}\ A_{N, 2}\ \cdots\ A_{N, N}$

Print a newline at the end of the output for each case.

Sample

Sample 1
Input
3
3
2 0 2 5
2
9
4
3 1 4 1 5 9 2 6 5
Output
2 0 1
0 0 1
0 0 4
2 0
2 5
1 1 0 0
1 0 0 1
2 2 0 4
1 4 0 0

For test case $1$, if we let $$ A=\begin{pmatrix} 2 & 0 & 1 \\ 0 & 0 & 1 \\ 0 & 0 & 4 \end{pmatrix} $$ then $$ \begin{aligned} B_{1,1} &= A_{1,1}+A_{1,2}+A_{2,1}+A_{2,2} = 2 \\ B_{1,2} &= A_{1,2}+A_{1,3}+A_{2,2}+A_{2,3} = 2 \\ B_{2,1} &= A_{2,1}+A_{2,2}+A_{3,1}+A_{3,2} = 0 \\ B_{2,2} &= A_{2,2}+A_{2,3}+A_{3,2}+A_{3,3} = 5 \\ \end{aligned} $$ and $[2,2,0,5]$ matches $C$ when rearranged appropriately, so this output is valid as an example of $A$.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。