問題一覧 > 通常問題

No.223 Magic Square with One Specified Cell

レベル : / AI推定難易度 : 1975 / 実行時間制限 : 1ケース 5.000秒 / メモリ制限 : 512 MB / スペシャルジャッジ問題 (複数の解が存在する可能性があります)
タグ : (AC するまで非表示) / 解いたユーザー数 37
作問者 : 👑 tails
お気に入りにしたユーザー ProblemId : 558 / 自分の提出
問題文最終更新日: 2026-06-26 05:31:25
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. Back to the original
yukicoder contest 108 (順位表) の他の問題:

Problem Statement

Construct one magic square with $N$ rows and $N$ columns.
However, the number in row $Y$, column $X$ must be $Z$.

In this problem, a magic square with $N$ rows and $N$ columns is
an arrangement of the integers from $1$ to $N^2$, each used exactly once, in $N$ rows and $N$ columns,
such that the sums of the numbers in each row, each column, and each of the two diagonals are all equal.

An example of a magic square with $4$ rows and $4$ columns:

1 15 14 4
12 6 7 9
8 10 11 5
13 3 2 16

Since the algorithms involved rarely appear in competitive programming, you may refer to the section "魔方陣の作り方" (How to construct magic squares) of Wikipedia "魔方陣" (Magic square) as a hint.
To obtain the answer, you need to combine several algorithms.

Input

$N$ $X$ $Y$ $Z$

$N \in \{4,8,16\}$
$1 \le X \le N$
$1 \le Y \le N$
$1 \le Z \le N^2$
$X,Y,Z$ are all integers.

It is guaranteed that at least one magic square satisfying the conditions exists.

Output

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

Output one magic square with $N$ rows and $N$ columns. However, the number in row $Y$, column $X$ must be $Z$ ( $A_{Y,X} = Z$ ).
If there are multiple magic squares satisfying the conditions, you may output any of them.
On line $y$ of the output, print the numbers in row $y$ of the magic square, separated by spaces, in order starting from the number in column $1$, $A_{y,1}$.
(Extra spaces before or after the numbers are allowed.)

Sample

Sample 1
Input
4 1 2 10
Output
3 13 16 2
10 8 5 11
6 12 9 7
15 1 4 14

We construct one magic square with $4$ rows and $4$ columns in which the number in row $2$, column $1$ is $10$.
There are other magic squares satisfying the conditions besides the one above, and you may output any of them.

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