原神 R6 A

· · 题解

一个数不能出现大于 $2n-1$ 次,所以 $k\le n/2$ 无解。注意到 $n$ 为奇数我们可以: ``` 22331 23311 33112 31122 11223 ``` 偶数我们可以: ``` 55555555 22334415 23344115 33441125 34411225 44112235 41122335 11223345 ``` 对于更大的 $k$ 将数逐个替换掉即可。时间复杂度 $\mathcal{O}(Tn^2)$。 ```cpp #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(""); } } } } ```