题面
来源:USACO 官网。模型收到的是同一份题面的文字。
Bessie has an $N\times N$ ($1\le N\le 1000$) addition table where the integer in the cell at row $r$ and column $c$ is $r+c$, for all $1\le r,c\le N$. For example, for $N=3$, the table would look like this:
2 3 4 3 4 5 4 5 6
Unfortunately, Elsie got ahold of the table and permuted it by performing the following three types of operations as many times as she wanted.
- Swap two rows
- Swap two columns
- Select two values $a$ and $b$ that are both present in the table, then simultaneously change every occurrence of $a$ to $b$ and every occurrence of $b$ to $a$.
Elsie will always perform operations in increasing order of type; that is, she performs as many operations as she likes (possibly none) first of type $1$, then of type $2$, and finally of type $3$.
Help Bessie recover a possible state of the table after Elsie finished applying all of her operations of types $1$ and $2$, but before applying any operations of type $3$. There may be multiple possible answers, in which case you should output the lexicographically smallest one.
To compare two tables lexicographically, compare the first entries at which they differ, when reading both tables in the natural order (rows from top to bottom, left to right within a row).
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains $N$.The next $N$ lines each contain $N$ integers, representing Bessie's addition table after Elsie has permuted it.
OUTPUT FORMAT (print output to the terminal / stdout):
The lexicographically minimum possible state of the table after all operations of types 1 and 2, but before any operations of type 3. It is guaranteed that the answer exists.SAMPLE INPUT:
1 2
SAMPLE OUTPUT:
2
Regardless of what operations Elsie performs, the table won't change.
SAMPLE INPUT:
3 3 4 2 5 2 3 6 3 5
SAMPLE OUTPUT:
4 2 3 5 3 4 6 4 5
Here is a possible sequence of operations Elsie might have performed.
2 3 4 3 4 5 4 5 6 -> (op 1: swap columns 2 and 3) 2 4 3 3 5 4 4 6 5 -> (op 1: swap columns 1 and 2) 4 2 3 5 3 4 6 4 5 -> (op 3: swap values 2 and 3) 4 3 2 5 2 4 6 4 5 -> (op 3: swap values 3 and 4) 3 4 2 5 2 3 6 3 5
Note: the following is also a possible state of the table after operations of types 1 and 2, but it is not the lexicographically smallest because the second entry of the first row is larger than in the correct answer.
4 6 5 3 5 4 2 4 3
SAMPLE INPUT:
6 8 10 5 6 7 4 12 11 10 4 8 2 5 4 6 7 9 8 10 2 4 8 5 12 6 8 7 9 3 5 4 12 8 5 6 10
SAMPLE OUTPUT:
7 5 8 9 10 6 4 2 5 6 7 3 8 6 9 10 11 7 5 3 6 7 8 4 9 7 10 11 12 8 6 4 7 8 9 5
SCORING:
- Inputs 4-5: $N\le 6$
- Inputs 6-7: $N\le 8$
- Inputs 8-11: $N\le 100$
- Inputs 12-15: No additional constraints.
Problem credits: Benjamin Qi
结果
Kimi K3.1 ProKimi
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 8.4k平均输出 token(含思考) ↓
- 30.2输出速度 tokens/s ↑
- 279 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 5Pylint 问题 ↓
- 8.94Pylint 得分 ↑
- 18圈复杂度 ↓
- 62.5可维护性指数 ↑
DeepSeek Flash V4.1-1017DeepSeek
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 20.4k平均输出 token(含思考) ↓
- 197.4输出速度 tokens/s ↑
- 104 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 7Pylint 问题 ↓
- 9.10Pylint 得分 ↑
- 29圈复杂度 ↓
- 48.3可维护性指数 ↑
MiniMax M3.1 FlashMiniMax
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 33.2k平均输出 token(含思考) ↓
- 172.1输出速度 tokens/s ↑
- 194 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 5Pylint 问题 ↓
- 9.25Pylint 得分 ↑
- 24圈复杂度 ↓
- 54.5可维护性指数 ↑
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
import sys def main(): input = sys.stdin.buffer.readline n = int(input()) table = [] freq = [0] * (2 * n + 1) for _ in range(n): row = list(map(int, input().split())) table.append(row) for value in row: freq[value] += 1 # A value occurring once must represent either 2 or 2*N. anchor_row = anchor_col = 0 found = False for i in range(n): for j in range(n): if freq[table[i][j]] == 1: anchor_row, anchor_col = i, j found = True break if found: break # Treat the selected singleton as 2. Its row and column reveal the # original row and column labels through the value frequencies. row_labels = [freq[table[i][anchor_col]] for i in range(n)] col_labels = [freq[table[anchor_row][j]] for j in range(n)] del table # The only other possible table replaces every entry x by 2*N+2-x. # Determine which of the two is lexicographically smaller. middle = n + 1 complement = False decided = False for r in row_labels: for c in col_labels: value = r + c if value != middle: complement = value > middle decided = True break if decided: break output = [] if complement: total = 2 * n + 2 for r in row_labels: base = total - r output.append(" ".join(str(base - c) for c in col_labels)) else: for r in row_labels: output.append(" ".join(str(r + c) for c in col_labels)) sys.stdout.write("\n".join(output) + "\n") if __name__ == "__main__": main()
import sys def solve() -> None: data = list(map(int, sys.stdin.buffer.read().split())) if not data: return n = data[0] flat = data[1:1 + n * n] if n == 1: # The only value in the original 1x1 addition table is 2. print(2) return max_val = 2 * n freq = [0] * (max_val + 2) for v in flat: freq[v] += 1 # The two values occurring exactly once are at the two opposite corners # corresponding to sums 2 and 2N. singles = [] for idx, v in enumerate(flat): if freq[v] == 1: singles.append((idx // n, idx % n)) if len(singles) == 2: break best = None for r0, c0 in singles: # Treat this singleton cell as the (1, 1) corner of the sum table. R = [0] * n seen = [False] * (n + 1) valid = True for i in range(n): rv = freq[flat[i * n + c0]] if rv < 1 or rv > n or seen[rv]: valid = False break seen[rv] = True R[i] = rv if not valid: continue C = [0] * n seen = [False] * (n + 1) row0 = r0 * n for j in range(n): cv = freq[flat[row0 + j]] if cv < 1 or cv > n or seen[cv]: valid = False break seen[cv] = True C[j] = cv if not valid: continue # Verify that cells with equal R[i] + C[j] have equal final values. mapping = [-1] * (2 * n + 1) ok = True for i in range(n): base = i * n ri = R[i] for j in range(n): s = ri + C[j] t = flat[base + j] if mapping[s] == -1: mapping[s] = t elif mapping[s] != t: ok = False break if not ok: break if not ok: continue cand = [0] * (n * n) idx = 0 for i in range(n): ri = R[i] for c in C: cand[idx] = ri + c idx += 1 if best is None or cand < best: best = cand out_lines = [] for i in range(n): out_lines.append(" ".join(map(str, best[i * n:(i + 1) * n]))) sys.stdout.write("\n".join(out_lines)) if __name__ == "__main__": solve()
import sys def solve(): read = sys.stdin.buffer.readline first_line = read() if not first_line: return n = int(first_line) max_value = 2 * n # There are 2N-1 possible values: 2, 3, ..., 2N. byte_count = ((max_value - 1) + 7) // 8 # For each value, store its byte index and bit within that byte. value_info = [None] * (max_value + 1) for value in range(2, max_value + 1): position = value - 2 value_info[value] = (position >> 3, 1 << (position & 7)) column_storage = [bytearray(byte_count) for _ in range(n)] row_masks = [0] * n counts = [0] * (max_value + 1) first_row = [0] * (max_value + 1) first_column = [0] * (max_value + 1) for r in range(n): row_bytes = bytearray(byte_count) for c, value in enumerate(map(int, read().split())): byte_index, bit = value_info[value] row_bytes[byte_index] |= bit column_storage[c][byte_index] |= bit old_count = counts[value] if old_count == 0: first_row[value] = r first_column[value] = c counts[value] = old_count + 1 row_masks[r] = int.from_bytes(row_bytes, "little") column_masks = [ int.from_bytes(storage, "little") for storage in column_storage ] # The labels occurring once correspond to original values 2 and 2N. anchor_value = 0 for value in range(2, max_value + 1): if counts[value] == 1: anchor_value = value break anchor_row = first_row[anchor_value] anchor_column = first_column[anchor_value] if hasattr(int, "bit_count"): popcount = int.bit_count else: def popcount(x): return bin(x).count("1") # Orient the anchor row and column as offset 1. # Intersection size = N - distance from the endpoint. anchor_row_mask = row_masks[anchor_row] row_offset = [ n + 1 - popcount(mask & anchor_row_mask) for mask in row_masks ] anchor_column_mask = column_masks[anchor_column] column_offset = [ n + 1 - popcount(mask & anchor_column_mask) for mask in column_masks ] # Compare T and its global reflection 2N+2-T. # At the first unequal cell, the smaller entry determines the table. midpoint = n + 1 reflected = None for r in row_offset: for c in column_offset: total = r + c if total != midpoint: reflected = total > midpoint break if reflected is not None: break if reflected is None: # Only possible when N = 1. reflected = False if reflected: row_offset = [n + 1 - r for r in row_offset] column_offset = [n + 1 - c for c in column_offset] text = [str(value) for value in range(max_value + 1)] write = sys.stdout.write for r in row_offset: write(" ".join(text[r + c] for c in column_offset) + "\n") if __name__ == "__main__": solve()