Universal Cup Judging System

Universal Cup

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

一個大小為 $N$ 的「好矩陣」是指一個 $N \times N$ 的正整數矩陣,使得每一列、每一行以及兩條對角線的總 XOR 值皆為 0。

更精確地說,一個 $N \times N$ 的矩陣 $A$ 被稱為大小為 $N$ 的好矩陣,若它滿足以下所有條件。在此,$x \oplus y$ 表示 $x$ 與 $y$ 的位元 XOR 運算,且 $\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。 若存在,請在第一行輸出所有元素總和的最小值。 接著輸出矩陣 $A$,共 $N$ 行,每行元素以空白分隔。亦即,第 $(i+1)$ 行應包含矩陣 $A$ 第 $i$ 列的元素,並以空白分隔。 若有多種解,輸出其中任意一種即可。

範例

輸入格式 1

2

輸出格式 1

4
1 1
1 1

輸入格式 2

1

輸出格式 2

-1

說明

對於第一個範例,每一列、每一行以及兩條對角線的總 XOR 值皆為 0,因此它滿足好矩陣的條件。此外,在所有大小為 2 的好矩陣中,所有元素的總和無法小於 4,因此最小值為 4。

對於第二個範例,不存在大小為 1 的好矩陣,因此輸出 -1。

非負整數 $x, y$ 的位元 XOR $x \oplus y$ 定義如下:

  • 在 $x \oplus y$ 的二進位表示中,$2^k$ ($k \ge 0$) 位上的數字為 1,若且唯若 $x$ 與 $y$ 二進位表示中 $2^k$ 位上的數字恰有一個為 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.