跳到正文

题解

原神 R6 A

Register_int约 3 分钟
原文
文章信息与版本

文章信息

收录
2026年1月1日
更新
2026年1月20日
篇幅
约 3 分钟

版本

  1. v1当前

nn 开这么小何意味。

一个数不能出现大于 2n−12n-1 次,所以 k≤n/2k\le n/2 无解。注意到 nn 为奇数我们可以:

代码
22331
23311
33112
31122
11223

偶数我们可以:

代码
55555555
22334415
23344115
33441125
34411225
44112235
41122335
11223345

对于更大的 kk 将数逐个替换掉即可。时间复杂度 O(Tn2)\mathcal{O}(Tn^2)。

C++
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 30;

int T, n, m, a[MAXN][MAXN];

vector<pair<int, int>> pos[MAXN];

int main() {
	for (scanf("%d", &T); T--; ) {
		scanf("%d%d", &n, &m);
		if (m <= n / 2) puts("fire big");
		else {
			int x = n / 2 + 1;
			for (int i = 1; i <= x; i++) pos[i].clear();
			if (n & 1) {
				for (int i = 0; i < x; i++) {
					for (int j = 0; j < n; j++) {
						int tx = n - j, ty = (i * 2 + j) % (n + 1) + 1;
						if (ty <= n) a[tx][ty] = i + 1, pos[i + 1].emplace_back(tx, ty);
						ty = ty % (n + 1) + 1;
						if (ty <= n) a[tx][ty] = i + 1, pos[i + 1].emplace_back(tx, ty);
					}
				}
			} else {
				for (int i = 0; i < x - 1; i++) {
					for (int j = 0; j < n - 1; j++) {
						int tx = n - j, ty = (i * 2 + j) % n + 1;
						if (ty < n) a[tx][ty] = i + 1, pos[i + 1].emplace_back(tx, ty);
						ty = ty % n + 1;
						if (ty < n) a[tx][ty] = i + 1, pos[i + 1].emplace_back(tx, ty);
					}
				}
				for (int i = 1; i <= n; i++) a[1][i] = x, pos[x].emplace_back(1, i);
				for (int i = 2; i <= n; i++) a[i][n] = x, pos[x].emplace_back(i, n);
			}
			for (int j = x + 1, k = 1; j <= m; j++) {
				for (; pos[k].size() <= 1; k++);
				a[pos[k].back().first][pos[k].back().second] = j;
				pos[k].pop_back();
			}
			for (int i = 1; i <= n; i++) {
				for (int j = 1; j <= n; j++) printf("%d ", a[i][j]); puts("");
			}
		}
	}
}
原文