Universal Cup Judging System

Universal Cup

Time Limit: 2.0 s Memory Limit: 1024 MB Total points: 100 Hackable ✓
Statistics

一个大小为 $N$ 的“好矩阵”是一个由正整数组成的 $N \times N$ 矩阵,使得每一行、每一列以及两条主对角线的异或和均为 0。

更准确地说,如果一个 $N \times N$ 矩阵 $A$ 满足以下所有条件,则称其为大小为 $N$ 的好矩阵。这里,$x \oplus y$ 表示 $x$ 和 $y$ 的按位异或,且 $\bigoplus_{i=1}^N a_i = a_1 \oplus \dots \oplus a_N$。

  • $A_{i,j} (1 \le i, j \le N)$ 是正整数
  • 对于每个 $i = 1, 2, \dots, N$,$\bigoplus_{j=1}^N A_{i,j} = 0$
  • 对于每个 $j = 1, 2, \dots, N$,$\bigoplus_{i=1}^N A_{i,j} = 0$
  • $\bigoplus_{i=1}^N A_{i,i} = 0$
  • $\bigoplus_{i=1}^N A_{i,N-i+1} = 0$

给定一个正整数 $N$。

在所有大小为 $N$ 的好矩阵中,输出一个使得所有元素的总和 $\sum_{1 \le i, j \le N} A_{i,j}$ 最小的矩阵。

如果不存在大小为 $N$ 的好矩阵,则报告该情况。

输入格式

输入包含一个整数 $N$。($1 \le N \le 2 \times 10^3$)

输出格式

如果不存在大小为 $N$ 的好矩阵,在单行中输出 -1。

如果存在,在第一行输出所有元素的最小可能总和。

然后在接下来的 $N$ 行中输出矩阵 $A$,元素之间用空格分隔。即第 $(i+1)$ 行应包含矩阵 $A$ 的第 $i$ 行元素,用空格分隔。

如果有多个解,可以输出其中任何一个。

样例

输入 1

2

输出 1

4
1 1
1 1

输入 2

1

输出 2

-1

说明

对于第一个样例,每一行、每一列和两条对角线的异或和均为 0,因此它满足好矩阵的条件。此外,在所有大小为 2 的好矩阵中,所有元素的总和不能小于 4,因此最小值为 4。

对于第二个样例,不存在大小为 1 的好矩阵,因此输出 -1。

非负整数 $x, y$ 的按位异或 $x \oplus y$ 定义如下:

  • 在 $x \oplus y$ 的二进制表示中,如果 $x$ 和 $y$ 的二进制表示中 $2^k$ ($k \ge 0$) 位上的数字恰好有一个为 1,则该位上的数字为 1;否则为 0。

例如,$3 \oplus 5 = 6$(二进制下,$011 \oplus 101 = 110$)。

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#1530EditorialOpen题解jiangly2026-04-15 16:05:05View

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.