#P13052. [GCJ 2020 Qualification] Indicium

    ID: 12857 Type: RemoteJudge 20000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 6 Uploaded By: Tags>2020网络流Special Judge二分图Google Code Jam

[GCJ 2020 Qualification] Indicium

题目描述

Indicium means "trace" in Latin. In this problem we work with Latin squares and matrix traces.

A Latin square is an N\mathbf{N}-by-N\mathbf{N} square matrix in which each cell contains one of N\mathbf{N} different values, such that no value is repeated within a row or a column. In this problem, we will deal only with "natural Latin squares" in which the N\mathbf{N} values are the integers between 1 and N\mathbf{N}.

The trace of a square matrix is the sum of the values on the main diagonal (which runs from the upper left to the lower right).

Given values N\mathbf{N} and K\mathbf{K}, produce any N\mathbf{N}-by-N\mathbf{N} "natural Latin square" with trace K\mathbf{K}, or say it is impossible. For example, here are two possible answers for N=3,K=6\mathbf{N}=3, \mathbf{K}=6. In each case, the values that contribute to the trace are underlined.

$\begin{array}{llll}\underline{2} & 1 & 3 & \underline{3} \\3 & \underline{2} & 1 & 1 \\1 & 3 & \underline{2} & 2\end{array} \begin{array}{lll}1 & 2 \\ \underline{2} & 3 \\3 & \underline{1}\end{array}$

输入格式

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow. Each consists of one line containing two integers N\mathbf{N} and K\mathbf{K} : the desired size of the matrix and the desired trace.

输出格式

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is IMPOSSIBLE if there is no answer for the given parameters or POSSIBLE otherwise. In the latter case, output N\mathbf{N} more lines of N\mathbf{N} integers each, representing a valid "natural Latin square" with a trace of K\mathbf{K}, as described above.

2
3 6
2 3
Case #1: POSSIBLE
2 1 3
3 2 1
1 3 2
Case #2: IMPOSSIBLE

提示

Sample Explanation

Sample Case #1 is the one described in the problem statement.

Sample Case #2 has no answer. The only possible 2-by-2 "natural Latin squares" are as follows:

1 2 2 1
2 1 1 2

These have traces of 2 and 4, respectively. There is no way to get a trace of 3.

Limits

  • $\mathrm{N} \leqslant \mathrm{K} \leqslant \mathrm{N}^{2}$.

Test set 1 (7 Pts, Visible Verdict)

  • T=44\mathrm{T}=44.
  • 2N52 \leqslant \mathrm{N} \leqslant 5.

Test set 2 (25 Pts, Hidden Verdict)

  • 1T1001 \leqslant \mathrm{T} \leqslant 100.
  • 2N502 \leqslant \mathrm{N} \leqslant 50.