Codeforces1917E - Construct Matrix
1.CW初中-C0102B(加强版)(CF1720D2-Trie树)2.CW初中-C0106D3.Luogu-P4654-[CEOI2017] Mousetrap4.2023.08.07模拟赛题解5.2023.08.21 模拟赛B题6.CW高中-C0296D7.Codeforces Edu 154 (A-E)8.Codeforces Pinely Round 2 (D~G)9.Atcoder-ABC317G-Rearranging10.Atcoder-ARC165F-Make Adjacent11.Codeforces1870F-Lazy Numbers12.CSP2023 题解13.CW高中-C0389B14.[PA2021] Wystawa15.CW高中-C0443D
16.Codeforces1917E - Construct Matrix
17.Codeforces1917F - Construct Tree18.Atcoder Xmas Contest 2022 H - Happy Game19.CW高中-C0452B20.CW高中-C0486A21.QOJ #1280.Fibonacci Partition/Fibonacci性质大杂烩22.图论杂题23.QOJ #1285.Stirling NumberCodeforces1917E - Construct Matrix
首先考虑因为 \(n\) 为偶数,所以 \(k\) 为奇数时不可能满足条件。
其次,如果 \(4|k\),那么实际上在矩阵中一直放 \(2\times 2\) 的全为 \(1\) 的矩阵就可以了。
随后,如果 \(k \equiv 2 \mod 4\),那么可以证明如果 \(k \ne 2 \land k \ne n^2-2\) ,那么必然有解。
我们在右下角预留一个 \(4\times 4\) 的矩阵填,先填其他位置。
然后会发现这个 \(4\times 4\) 的矩阵中,除了 \(k' = 2\) 和 \(k' = 14\) 的情况没法填,其他都有解。
又因为 \(k \ne 2 \land k \ne n^2-2\),所以对于 \(k'=2\) 在外面少填某个 \(2\times 2\) 的矩阵即可;对于 \(k'=14\) 在外面多填某个 \(2\times 2\) 的矩阵即可。
另外 \(k=2\) 或 \(k=n^2-2\) 的情况,有解当且仅当 \(n=2\)。
代码写得很丑因为是边想边写的。
#include <bits/stdc++.h>
using namespace std;
int n, k;
int mp[1005][1005], ans[20][5][5];
int tgr[5], tgc[5];
int main (){
int T;
scanf ("%d", &T);
for (int i = 0;i < (1 << 16);++ i){
int cnt = 0;
for (int j = 0;j < 4;++ j)
tgr[j] = tgc[j] = 0;
for (int j = 0;j < 16;++ j)
if (i >> j & 1){
++ cnt;
tgr[j / 4] ^= 1;
tgc[j % 4] ^= 1;
}
bool tag = true;
for (int j = 0;j < 4;++ j)
tag = tag && tgr[j] == 0 && tgc[j] == 0;
if (tag){
for (int x = 0;x < 4;++ x)
for (int y = 0;y < 4;++ y)
ans[cnt][x][y] = i >> ((x << 2) + y) & 1;
}
}
while (T --){
scanf ("%d%d", &n, &k);
for (int i = 1;i <= n;++ i)
for (int j = 1;j <= n;++ j)
mp[i][j] = 0;
if (k == 2 || k == n * n - 2){
if (n == 2)
puts ("Yes\n1 0\n0 1");
else
puts ("No");
} else {
if (k & 1)
puts ("No");
else {
if (k % 4 == 0){
puts ("Yes");
for (int i = 1;i <= n && k > 0;i += 2)
for (int j = 1;j <= n && k > 0;j += 2){
k -= 4;
mp[i][j] = mp[i + 1][j] = mp[i][j + 1] = mp[i + 1][j + 1] = 1;
}
for (int i = 1;i <= n;++ i, puts (""))
for (int j = 1;j <= n;++ j)
printf ("%d ", mp[i][j]);
} else {
if (k % 16 != 2 && k % 16 != 14){
int t = (k - 1) % 16 + 1;
k -= t;
for (int i = 1;i <= n && k > 0;i += 2)
for (int j = 1;j <= n && k > 0;j += 2)
if (i <= n - 4 || j <= n - 4){
k -= 4;
mp[i][j] = mp[i + 1][j] = mp[i][j + 1] = mp[i + 1][j + 1] = 1;
}
for (int i = 0;i < 4;++ i)
for (int j = 0;j < 4;++ j)
mp[i + n - 3][j + n - 3] = ans[t][i][j];
puts ("Yes");
for (int i = 1;i <= n;++ i, puts (""))
for (int j = 1;j <= n;++ j)
printf ("%d ", mp[i][j]);
} else
if (k % 16 == 2){
int t = 6;
k -= t;
for (int i = 1;i <= n && k > 0;i += 2)
for (int j = 1;j <= n && k > 0;j += 2)
if (i <= n - 4 || j <= n - 4){
k -= 4;
mp[i][j] = mp[i + 1][j] = mp[i][j + 1] = mp[i + 1][j + 1] = 1;
}
for (int i = 0;i < 4;++ i)
for (int j = 0;j < 4;++ j)
mp[i + n - 3][j + n - 3] = ans[t][i][j];
puts ("Yes");
for (int i = 1;i <= n;++ i, puts (""))
for (int j = 1;j <= n;++ j)
printf ("%d ", mp[i][j]);
} else
if (k % 16 == 14){
int t = 10;
k -= t;
for (int i = 1;i <= n && k > 0;i += 2)
for (int j = 1;j <= n && k > 0;j += 2)
if (i <= n - 4 || j <= n - 4){
k -= 4;
mp[i][j] = mp[i + 1][j] = mp[i][j + 1] = mp[i + 1][j + 1] = 1;
}
for (int i = 0;i < 4;++ i)
for (int j = 0;j < 4;++ j)
mp[i + n - 3][j + n - 3] = ans[t][i][j];
puts ("Yes");
for (int i = 1;i <= n;++ i, puts (""))
for (int j = 1;j <= n;++ j)
printf ("%d ", mp[i][j]);
}
}
}
}
}
return 0;
}
本文作者:saubguiu
本文链接:https://www.cnblogs.com/imcaigou/p/17925836.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步