题解:P17863 中秋 / plenilune

· · 题解

upd on 26.11.09:gpt 发现讨论漏了一个情况。

首先一个事情是方向存在某种对称性,由于 0 和 2 是对称的,3 和 7 是对称的,4 和 6 是对称的,所以我们的讨论量可以减半。

下面只呈现所有有解的情况(对称的会略去),一些不太平凡的情况用 * 标记出来了:

(0,2,4):

合法条件是 [n \equiv 1 \bmod 2],比较显然。

(0,2,5):

合法条件是 [(n-1)(m-1) \equiv 0 \bmod 3]。

对原图三染色,坐标 (i,j) 的颜色是 (i+j \bmod 3),每次行走颜色必然 +1,起点是 2,终点是 n+m \bmod 3,经过了 nm-1 步,那么需要满足 [nm+1 \equiv n+m \bmod 3]。然后有合法性构造,构造是平凡的,略去。

(0,3,5):

合法条件是 [m \equiv 2 \bmod 4],构造大概长这样:

*(0,3,6):

同样三染色,但是 (i,j) 的颜色为 (j-i \bmod 3),(1,1) 的颜色为 0,(n,m) 的颜色为 (m-n) \bmod 3,那么有 [0+nm-1 \equiv m-n \bmod 3],也就是 [(m+1)(n-1) \equiv 0 \bmod 3]。

其中 m 的一项显然有合法性构造,n 的那一项对于 m>4 均可行,有一个不太平凡的构造。大概长这样:

(0,1,2,4):

和 (0,2,4) 答案一样。

(0,1,2,5):

首先会继承 (0,2,5) 的答案 [(n-1)(m-1) \equiv 0 \bmod 3]。但是我们惊讶地发现,使用这四个方向向量远远强于原来的三个。具体来说,下面我们证明对于 n,m 不为 3 时,一定可以全部遍历网格。

首先我们发现,如果 n \times m 是合法的,那么 (n+3) \times m, n \times (m+3) 必定也是合法的,因为考虑如下结构:

这个结构可以缩减 3 \times n,对称一下可以缩减 n \times 3。也就是说,问题被缩减到了 (5,5),(5,6),(6,5),(6,6) 四种,中间两种显然是对称的,也就是说我们只需要对这三种情况手动构造即可。下面给出手动构造:

![](https://cdn.luogu.com.cn/upload/image_hosting/btpsidkm.png) $5 \times 6$: ![](https://cdn.luogu.com.cn/upload/image_hosting/js0naiux.png) $6 \times 6$: ![](https://cdn.luogu.com.cn/upload/image_hosting/l9njc9rv.png) ### $(0,1,3,5)$: 和 $(0,3,5)$ 等价。 ### *$(0,1,3,6)$: 首先继承来自 $(0,3,6)$ 的答案,$[(m+1)(n-1) \equiv 0 \bmod 3]$。但是我们发现其实比这个强得多。 问题规模难以缩减,但是我们发现,对于 $n,m$ 较大的情况,构造一定存在,具体来说有以下构造,以 $10 \times 10$ 举例子: ![](https://cdn.luogu.com.cn/upload/image_hosting/62qhajdw.png) 大概就是先沿着外面绕一圈,然后进入内部的一个小矩形,最后从右下方出去。但是这个还需要进行调整,调整是针对 $n$ 的,主要针对内部小矩阵的 $n$ 调整: + 从 $47 \to 64$ 可以看作一个 $3 \times m$ 的子结构,同理有一个平凡的 $4 \times m$ 的子结构,由裴蜀定理可知用 $3a+4b+1/2$ 可以表示所有 $n \ge 5$。需要单独讨论一下 $n=6$。 刚刚我们讨论完了 $n \ge 5 \wedge m\ge 4$ 的情况,现在讨论一下比较小的情况。 + $m=3$:比较平凡,有解当且仅当 $n$ 为奇数。 + $n=3$:首先对于 $3 \times 3$ 有解: ![](https://cdn.luogu.com.cn/upload/image_hosting/srkph4o8.png) 然后 $3 \times 4$ 无解,剩下的全部有解,大概思路是对 $m$ 讨论,如果 $m$ 模 $3$ 的余数为 $0$,那么用下图中间 $3 \times 3$ 的小矩阵循环。否则中间再加一个形如 $10 \to 16\to 14 \to 12 \to 18$ 进行 $1$ 的调整即可。 ![](https://cdn.luogu.com.cn/upload/image_hosting/7dsbekuu.png) + $n=4$:首先 $(4,4)$ 无解,对于 $n >4$ 的情况比较平凡,大概就是下图: ![](https://cdn.luogu.com.cn/upload/image_hosting/xa5h9zdl.png) 然后我们终于做完了这个情况! ### $(0,2,3,4)$: 和 $(0,2,4)$ 等价。 ### $(0,2,3,5)$: 首先继承 $(0,2,5)$ 的 $[(n-1)(m-1) \equiv 0 \bmod 3]$,然后考虑我们可以让 $m$ 的规模减少 $3$ 或者 $4$,那么可以全覆盖的 $m$ 可以表示为 $3a+4b+1$ 或者 $3a+4b+2$ 的形式,可以覆盖所有 $m$,那么所有都可行。 还需要判一些细节,对于 $(3,3)$ 无解,然后考虑 $m=3$,除了 $(3,3)$ 和 $(6,3)$ 都无解,构造和上一个情况的 $n=3$ 类似,大概长这样,总体来说是拿 $3 \times 3$ 循环,再做微调: ![](https://cdn.luogu.com.cn/upload/image_hosting/frhes24g.png) ### $(0,2,3,6)$: 和上面那个基本上同理,要弱智一点。 ### $(0,2,3,7)$: 一定可行,因为可以一个斜行一个斜行走。 ### $(0,2,4,5)$: 一定可行,和 $(0,2,3,6)$ 同理。 ### $(0,2,4,6)$: 网格图哈密顿路,当且仅当 $n,m$ 有一个奇数有解。 ### $(0,3,4,5)$: 和 $(0,3,5)$ 等价。 ### $(0,3,4,6)$: 首先如果 $m \ge 5$ 一定有解,因为大概长这样,$n$ 可以通过调整达到: ![](https://cdn.luogu.com.cn/upload/image_hosting/gq7euvuo.png) 如果 $m=4$ 可以从 $(0,3,6)$ 继承答案,如果 $m=3$ 一定无解。 ### *$(0,3,5,6)$: 首先 $m=3,4$ 无解,然后继承 $(0,3,5)$ 的 $[m \equiv 2 \bmod 4]$,和 $(0,3,6)$ 的 $[(m+1)(n-1) \equiv 0 \bmod 3]$。 首先对 $m$ 可以缩减规模,如果 $(n,m)$ 合法,那么 $(n,m+3)$ 必定合法。显然有一些 $n \times 3, n \times 4$ 的结构,合法的 $m$ 可以写为 $3a+4b+2$,那么除了 $7$ 以外的 $m$ 都是合法的。对 $m=7$ 单独讨论。 首先 $n=3k+1$ 对于 $m=7$ 肯定有解,对于 $3k+2$ 的构造大概长这样,$n=3k$ 时也有解,这里懒得写了,见代码: ![](https://cdn.luogu.com.cn/upload/image_hosting/7l7kt094.png) ### $(0,3,5,7)$: 和 $(0,3,5)$ 等价。 ### *$(0,3,6,7)$: 首先继承 $(0,3,6)$ 的 $[(m+1)(n-1) \equiv 0 \bmod 3]$。 很明显如果 $(n,m)$ 合法,那么 $(n,m+3)$ 合法。首先 $m \le 4$ 显然无解,讨论 $m \ge 5$ 的情况: $m=5$ 显然合法,对 $m=6$ 讨论,首先 $(3,6)$ 和 $(5,6)$ 无解,对于 $n \ge 6$,我们注意到有构造如下图,其中中间两列可以一直循环 $2 \times 2$,而且可以把 $18 \to 19$ 改为新占一行来调整奇偶性,所以对于 $n \ge 6$ 均有解: ![](https://cdn.luogu.com.cn/upload/image_hosting/hmu6eatb.png) 最后对 $m=7$ 讨论,再次根据 $n$ 模 $3$ 的值讨论: + $n$ 模 $3$ 为 $1$:这个 $(0,3,6)$ 已经讨论了。 + $n$ 模 $3$ 为 $2$:大概长这样: ![](https://cdn.luogu.com.cn/upload/image_hosting/ji3zk57g.png) + $n$ 模 $3$ 为 $0$:大概长这样: ![](https://cdn.luogu.com.cn/upload/image_hosting/ppqpiuq5.png) 会意即可吧,懒得写了,总之就是对于 $m \ge 5$,除了 $(3,6),(5,6)$ 都有解。 ### *$(1,3,4,6)$: 注:这一部分 nullpt2 的构造比我的简洁很多,具体见代码。 首先考虑 $n,m$ 都比较大的情况,我们断言此时一定有合法构造,具体来说,拿 $11 \times 11$ 举例子,如下图: ![](https://cdn.luogu.com.cn/upload/image_hosting/sygad0kx.png) 构造的基本思路,是左上角一块 $a \times 2$,左下一块 $b \times 3$($b$ 是奇数),右边同理,中间是若干 $3c+4d+2$ 拼合成的块。 首先考虑 $3c+4d+2$ 无法表示哪些数,$1,3,4,7$ 无法表示,$1,3,4$ 先不考虑,考虑 $n=7$ 怎么进行特殊构造,对 $m$ 对 $3$ 取模的值讨论即可,具体来说如下面三张图,懒得写了: ![](https://cdn.luogu.com.cn/upload/image_hosting/xb8aliza.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/xv4w6cpq.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/mvl4rpoj.png) 接下来考虑 $n \ne 7$,并且 $n$ 比较大的情况,两边会有一点细节的调整($2$ 列和 $3$ 列的交界处)。 关于这个细节调整,简直是灾难性的,笔者写完了才意识到这个问题,首先对 $n=9$ 有构造: ![](https://cdn.luogu.com.cn/upload/image_hosting/kzp3cuks.png) 然后对于 $n=5$ 有构造: ![](https://cdn.luogu.com.cn/upload/image_hosting/vy6lk1q1.png) 然后大的部分有两个 $3$ 结构和两个 $4$ 结构的调整都是平凡的,问题缩减到小的部分,对 $n=6,8,10$ 继续调整,可以用一种针对偶数的构造解决。 这个构造应该对 $n \ge 5,m \ge 6$ 是通用的,$n < 5$ 无解(存疑),也就是我们只需要对 $m$ 较小进行一些讨论: + $m=3$:这个比较简单,有解当且仅当 $n$ 是奇数并且 $n \ge 5$,构造和上面大情况的左侧类似,略去。 + $m=4$:构造如下图所示,先把左上角填满,下面的构造就是一直循环 ,当且仅当 $n$ 是偶数有解。 ![](https://cdn.luogu.com.cn/upload/image_hosting/s30to8yr.png) + $m=5$:按 $n$ 模 $4$ 讨论有点精神污染,事实上有一个非常好调整的构造: ![](https://cdn.luogu.com.cn/upload/image_hosting/kklo5ff8.png) ### 五个方向向量 首先大部分都可以直接枚举子集得到构造,少部分特例如下,注意有很多 $5\times 5, 5 \times 6, 6 \times 5, 6 \times 6$ 的特判下文未列出。 ### $(0,1,2,4,6)$: 由 $(0,2,4,6)$ 知道 $n,m$ 不全是偶数时有解。全是偶数有一个比较平凡的构造。 ### $(0,1,3,4,6)$: 从 $(0,1,3,6)$ 先继承大部分的答案,然后对于 $n \times 3$ 和 $3 \times 4$ 单独构造即可。 ### $(0,1,3,4,7)$: 这个的任意一个子集都无解,所以考虑手动讨论一下。 有解当且仅当 $n=3,4$ 且 $m$ 为偶数,构造如下: ![](https://cdn.luogu.com.cn/upload/image_hosting/1yfjq3m8.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/fmg870qv.png) ### $(0,1,3,5,7)$: 有解当且仅当 $m$ 是偶数,根据 $m$ 模 $4$ 的值进行讨论: + 当 $m$ 是 $4$ 的倍数: ![](https://cdn.luogu.com.cn/upload/image_hosting/q5n9zxql.png) + 当 $m \equiv 2 \bmod 4$: ![](https://cdn.luogu.com.cn/upload/image_hosting/ojrmed68.png) ### *$(1,3,4,5,7)$: 有解当且仅当 $m>n$ 并且 $n,m$ 都是偶数,我觉得我放一张图就能看懂了: ![](https://cdn.luogu.com.cn/upload/image_hosting/asa5w6i1.png) 大概解释一下,先在最左边嵌进去若干个 $n \times 2$ 调整到 $n \times (n+2)$,然后构造即可。 ### $(1,3,4,6,7)$: 首先从 $(1,3,4,6)$ 继承答案,那么现在没确定的是 $n \le 4$ 和 $m \le 4$,分开讨论: + $n=3$:当且仅当 $m$ 是奇数有解,构造如下: ![](https://cdn.luogu.com.cn/upload/image_hosting/nukj9orv.png) + $n=4$:对于 $m \ge 5$,全部有解。讨论 $m$ 的奇偶性即可: ![](https://cdn.luogu.com.cn/upload/image_hosting/co4z8b8h.png) ![](https://cdn.luogu.com.cn/upload/image_hosting/brp12c3s.png) 然后对于 $m=3/4$,这个操作方向集合本身是对称的,交换 $n,m$ 构造即可。 ### 方向向量超过五个 直接枚举子集即可。 ------------------------- 讲得太狗屎了,给一份 std,std 写得慢且臭长,写了 30K,nullpt2 只写了大概 10K 左右: ::::info[代码] ```cpp #include <bits/stdc++.h> #define Debug(fmt, args...) fprintf(stderr, "(func %s, line #%d): " fmt, __usr_func__, __LINE__, ##args), fflush(stderr) #define debug(fmt, args...) fprintf(stderr, fmt, ##args), fflush(stderr) #define ll long long #define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define pii pair<ll, ll> #define mp make_pair #define pb push_back #define eb emplace_back #define ld lower_bound #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define drep(i, a, b) for (int i = (a); i >= (b); i--) #define U(i, a, b) for (int i = (a); i < (b); i++) #define ud upper_bound #define mem(s, k) memset(s, k, sizeof(s)) #define fi first #define se second #define ull unsigned long long #define vi vector<ll> #define fv inline void #define fn inline static #define sz(x) (int)(x.size()) #define bp(x) __builtin_popcount(x) using u8 = unsigned char; using u16 = unsigned short; using u32 = unsigned; using u64 = unsigned ll; using i16 = short; using i32 = ll; using i64 = ll; using db = long double; using namespace std; const i32 mod = 998244353; template <typename T> fv chkmax(T &x, T y) { x = x < y ? y : x; } template <typename T> fv chkmin(T &x, T y) { x = x > y ? y : x; } fv inc(i64 &x, i64 y) { x = (x + y >= mod ? x + y - mod : x + y); } fv dec(i64 &x, i64 y) { x = (x - y < 0 ? x - y + mod : x - y); } fn i64 madd(i64 x, i64 y) { return (x + y >= mod ? x + y - mod : x + y); } fn i64 msub(i64 x, i64 y) { return madd(x, mod - y); } fn i32 qkpow(i32 a, i32 b) { i32 ans = 1; for (; b; b >>= 1) { if (b & 1) ans = ans * a % mod; a = a * a % mod; } return ans; } vi get(i32 S) { vi H; i32 cnt = 0; while (S) { if (S & 1) H.pb(cnt); cnt++, S >>= 1; } return H; } fn i32 query(vi S) { i32 sum = 0; for (i32 x : S) sum += (1ll << x); return sum; } vector <vi> cl; i32 to[8] = {2, 1, 0, 7, 6, 5, 4, 3}; fn bool check(i32 n, i32 m, i32 S) { vi H = get(S), HH; for (i32 x : H) HH.pb(to[x]); sort(HH.begin(), HH.end()); U (i, 0, (i32)H.size()) if (HH[i] < H[i]) { return check(m, n, query(HH)); } else if (HH[i] > H[i]) break; if (H.size() <= 2) return 0; else if (H.size() == 3) { if (H == (vi){0, 2, 4}) { if (n % 2 == 0) return 0; else return 1; } else if (H == (vi){0, 2, 5}) { if (n % 3 != 1 && m % 3 != 1) return 0; return 1; } else if (H == (vi){0, 2, 6}) { return check(m, n, 21); } else if (H == (vi){0, 3, 5}) { if (m % 4 != 2) return 0; return 1; } else if (H == (vi){0, 3, 6}) { if (m % 3 != 2 && n % 3 != 1) return 0; if (m % 3 == 2) return 1; if (n % 3 == 1) { if (m <= 4) return 0; return 1; } } else return 0; } else if (H.size() == 4) { if (H == (vi){0, 1, 2, 4}) { return check(n, m, query({0, 2, 4})); } else if (H == (vi){0, 1, 2, 5}) { if (n % 3 == 1 || m % 3 == 1) return check(n, m, query({0, 2, 5})); if (n == 3 || m == 3) return 0; return 1; } else if (H == (vi){0, 1, 3, 5}) { return check(n, m, query({0, 3, 5})); } else if (H == (vi){0, 1, 3, 6}) { if (m % 3 == 2 || n % 3 == 1) return check(n, m, query({0, 3, 6})); return 1; } else if (H == (vi){0, 2, 3, 4}) { return check(n, m, query({0, 2, 4})); } else if (H == (vi){0, 2, 3, 5}) return 1; else if (H == (vi){0, 2, 3, 6}) return 1; else if (H == (vi){0, 2, 3, 7}) return 1; else if (H == (vi){0, 2, 4, 5}) return 1; else if (H == (vi){0, 2, 4, 6}) { if (n % 2 == 0 && m % 2 == 0) return 0; if (n % 2 == 1) return check(n, m, query({0, 2, 4})); return check(n, m, query({0, 2, 6})); } else if (H == (vi){0, 3, 4, 5}) { return check(n, m, query({0, 3, 5})); } else if (H == (vi){0, 3, 4, 6}) return 1; else if (H == (vi){0, 3, 5, 6}) { if (m % 4 == 2) return check(n, m, query({0, 3, 5})); if (m % 3 == 2 || n % 3 == 1) return check(n, m, query({0, 3, 6})); return m != 7 || n % 3 != 0 || n >= 9; } else if (H == (vi){0, 3, 5, 7}) { return check(n, m, query({0, 3, 5})); } else if (H == (vi){0, 3, 6, 7}) { if (m % 3 == 2 || n % 3 == 1) return check(n, m, query({0, 3, 6})); if (n == 5 && m == 6) return 0; return 1; } else if (H == (vi){1, 3, 4, 6}) { if (m == 5) { if (n == 6) return 0; } else if (m == 6) { if (n == 6) return 0; } return 1; } } else { if (H == (vi){0, 1, 3, 5, 7}) { if (m % 2 == 0) return 1; return 0; } if (H == (vi){1, 3, 4, 5, 7}) { if (n % 2 != 0 || m % 2 != 0 || m <= n) return 0; return 1; } if (H == (vi){0, 1, 4, 6, 7} || H == (vi){1, 3, 4, 5, 6}) { if ((n == 5 && m == 6) || (n == 6 && m == 5)) return 1; if (n == 6 && m == 6) return 1; } if (H == (vi){0, 1, 2, 4, 6}) return 1; if (H == (vi){1, 3, 4, 6, 7}) { if (n == 6 && m == 6) return 1; } for (i32 T = (S - 1) & S; T; T = (T - 1) & S) { if (check(n, m, T)) return 1; } } return 0; } vector <vi> solve(i32 n, i32 m, i32 S) { vector <vi> id; id.resize(n + 5); rep (i, 1, n) id[i].resize(m + 5); vi H = get(S); i32 tot = 0; // for (i32 x : H) cerr << x << " "; // cerr << "\n"; vi HH; for (i32 x : H) HH.pb(to[x]); sort(HH.begin(), HH.end()); U (i, 0, (i32)H.size()) if (HH[i] < H[i]) { vector <vi> Id = solve(m, n, query(HH)); if (Id == cl) return cl; rep (i, 1, n) rep (j, 1, m) id[i][j] = Id[j][i]; return id; } else if (HH[i] > H[i]) break; if (H.size() <= 2) return cl; else if (H.size() == 3) { if (H == (vi){0, 2, 4}) { if (n % 2 == 0) return cl; else { for (i32 i = 1; i <= n; i += 2) { rep (j, 1, m) id[i][j] = ++tot; if (i < n) drep (j, m, 1) id[i + 1][j] = ++tot; } return id; } } else if (H == (vi){0, 2, 5}) { if (n % 3 != 1 && m % 3 != 1) return cl; if (n % 3 == 1) { for (i32 i = 1; i <= n; i += 3) { rep (j, 1, m) id[i][j] = ++tot; if (i < n) { drep (j, m, 1) id[i + 1][j] = ++tot, id[i + 2][j] = ++tot; } } return id; } else { vector <vi> Id = solve(m, n, S); rep (i, 1, n) rep (j, 1, m) id[i][j] = Id[j][i]; return id; } } else if (H == (vi){0, 2, 6}) { vector <vi> Id = solve(m, n, 21); rep (i, 1, n) rep (j, 1, m) id[i][j] = Id[j][i]; return id; } else if (H == (vi){0, 3, 5}) { if (m % 4 != 2) return cl; for (i32 j = 1; j <= m; j += 4) { rep (i, 1, n) id[i][j] = ++tot, id[i][j + 1] = ++tot; if (j + 2 <= m) { drep (i, n, 1) id[i][j + 2] = ++tot, id[i][j + 3] = ++tot; } } return id; } else if (H == (vi){0, 3, 6}) { if (m % 3 != 2 && n % 3 != 1) return cl; if (m % 3 == 2) { for (i32 j = 1; j <= m; j += 3) { rep (i, 1, n) id[i][j] = ++tot, id[i][j + 1] = ++tot; if (j + 1 <= m) { drep (i, n, 1) id[i][j + 2] = ++tot; } } return id; } if (n % 3 == 1) { if (m <= 4) return cl; rep (i, 1, n) id[i][1] = ++tot, id[i][2] = ++tot; for (i32 i = n; i >= 1; i -= 3) { rep (j, 3, m - 2) id[i][j] = ++tot; if (i > 1) { drep (j, m - 2, 3) id[i - 1][j] = ++tot, id[i - 2][j] = ++tot; } } rep (i, 1, n) id[i][m - 1] = ++tot, id[i][m] = ++tot; return id; } } else return cl; } else if (H.size() == 4) { if (H == (vi){0, 1, 2, 4}) { return solve(n, m, query({0, 2, 4})); } else if (H == (vi){0, 1, 2, 5}) { if (n % 3 == 1 || m % 3 == 1) return solve(n, m, query({0, 2, 5})); if (n == 3 || m == 3) return cl; i32 nn = 0, mm = 0; while (n - nn > 6) { rep (i, 1, m) id[nn + 1][i] = ++tot; drep (i, m, 1) id[nn + 2][i] = ++tot, id[nn + 3][i] = ++tot; nn += 3; } while (m - mm > 6) { rep (i, nn + 1, n) id[i][mm + 1] = ++tot; drep (i, n, nn + 1) id[i][mm + 2] = ++tot, id[i][mm + 3] = ++tot; mm += 3; } auto ins = [&](i32 a, i32 b) -> void { id[nn + a][mm + b] = ++tot; }; auto insr = [&](i32 a, i32 b) -> void { id[nn + b][mm + a] = ++tot; }; if (n - nn == 5 && m - mm == 5) { ins(1, 1), ins(2, 1), ins(3, 2), ins(4, 2), ins(3, 1); ins(4, 1), ins(5, 1), ins(5, 2), ins(5, 3), ins(5, 4); ins(4, 3), ins(4, 4), ins(3, 3), ins(2, 2), ins(2, 3); ins(1, 2), ins(1, 3), ins(1, 4), ins(1, 5), ins(2, 5); ins(3, 5), ins(2, 4), ins(3, 4), ins(4, 5), ins(5, 5); } else if (n - nn == 6 && m - mm == 6) { ins(1, 1), ins(2, 1), ins(3, 1), ins(3, 2), ins(4, 3), ins(5, 3); ins(4, 2), ins(5, 2), ins(4, 1), ins(5, 1), ins(6, 1), ins(6, 2); ins(6, 3), ins(6, 4), ins(6, 5), ins(5, 4), ins(5, 5), ins(4, 4); ins(3, 3), ins(2, 2), ins(2, 3), ins(1, 2), ins(1, 3), ins(1, 4); ins(1, 5), ins(1, 6), ins(2, 6), ins(3, 6), ins(2, 5), ins(3, 5); ins(2, 4), ins(3, 4), ins(4, 5), ins(4, 6), ins(5, 6), ins(6, 6); } else if (n - nn == 5 && m - mm == 6) { ins(1, 1), ins(2, 1), ins(3, 1), ins(4, 1), ins(5, 1); ins(5, 2), ins(5, 3), ins(4, 2), ins(4, 3), ins(3, 2); ins(3, 3), ins(2, 2), ins(2, 3), ins(1, 2), ins(1, 3); ins(2, 4), ins(2, 5), ins(1, 4), ins(1, 5), ins(1, 6); ins(2, 6), ins(3, 6), ins(4, 6), ins(3, 5), ins(4, 5); ins(3, 4), ins(4, 4), ins(5, 4), ins(5, 5), ins(5, 6); } else if (n - nn == 6 && m - mm == 5) { insr(1, 1), insr(2, 1), insr(3, 1), insr(4, 1), insr(5, 1); insr(5, 2), insr(5, 3), insr(4, 2), insr(4, 3), insr(3, 2); insr(3, 3), insr(2, 2), insr(2, 3), insr(1, 2), insr(1, 3); insr(2, 4), insr(2, 5), insr(1, 4), insr(1, 5), insr(1, 6); insr(2, 6), insr(3, 6), insr(4, 6), insr(3, 5), insr(4, 5); insr(3, 4), insr(4, 4), insr(5, 4), insr(5, 5), insr(5, 6); } return id; } else if (H == (vi){0, 1, 3, 5}) { return solve(n, m, query({0, 3, 5})); } else if (H == (vi){0, 1, 3, 6}) { if (m % 3 == 2 || n % 3 == 1) return solve(n, m, query({0, 3, 6})); rep (i, 1, m - 2) id[1][i] = ++tot; rep (i, 1, n - 5) id[i][m - 1] = ++tot, id[i][m] = ++tot; id[n - 4][m - 1] = ++tot, id[n - 3][m] = ++tot, id[n - 4][m] = ++tot, id[n - 3][m - 1] = ++tot; id[n - 2][m] = ++tot, id[n - 1][m - 1] = ++tot; drep (i, m - 2, 1) id[n][i] = ++tot, id[n - 1][i] = ++tot; i32 A = 0, B = 0, C = 0; for (i32 o : {1, 2}) { rep (a, 0, 5) { rep (b, 0, n) { if (a * 3 + b * 4 + o == n - 3) { A = a, B = b, C = o; } } } } if (n == 6) { vector <vi> Id; Id.resize(n + 5); if (m % 3 == 0) { Id[1] = {0, 1,2,3,4,5,6}; Id[2] = {0, 23,24,25,26,7,9}; Id[3] = {0, 22,29,27,32,10,8}; Id[4] = {0, 21,28,30,31,33,11}; Id[5] = {0, 20,18,16,14,12,34}; Id[6] = {0, 19,17,15,13,35,36}; rep (i, 1, 6) rep (j, 1, 6) id[i][j] = Id[i][j]; tot = 6 * 6; for (i32 j = 6; j < m; j += 3) { drep (i, n, 1) id[i][j + 1] = ++tot; rep (i, 1, n) id[i][j + 2] = ++tot, id[i][j + 3] = ++tot; } } else { Id[1] = {0, 1,2,3,4,5,6,7}; Id[2] = {0, 22,23,24,12,10,8,34}; Id[3] = {0, 21,25,13,11,9,35,33}; Id[4] = {0, 20,14,26,27,36,30,32}; Id[5] = {0, 19,17,15,37,28,29,31}; Id[6] = {0, 18,16,38,39,40,41,42}; rep (i, 1, 6) rep (j, 1, 7) id[i][j] = Id[i][j]; tot = 6 * 7; for (i32 j = 7; j < m; j += 3) { drep (i, n, 1) id[i][j + 1] = ++tot; rep (i, 1, n) id[i][j + 2] = ++tot, id[i][j + 3] = ++tot; } } return id; } i32 now = n - 1; rep (i, 1, A) { rep (i, 1, m - 4) id[now - 1][i] = ++tot; drep (i, m - 4, 1) id[now - 2][i] = ++tot, id[now - 3][i] = ++tot; now -= 3; } rep (i, 1, B) { rep (i, 1, m - 4) id[now - 1][i] = ++tot, id[now - 2][i] = ++tot; drep (i, m - 4, 1) id[now - 3][i] = ++tot, id[now - 4][i] = ++tot; now -= 4; } if (C == 1) { rep (i, 1, m - 4) id[now - 1][i] = ++tot; } else { rep (i, 1, m - 4) id[now - 1][i] = ++tot, id[now - 2][i] = ++tot; } rep (i, 2, n - 2) id[i][m - 3] = ++tot, id[i][m - 2] = ++tot; id[n - 2][m - 1] = ++tot, id[n - 1][m] = ++tot, id[n][m - 1] = ++tot, id[n][m] = ++tot; return id; } else if (H == (vi){0, 2, 3, 4}) { return solve(n, m, query({0, 2, 4})); } else if (H == (vi){0, 2, 3, 5}) { i32 A = 0, B = 0, C = 0; for (i32 o : {1, 2}) { rep (a, 0, 5) { rep (b, 0, m) { if (a * 3 + b * 4 + o == m) { A = a, B = b, C = o; } } } } i32 now = 0; rep (i, 1, A) { rep (i, 1, n) id[i][now + 1] = ++tot; drep (i, n, 1) id[i][now + 2] = ++tot, id[i][now + 3] = ++tot; now += 3; } rep (i, 1, B) { rep (i, 1, n) id[i][now + 1] = ++tot, id[i][now + 2] = ++tot; drep (i, n, 1) id[i][now + 3] = ++tot, id[i][now + 4] = ++tot; now += 4; } if (C == 1) { rep (i, 1, n) id[i][now + 1] = ++tot; } else { rep (i, 1, n) id[i][now + 1] = ++tot, id[i][now + 2] = ++tot; } return id; } else if (H == (vi){0, 2, 3, 6}) { i32 now = 0; if (!(m & 1)) { rep (i, 1, n) id[i][now + 1] = ++tot, id[i][now + 2] = ++tot; drep (i, n, 1) id[i][now + 3] = ++tot; now += 3; } while (now + 2 <= m) { rep (i, 1, n) id[i][now + 1] = ++tot; drep (i, n, 1) id[i][now + 2] = ++tot; now += 2; } rep (i, 1, n) id[i][now + 1] = ++tot; return id; } else if (H == (vi){0, 2, 3, 7}) { rep (sum, 2, n + m) { if (sum & 1) { drep (i, min<i32>(n, sum), 1) { i32 j = sum - i; if (j > m || j < 1) continue ; id[i][j] = ++tot; } } else { rep (i, 1, min<i32>(n, sum)) { i32 j = sum - i; if (j > m || j < 1) continue ; id[i][j] = ++tot; } } } return id; } else if (H == (vi){0, 2, 4, 5}) { i32 now = 0; if (!(n & 1)) { rep (i, 1, m) id[now + 1][i] = ++tot; drep (i, m, 1) id[now + 2][i] = ++tot, id[now + 3][i] = ++tot; now += 3; } while (now < n) { rep (i, 1, m) id[now + 1][i] = ++tot; if (now + 1 == n) break ; drep (i, m, 1) id[now + 2][i] = ++tot; now += 2; } return id; } else if (H == (vi){0, 2, 4, 6}) { if (n % 2 == 0 && m % 2 == 0) return cl; if (n % 2 == 1) return solve(n, m, query({0, 2, 4})); return solve(n, m, query({0, 2, 6})); } else if (H == (vi){0, 3, 4, 5}) { return solve(n, m, query({0, 3, 5})); } else if (H == (vi){0, 3, 4, 6}) { rep (i, 1, n) id[i][1] = ++tot, id[i][2] = ++tot; i32 now = n + 1; if (!(n & 1)) { rep (i, 3, m - 2) id[now - 1][i] = ++tot; drep (i, m - 2, 3) id[now - 2][i] = ++tot, id[now - 3][i] = ++tot; now -= 3; } while (now > 1) { rep (i, 3, m - 2) id[now - 1][i] = ++tot; if (now == 2) break; drep (i, m - 2, 3) id[now - 2][i] = ++tot; now -= 2; } rep (i, 1, n) id[i][m - 1] = ++tot, id[i][m] = ++tot; return id; } else if (H == (vi){0, 3, 5, 6}) { if (m % 4 == 2) return solve(n, m, query({0, 3, 5})); if (m % 3 == 2 || n % 3 == 1) return solve(n, m, query({0, 3, 6})); if (m != 7) { i32 A = 0, B = 0, C = 2; rep (a, 0, 5) { rep (b, 0, m) { if (a * 3 + b * 4 + C == m) { A = a, B = b; } } } i32 now = 0; rep (i, 1, A) { rep (i, 1, n) id[i][now + 1] = ++tot, id[i][now + 2] = ++tot; drep (i, n, 1) id[i][now + 3] = ++tot; now += 3; } rep (i, 1, B) { rep (i, 1, n) id[i][now + 1] = ++tot, id[i][now + 2] = ++tot; drep (i, n, 1) id[i][now + 3] = ++tot, id[i][now + 4] = ++tot; now += 4; } rep (i, 1, n) id[i][now + 1] = ++tot, id[i][now + 2] = ++tot; } else { if (n % 3 == 0 && n < 9) return cl; rep (i, 1, n) id[i][1] = ++tot, id[i][2] = ++tot; if (n % 3 == 0) { const i32 bot[4][3] = {{12, 7, 8}, {11, 9, 6}, {10, 4, 5}, {1, 2, 3}}; const i32 mid[3][3] = {{9, 7, 5}, {8, 6, 4}, {1, 2, 3}}; const i32 top[5][3] = {{13, 14, 15}, {12, 7, 8}, {11, 9, 6}, {10, 4, 5}, {1, 2, 3}}; auto put = [&](i32 x, const auto &a) { for (const auto &row : a) { U (j, 0, 3) id[x][j + 3] = tot + row[j]; ++x; } tot += 3 * (sizeof(a) / sizeof(a[0])); }; put(n - 3, bot); for (i32 now = n - 6; now >= 6; now -= 3) put(now, mid); put(1, top); } else { id[n][3] = ++tot, id[n][4] = ++tot, id[n][5] = ++tot; for (i32 now = n; now > 3; now -= 3) { id[now - 1][4] = ++tot, id[now - 1][5] = ++tot, id[now - 2][5] = ++tot; id[now - 3][5] = ++tot, id[now - 2][4] = ++tot, id[now - 1][3] = ++tot; id[now - 2][3] = ++tot, id[now - 3][3] = ++tot, id[now - 3][4] = ++tot; } id[1][3] = ++tot, id[1][4] = ++tot, id[1][5] = ++tot; } rep (i, 1, n) id[i][m - 1] = ++tot, id[i][m] = ++tot; } return id; } else if (H == (vi){0, 3, 5, 7}) { return solve(n, m, query({0, 3, 5})); } else if (H == (vi){0, 3, 6, 7}) { if (m % 3 == 2 || n % 3 == 1) return solve(n, m, query({0, 3, 6})); if (n == 5 && m == 6) return cl; if (n == 5 && m % 3 == 0) { i32 now = 0; while (now + 9 < m) { rep (i, 1, n) id[i][now + 1] = ++tot, id[i][now + 2] = ++tot; drep (i, n, 1) id[i][now + 3] = ++tot; now += 3; } auto ins = [&](i32 a, i32 b) -> void { id[a][now + b] = ++tot; }; rep (i, 1, 5) ins(i, 1), ins(i, 2); drep (i, 4, 1) ins(i, 3); rep (i, 1, 3) ins(i, 4), ins(i, 5); ins(4, 4), ins(5, 3), ins(5, 4), ins(4, 5), ins(4, 6), ins(5, 5), ins(5, 6); ins(5, 7), ins(4, 7), ins(3, 7), ins(2, 7), ins(3, 6), ins(2, 6), ins(1, 6), ins(1, 7); rep (i, 1, 5) ins(i, 8), ins(i, 9); return id; } i32 now = 0; while (now + 7 < m) { rep (i, 1, n) id[i][now + 1] = ++tot, id[i][now + 2] = ++tot; drep (i, n, 1) id[i][now + 3] = ++tot; now += 3; } if (m - now == 6) { rep (i, 1, n) id[i][now + 1] = ++tot, id[i][now + 2] = ++tot; id[n][now + 3] = ++tot, id[n][now + 4] = ++tot; for (i32 i = n; i > 5; i -= 2) { id[i - 1][now + 4] = ++tot, id[i - 2][now + 4] = ++tot; id[i - 1][now + 3] = ++tot, id[i - 2][now + 3] = ++tot; } if (n & 1) id[4][now + 3] = ++tot, id[4][now + 4] = ++tot; id[3][now + 4] = ++tot, id[2][now + 4] = ++tot, id[3][now + 3] = ++tot, id[2][now + 3] = ++tot; id[1][now + 3] = ++tot, id[1][now + 4] = ++tot; rep (i, 1, n) id[i][now + 5] = ++tot, id[i][now + 6] = ++tot; } else if (m - now == 7) { rep (i, 1, n) id[i][now + 1] = ++tot, id[i][now + 2] = ++tot; i32 Now = 0; id[n - 1][now + 3] = ++tot, id[n - 1][now + 4] = ++tot, id[n][now + 3] = ++tot; id[n][now + 4] = ++tot, id[n][now + 5] = ++tot, id[n - 1][now + 5] = ++tot; drep (i, 5, 3) id[n - 2][now + i] = ++tot, id[n - 3][now + i] = ++tot; Now = n - 3; while (Now - 3 > 1) { rep (i, 3, 5) id[Now - 1][now + i] = ++tot; drep (i, 5, 3) id[Now - 2][now + i] = ++tot, id[Now - 3][now + i] = ++tot; Now -= 3; } if (Now == 3) { id[2][now + 3] = ++tot, id[1][now + 3] = ++tot, id[1][now + 4] = ++tot; id[1][now + 5] = ++tot, id[2][now + 4] = ++tot, id[2][now + 5] = ++tot; } else if (Now == 2) { rep (i, 3, 5) id[1][now + i] = ++tot; } else assert(0); rep (i, 1, n) id[i][now + 6] = ++tot, id[i][now + 7] = ++tot; } return id; } else if (H == (vi){1, 3, 4, 6}) { if (n == 5) { id[1][1] = ++tot; rep (i, 2, m - 3) id[2][i] = ++tot, id[1][i] = ++tot; id[2][m - 2] = ++tot, id[3][m - 1] = ++tot; drep (i, 4, 1) id[i][m] = ++tot; id[1][m - 1] = ++tot, id[1][m - 2] = ++tot, id[2][m - 1] = ++tot; drep (i, m - 2, 3) id[3][i] = ++tot; rep (i, 1, n) rep (j, 1, m) if (!id[i][j]) id[i][j] = n * m + 1 - id[n - i + 1][m - j + 1]; return id; } else if (m == 5) { if (n == 6) return cl; if (n == 7) { id[1] = {0, 1, 18, 17, 15, 13}; id[2] = {0, 19, 2, 16, 14, 12}; id[3] = {0, 21, 20, 3, 5, 11}; id[4] = {0, 30, 22, 6, 4, 10}; id[5] = {0, 29, 31, 23, 7, 9}; id[6] = {0, 28, 24, 32, 34, 8}; id[7] = {0, 27, 26, 25, 33, 35}; return id; } id[1][1] = ++tot; rep (i, 2, n - 7) id[i][2] = ++tot, id[i][1] = ++tot; id[n - 6][2] = ++tot, id[n - 5][1] = ++tot, id[n - 6][1] = ++tot, id[n - 5][2] = ++tot; id[n - 4][1] = ++tot, id[n - 3][2] = ++tot, id[n - 4][2] = ++tot, id[n - 3][3] = ++tot; id[n - 2][4] = ++tot; drep (i, n - 1, 1) id[i][5] = ++tot; drep (i, 4, 2) id[1][i] = ++tot; id[2][3] = ++tot, id[3][4] = ++tot, id[2][4] = ++tot, id[3][3] = ++tot; rep (i, 4, n - 4) id[i][4] = ++tot, id[i][3] = ++tot; id[n - 3][4] = ++tot, id[n - 2][3] = ++tot, id[n - 1][2] = ++tot, id[n][3] = ++tot; id[n][2] = ++tot, id[n][1] = ++tot, id[n - 1][1] = ++tot, id[n - 2][1] = ++tot, id[n - 3][1] = ++tot; rep (i, 1, 3) id[n - 3 + i][1 + i] = ++tot; id[n - 1][m - 1] = ++tot, id[n][m] = ++tot; } else if (m == 6) { if (n == 6) return cl; id[1][1] = ++tot, id[2][2] = ++tot, id[1][2] = ++tot, id[2][1] = ++tot; rep (i, 3, n - 4) id[i][2] = ++tot, id[i][1] = ++tot; id[n - 3][2] = ++tot, id[n - 2][3] = ++tot, id[n - 1][2] = ++tot, id[n][3] = ++tot; id[n][2] = ++tot, id[n][1] = ++tot, id[n - 1][1] = ++tot, id[n - 2][1] = ++tot, id[n - 3][1] = ++tot; id[n - 2][2] = ++tot, id[n - 1][3] = ++tot; if (n & 1) { id[n][4] = ++tot, id[n - 1][4] = ++tot, id[n - 2][4] = ++tot, id[n - 3][4] = ++tot; drep (i, n - 3, 1) id[i][3] = ++tot; rep (i, 2, n - 4) id[i][4 + (i % 2 == 1)] = ++tot; drep (i, n - 3, 1) id[i][6] = ++tot; id[1][5] = ++tot; rep (i, 1, n - 3) id[i][4 + (i % 2 == 0)] = ++tot; rep (i, 1, n) rep (j, 1, m) if (!id[i][j]) id[i][j] = n * m + 1 - id[n - i + 1][m - j + 1]; return id; } else { id[n][4] = ++tot, id[n - 1][4] = ++tot, id[n - 2][4] = ++tot, id[n - 3][4] = ++tot, id[n - 4][4] = ++tot; drep (i, n - 3, 1) id[i][3] = ++tot; rep (i, 2, n - 5) id[i][4 + (i % 2 == 1)] = ++tot; drep (i, n - 4, 1) id[i][6] = ++tot; id[1][5] = ++tot; rep (i, 1, n - 4) id[i][4 + (i % 2 == 0)] = ++tot; rep (i, 1, n) rep (j, 1, m) if (!id[i][j]) id[i][j] = n * m + 1 - id[n - i + 1][m - j + 1]; return id; } } else if (n == 7) { if (m == 8) { id[1] = {0, 1, 3, 5, 7, 9, 21, 20, 19}; id[2] = {0, 47, 2, 4, 6, 8, 10, 22, 18}; id[3] = {0, 46, 48, 30, 29, 28, 23, 11, 17}; id[4] = {0, 45, 44, 49, 31, 27, 12, 24, 16}; id[5] = {0, 41, 43, 32, 50, 26, 25, 13, 15}; id[6] = {0, 40, 42, 37, 33, 51, 53, 55, 14}; id[7] = {0, 39, 38, 36, 35, 34, 52, 54, 56}; return id; } id[1][1] = ++tot, id[2][2] = ++tot, id[1][2] = ++tot, id[2][1] = ++tot; id[3][2] = ++tot, id[3][1] = ++tot, id[4][2] = ++tot, id[5][3] = ++tot; id[6][2] = ++tot, id[7][3] = ++tot, id[7][2] = ++tot, id[7][1] = ++tot; id[6][1] = ++tot, id[5][1] = ++tot, id[4][1] = ++tot, id[5][2] = ++tot; id[6][3] = ++tot; rep (i, 4, m - 2) id[7][i] = ++tot, id[6][i] = ++tot; id[5][m - 2] = ++tot; if (m % 3 == 0) { id[4][m - 2] = ++tot; i32 now = m - 2; while (now > 4) { id[4][now - 1] = ++tot, id[3][now - 1] = ++tot, id[3][now - 2] = ++tot; id[3][now - 3] = ++tot, id[4][now - 2] = ++tot, id[5][now - 1] = ++tot; id[5][now - 2] = ++tot, id[5][now - 3] = ++tot, id[4][now - 3] = ++tot; now -= 3; } id[4][3] = ++tot, id[3][3] = ++tot; rep (i, 1, n) { rep (j, 1, m) if (!id[i][j]) id[i][j] = n * m + 1 - id[n - i + 1][m - j + 1]; } } else { if (m % 3 == 1) { i32 now = m - 2; while (now > 5) { drep (i, 5, 3) id[i][now - 1] = ++tot; rep (i, 3, 5) id[i][now - 2] = ++tot, id[i][now - 3] = ++tot; now -= 3; } drep (i, 5, 3) id[i][now - 1] = ++tot; } else { i32 now = m - 2; while (now > 9) { drep (i, 5, 3) id[i][now - 1] = ++tot; rep (i, 3, 5) id[i][now - 2] = ++tot, id[i][now - 3] = ++tot; now -= 3; } drep (i, 5, 3) id[i][now - 1] = ++tot; id[3][7] = ++tot, id[3][6] = ++tot, id[3][5] = ++tot, id[4][6] = ++tot, id[5][7] = ++tot; id[4][7] = ++tot, id[5][6] = ++tot, id[5][5] = ++tot, id[4][5] = ++tot; drep (i, 5, 3) id[i][4] = ++tot; } drep (i, 4, 1) id[i][3] = ++tot; rep (i, 4, m - 4) id[2][i] = ++tot, id[1][i] = ++tot; id[2][m - 3] = ++tot, id[3][m - 2] = ++tot, id[4][m - 1] = ++tot, id[5][m] = ++tot; drep (i, 4, 1) id[i][m] = ++tot; id[2][m - 1] = ++tot, id[1][m - 1] = ++tot, id[1][m - 2] = ++tot, id[1][m - 3] = ++tot; id[2][m - 2] = ++tot, id[3][m - 1] = ++tot, id[4][m - 2] = ++tot, id[5][m - 1] = ++tot; id[6][m] = ++tot, id[7][m - 1] = ++tot, id[6][m - 1] = ++tot, id[7][m] = ++tot; } } else if (n == 9) { id[1][1] = ++tot, id[2][2] = ++tot, id[1][2] = ++tot, id[2][1] = ++tot; rep (i, 3, n - 4) id[i][2] = ++tot, id[i][1] = ++tot; id[n - 3][2] = ++tot, id[n - 2][3] = ++tot, id[n - 1][2] = ++tot, id[n][3] = ++tot; id[n][2] = ++tot, id[n][1] = ++tot, id[n - 1][1] = ++tot, id[n - 2][1] = ++tot, id[n - 3][1] = ++tot; id[n - 2][2] = ++tot, id[n - 1][3] = ++tot; rep (i, 4, m - 2) id[n][i] = ++tot, id[n - 1][i] = ++tot; drep (i, m - 2, 4) id[n - 2][i] = ++tot; id[6][4] = ++tot, id[6][3] = ++tot, id[5][3] = ++tot, id[4][3] = ++tot, id[5][4] = ++tot; rep (i, 5, m - 2) id[6][i] = ++tot, id[5][i] = ++tot; id[4][m - 2] = ++tot; drep (i, m - 3, 4) id[4][i] = ++tot, id[3][i] = ++tot; id[3][3] = ++tot; rep (i, 3, m - 3) id[2][i] = ++tot, id[1][i] = ++tot; rep (i, 1, n) rep (j, 1, m) if (!id[i][j]) id[i][j] = n * m + 1 - id[n - i + 1][m - j + 1]; return id; } else { i32 A = 0, B = 0, C = 2; rep (a, 0, 5) { rep (b, 0, n) { if (a * 3 + b * 4 + C == n) { A = a, B = b; break ; } } if (A || B) break ; } if (n % 2 == 0) { id[1][1] = ++tot, id[2][2] = ++tot, id[3][3] = ++tot; id[2][3] = ++tot, id[1][3] = ++tot, id[1][2] = ++tot; id[2][1] = ++tot, id[3][2] = ++tot; rep (i, 4, n) id[i][2 + (i % 2 == 0)] = ++tot; id[n][2] = ++tot; drep (i, n, 3) id[i][1] = ++tot; rep (i, 4, n - 1) id[i][2 + (i & 1)] = ++tot; i32 now = n + 1; rep (i, 1, A) { rep (i, 4, m - 3) id[now - 1][i] = ++tot, id[now - 2][i] = ++tot; drep (i, m - 3, 4) id[now - 3][i] = ++tot; now -= 3; } rep (i, 1, B) { rep (i, 4, m - 3) id[now - 1][i] = ++tot, id[now - 2][i] = ++tot; drep (i, m - 3, 4) id[now - 3][i] = ++tot, id[now - 4][i] = ++tot; now -= 4; } rep (i, 4, m - 3) id[2][i] = ++tot, id[1][i] = ++tot; rep (i, 1, n) { rep (j, 1, m) if (!id[i][j]) id[i][j] = n * m + 1 - id[n - i + 1][m - j + 1]; } return id; } // cerr << A << " - " << B << "\n"; if (B >= 2) { id[1][1] = ++tot, id[2][2] = ++tot, id[1][2] = ++tot, id[2][1] = ++tot; rep (i, 3, n - 4) id[i][2] = ++tot, id[i][1] = ++tot; id[n - 3][2] = ++tot, id[n - 2][3] = ++tot, id[n - 1][2] = ++tot, id[n][3] = ++tot; id[n][2] = ++tot, id[n][1] = ++tot, id[n - 1][1] = ++tot, id[n - 2][1] = ++tot, id[n - 3][1] = ++tot; id[n - 2][2] = ++tot, id[n - 1][3] = ++tot; rep (i, 4, m - 2) id[n][i] = ++tot, id[n - 1][i] = ++tot; drep (i, m - 2, 4) id[n - 2][i] = ++tot, id[n - 3][i] = ++tot; id[n - 3][3] = ++tot; i32 now = n - 3; rep (i, 1, A) { rep (i, 3, m - 2) id[now - 1][i] = ++tot, id[now - 2][i] = ++tot; drep (i, m - 2, 3) id[now - 3][i] = ++tot; now -= 3; } rep (i, 1, B - 2) { rep (i, 3, m - 2) id[now - 1][i] = ++tot, id[now - 2][i] = ++tot; drep (i, m - 2, 3) id[now - 3][i] = ++tot, id[now - 4][i] = ++tot; now -= 4; } rep (i, 3, m - 2) id[now - 1][i] = ++tot, id[now - 2][i] = ++tot; id[4][m - 2] = ++tot; drep (i, m - 3, 3) id[4][i] = ++tot, id[3][i] = ++tot; rep (i, 3, m - 3) id[2][i] = ++tot, id[1][i] = ++tot; id[2][m - 2] = ++tot, id[3][m - 1] = ++tot, id[4][m] = ++tot; id[3][m] = ++tot, id[2][m] = ++tot, id[1][m] = ++tot, id[1][m - 1] = ++tot, id[1][m - 2] = ++tot; id[2][m - 1] = ++tot, id[3][m - 2] = ++tot, id[4][m - 1] = ++tot; rep (i, 5, n - 2) id[i][m] = ++tot, id[i][m - 1] = ++tot; id[n - 1][m] = ++tot, id[n][m - 1] = ++tot, id[n - 1][m - 1] = ++tot, id[n][m] = ++tot; } else { assert(A >= 2); i32 now; if (B > 0) { id[1][1] = ++tot, id[2][2] = ++tot, id[1][2] = ++tot, id[2][1] = ++tot; rep (i, 3, n - 4) id[i][2] = ++tot, id[i][1] = ++tot; id[n - 3][2] = ++tot, id[n - 2][3] = ++tot, id[n - 1][2] = ++tot, id[n][3] = ++tot; id[n][2] = ++tot, id[n][1] = ++tot, id[n - 1][1] = ++tot, id[n - 2][1] = ++tot, id[n - 3][1] = ++tot; id[n - 2][2] = ++tot, id[n - 1][3] = ++tot; rep (i, 4, m - 2) id[n][i] = ++tot, id[n - 1][i] = ++tot; drep (i, m - 2, 4) id[n - 2][i] = ++tot, id[n - 3][i] = ++tot; id[n - 3][3] = ++tot; now = n - 3; rep (i, 1, A - 2) { rep (i, 3, m - 2) id[now - 1][i] = ++tot, id[now - 2][i] = ++tot; drep (i, m - 2, 3) id[now - 3][i] = ++tot; now -= 3; } rep (i, 1, B - 1) { rep (i, 3, m - 2) id[now - 1][i] = ++tot, id[now - 2][i] = ++tot; drep (i, m - 2, 3) id[now - 3][i] = ++tot, id[now - 4][i] = ++tot; now -= 4; } rep (i, 3, m - 2) id[now - 1][i] = ++tot, id[now - 2][i] = ++tot; } else { assert(n == 11); id[1][1] = ++tot, id[2][2] = ++tot, id[1][2] = ++tot, id[2][1] = ++tot; rep (i, 3, 5) id[i][2] = ++tot, id[i][1] = ++tot; id[6][2] = ++tot, id[7][3] = ++tot, id[8][2] = ++tot, id[9][3] = ++tot, id[10][2] = ++tot; id[11][3] = ++tot, id[11][2] = ++tot, id[11][1] = ++tot; drep (i, 10, 6) id[i][1] = ++tot; id[7][2] = ++tot, id[8][3] = ++tot, id[9][2] = ++tot, id[10][3] = ++tot; rep (i, 4, m - 2) id[n][i] = ++tot, id[n - 1][i] = ++tot; drep (i, m - 2, 4) id[n - 2][i] = ++tot; rep (i, 4, m - 2) id[8][i] = ++tot, id[7][i] = ++tot; } id[6][m - 2] = ++tot; drep (i, m - 3, 3) id[6][i] = ++tot; rep (i, 3, m - 3) id[5][i] = ++tot, id[4][i] = ++tot; drep (i, m - 3, 3) id[3][i] = ++tot; rep (i, 3, m - 3) id[2][i] = ++tot, id[1][i] = ++tot; id[2][m - 2] = ++tot, id[3][m - 1] = ++tot, id[4][m - 2] = ++tot, id[5][m - 1] = ++tot; drep (i, 6, 1) id[i][m] = ++tot; id[1][m - 1] = ++tot, id[1][m - 2] = ++tot, id[2][m - 1] = ++tot; id[3][m - 2] = ++tot, id[4][m - 1] = ++tot, id[5][m - 2] = ++tot, id[6][m - 1] = ++tot; rep (i, 7, n - 2) id[i][m] = ++tot, id[i][m - 1] = ++tot; id[n - 1][m] = ++tot, id[n][m - 1] = ++tot, id[n - 1][m - 1] = ++tot, id[n][m] = ++tot; } } return id; } } else if (H.size() >= 5) { if (H == (vi){0, 1, 3, 5, 7}) { if (m % 4 == 2) return solve(n, m, query({0, 3, 5})); if (m % 4 == 0) { vector <vi> Id = solve(n, m - 2, query({0, 3, 5})); rep (i, 1, n) rep (j, 1, m - 2) id[i][j] = Id[i][j]; tot = n * (m - 2); if (n % 2 == 0) { drep (i, n - 1, 1) id[i][m - (i & 1)] = ++tot; rep (i, 1, n) id[i][m - (i % 2 == 0)] = ++tot; id[n][m] = ++tot; } else { drep (i, n, 1) id[i][m - (i & 1)] = ++tot; rep (i, 1, n) id[i][m - (i % 2 == 0)] = ++tot; } return id; } return cl; } if (H == (vi){1, 3, 4, 5, 7}) { if (n % 2 != 0 || m % 2 != 0 || m <= n) return cl; vector <pii> S; rep (i, 1, n / 2) S.pb({2 * i - 1, 1}), S.pb({2 * i, 2}); drep (i, n / 2, 1) S.pb({2 * i, 1}), S.pb({2 * i - 1, 2}); i32 E = 2, L = (m - n - 2) / 2; U (b, 1, m / 2) { i32 col = 2 * b + 1; if (E == n) { drep (x, n, 1) S.pb({x, col + ((n - x) % 2 == 0 ? 0 : 1)}); S.pb({1, col}), S.pb({2, col + 1}); rep (x, 3, n) S.pb({x, col + (x % 2 == 1 ? 0 : 1)}); } else { U (x, 0, E) S.pb({E - x, col + (x & 1)}); S.pb({1, col}), S.pb({2, col + 1}); rep (x, 3, n) S.pb({x, col + (x % 2 == 1 ? 0 : 1)}); S.pb({n, col}); drep (x, n - 1, E + 1) S.pb({x, col + (x % 2 == 1 ? 1 : 0)}); if (L > 0) L--; else E += 2; } } for (auto p : S) id[p.fi][p.se] = ++tot; return id; } if (H == (vi){0, 1, 4, 6, 7}) { if (n == 5 && m == 6) { id[1] = {0, 1, 2, 25, 24, 23, 22}; id[2] = {0, 15, 4, 3, 26, 19, 21}; id[3] = {0, 14, 16, 5, 18, 27, 20}; id[4] = {0, 13, 12, 17, 6, 29, 28}; id[5] = {0, 11, 10, 9, 8, 7, 30}; return id; } else if (m == 5 && n == 6) { id[1] = {0, 1, 23, 22, 21, 20}; id[2] = {0, 3, 2, 24, 17, 19}; id[3] = {0, 5, 4, 16, 25, 18}; id[4] = {0, 13, 6, 15, 27, 26}; id[5] = {0, 12, 14, 7, 29, 28}; id[6] = {0, 11, 10, 9, 8, 30}; return id; } else if (n == 6 && m == 6) { id[1] = {0, 1, 2, 29, 28, 27, 26}; id[2] = {0, 5, 4, 3, 30, 23, 25}; id[3] = {0, 7, 6, 20, 22, 31, 24}; id[4] = {0, 17, 8, 19, 21, 33, 32}; id[5] = {0, 16, 18, 9, 10, 35, 34}; id[6] = {0, 15, 14, 13, 12, 11, 36}; return id; } } if (H == (vi){1, 3, 4, 5, 6}) { if (n == 5 && m == 6) { id[1] = {0, 1, 3, 5, 13, 12, 11}; id[2] = {0, 23, 2, 4, 6, 14, 10}; id[3] = {0, 22, 24, 16, 15, 7, 9}; id[4] = {0, 21, 17, 25, 27, 29, 8}; id[5] = {0, 20, 19, 18, 26, 28, 30}; return id; } else if (m == 5 && n == 6) { id[1] = {0, 1, 3, 11, 10, 9}; id[2] = {0, 26, 2, 4, 12, 8}; id[3] = {0, 25, 27, 13, 5, 7}; id[4] = {0, 22, 24, 28, 14, 6}; id[5] = {0, 21, 23, 18, 29, 15}; id[6] = {0, 20, 19, 17, 16, 30}; return id; } else if (n == 6 && m == 6) { id[1] = {0, 1, 19, 18, 16, 14, 13}; id[2] = {0, 20, 2, 17, 15, 10, 12}; id[3] = {0, 29, 21, 3, 5, 9, 11}; id[4] = {0, 28, 30, 22, 4, 6, 8}; id[5] = {0, 27, 23, 31, 33, 35, 7}; id[6] = {0, 26, 25, 24, 32, 34, 36}; return id; } } if (H == (vi){1, 3, 4, 6, 7}) { if (n == 6 && m == 6) { id[1] = {0, 1, 17, 19, 28, 27, 26}; id[2] = {0, 16, 2, 18, 20, 29, 25}; id[3] = {0, 15, 14, 3, 30, 21, 24}; id[4] = {0, 11, 13, 31, 4, 23, 22}; id[5] = {0, 10, 12, 5, 32, 35, 34}; id[6] = {0, 9, 8, 7, 6, 33, 36}; return id; } } if (H == (vi){0, 1, 2, 4, 6}) { if (n % 2 == 1 || m % 2 == 1) return solve(n, m, query({0, 2, 4, 6})); id[1][1] = ++tot, id[1][2] = ++tot; rep (i, 2, n - 1) id[i][2] = ++tot, id[i][1] = ++tot; id[n][1] = ++tot, id[n][2] = ++tot; drep (i, n, 1) id[i][3] = ++tot; vector <vi> Id = solve(n, m - 3, query({0, 2, 4, 6})); rep (i, 1, n) { rep (j, 4, m) id[i][j] = Id[i][j - 3] + 3 * n; } return id; } for (i32 T = (S - 1) & S; T; T = (T - 1) & S) { if (check(n, m, T)) return solve(n, m, T); } } return cl; } i32 n, m, S; fv sol() { cin >> n >> m >> S; // cerr << n << " " << m << " " << S << "\n"; cout << check(n, m, S) << "\n"; return ; if (S != 256) { vector <vi> id = solve(n, m, S); if (id == cl) { return cout << "0\n", void(); } cout << "1\n"; rep (i, 1, n) { rep (j, 1, m) cout << id[i][j] << " "; cout << "\n"; } } else { U (c, 0, 256) cout << check(n, m, c); cout << "\n"; } } int main() { // freopen("6-6.in", "r", stdin); // freopen("6-6.out", "w", stdout); // i32 a, b; // cin >> a >> b; // string s = to_string(a) + "-" + to_string(b); // freopen((s + ".in").c_str(), "r", stdin); // freopen((s + ".out").c_str(), "w", stdout); i32 T, c; cin >> T >> c; while (T--) sol(); } ``` :::: ::::info[code by nullpt2] ```cpp #include<cassert> #include<vector>//{{{ #include<iostream> #include<algorithm> #define F for #define W while #define fi first #define se second #define fv inline void #define eb emplace_back #define fn inline static #ifdef _WIN32 #define gc _getchar_nolock #else #define gc getchar_unlocked #endif #define mset __builtin_memset #define mcpy __builtin_memcpy #define mmov __builtin_memmove #define U(i,a,b) F(int i=(a);i<(int)(b);++i) #define L(i,a,b) F(int i=(a);i<=(int)(b);++i) #define R(i,a,b) F(int i=(a);i>=(int)(b);--i) using u8=unsigned char;using u16=unsigned short;using u32=unsigned;using u64=unsigned long long;using u128=__uint128_t;using i8=char;using i16=short;using i64=long long;using i128=__int128_t;template<typename T=u32>fn T ur(){u32 c;W(!__builtin_isdigit(c=gc()));T r=c&15;W(__builtin_isdigit(c=gc()))r=(r<<3)+(r<<1)+(c&15);return r;}template<typename T=int>fn T sr(){u32 c;bool x=false;W(!__builtin_isdigit(c=gc()))x=c=='-';T r=c&15;W(__builtin_isdigit(c=gc()))r=(r<<3)+(r<<1)+(c&15);return x?-r:r;}using std::vector;using vi=vector<int>;using vu=vector<u32>;using str=std::string;using pi=std::pair<int,int>;using pu=std::pair<u32,u32>;template<typename T,typename... A>fv uhi(T&x,A&&...v){x=std::max(x,T{v...});} template<typename T,typename... A>fv ulo(T&x,A&&...v){x=std::min(x,T{v...});} #if 0 constexpr u32 mod=1e9+7;fv dec(u32&x){if(x>=mod)x-=mod;}fv inc(u32&x){if((int)x<0)x+=mod;}fn u32 qpw(u32 x,u32 e){u32 r=e&1?x:1;W(e>>=1){x=(u64)x*x%mod;if(e&1)r=(u64)r*x%mod;}return r;}fv add(u32&x,u64 v){x=(x+v)%mod;} #endif//}}} fv out(vector<vi> o) { if (o.empty()) { std::cout << "0\n"; return; } std::cout << "1\n"; F(auto x : o) { F(auto v : x) std::cout << v << ' '; std::cout << '\n'; } } fv slv();int main(){//freopen("midautumn.in","r",stdin),freopen("midautumn.out","w",stdout); int t=ur();ur();W(t--)slv();return 0;} bool mark[256]; int shf(int x){int y=0;U(i,0,8)if(x>>i&1)y^=1<<(i&1?i^(i&2?4:0):(i^2));return y;} __attribute__((constructor))void setmark() { F(int x:{73,90,21,37,41,75,23,29,39,43,45,53,57,77,85,89,105,141,201,169,186,171,211,122,218,87})mark[shf(x)]=1; } fn bool chk(int n, int m, int c) { if (c == 90) { return n != 6 || m > 6; } else if (c == 21) { return n & 1; } else if (c == 73) { return m % 3 == 2 || n % 3 == 1; } else if (c == 37) { return n%3 == 1 || m % 3 == 1; } else if (c == 41 || c == 43 || c == 57 || c == 169) { return (m&3)==2; } else if(c==75||c==39||c==45||c==53||c==77||c==89||c==141||c==211||c==122||c==218||c==87){ return 1; } else if (c == 23 || c == 29) { return chk(n,m,21); } else if (c==85){ return (n|m)&1; } else if (c == 105) { return m != 7 || n % 3; } else if (c == 201) { return n != 5 || m != 6; } else if (c == 171) { return !(m & 1); } else if (c == 186) { return !((n | m) & 1) && n < m; } else { return mark[c] && chk(m,n,shf(c)); } } vector<vi> wrk(int n,int m,int c) { vector<vi> ans(n,vi(m)); const int nm = n * m; int x,y,tot; auto nx = [&](int dx,int dy){x+=dx,y+=dy,ans[x][y]=++tot;}; auto rep = [&](vector<pi> ch, int t) { W(t--) { F(auto [dx,dy] : ch) nx(dx,dy);} }; auto pl=[&](vector<vi>v){U(i,0,v.size())U(j,0,v[i].size())ans[i][j]=v[i][j];}; if (c == 90) { if (n == 6 && m <= 6) return {}; if (n >= 6 && m >= 6 && n != 7) { if (m == 6) { pl({{1,3},{4,2}}),x=1,y=0,tot=4; rep({{1,1},{0,-1}},(n>>1)-2); ans[++x][++y]=++tot; } else { pl({{1,6,5},{7,2,4},{0,8,3}}),x=2,y=1,tot=8; } W(x<n-1) nx(1,x<n-2?(y==1?1:-1):1); nx(0,-1);if(y>1)nx(0,-1),nx(-1,0);nx(x<n-1,-1); W(!ans[x-1][y])nx(-1,0);nx(1,1); W(!ans[x+1][y^3])nx(1,(y^3)-y);nx(1,1); if(m==6)W(x>n-1>>1)nx(-1,0);else{ int lx = n; W(true) { W(y!=m-4||x>lx-1) if(x>=lx-1)nx(-1,0);else nx(1,1);lx-=2; if (!lx) break; if (lx-1==7||lx-1<7&&lx-1!=6&&lx-1!=5&&lx-1!=2){ W(y!=3||x!=lx-1) if(x>=lx-1)nx(-1,0);else nx(1,-1);lx-=2; } else { nx(-1,0),nx(-1,0);W(y!=3)nx(0,-1);--lx; } } } } else if (n == 5) { ans[x=0][y=0]=tot=1,rep({{1,1},{-1,0}},m-4); rep({{1,1},{1,1},{1,1},{-1,0},{-1,0},{-1,0},{0,-1},{0,-1},{1,1},{1,-1}},1);rep({{0,-1}},m-5); } else if (n == 7) { pl({{1,3},{4,2},{6,5},{15,7},{14,16,8},{13,9,17},{12,11,10}}),x=5,y=2,tot=17,rep({{1,1},{-1,0}},m-5); if (m % 3 == 0) { --x, ++y; W(y > 4) rep({{0,-1},{-1,0},{0,-1},{-1,0},{0,-1},{0,-1},{1,1},{1,1},{0,-1}},1);nx(0,-1),nx(-1,0); } else if (m % 3 == 1) { nx(-1,0),nx(-1,0),++x; W(y > 4) rep({{0,-1},{-1,0},{-1,0},{0,-1},{0,-1},{1,1},{0,-1},{1,1},{0,-1}},1); nx(0,-1),nx(-1,0); } else if (m == 8) { pl({{1,3,5,7,9,21,20,19},{47,2,4,6,8,10,22,18},{46,48,30,29,28,23,11,17},{45,44,49,31,27,12,24,16},{41,43,32,50,26,25,13,15},{40,42,37,33,51,53,55,14},{39,38,36,35,34,52,54,56}}); } else if (m == 5) { pl({{1,18,17,15,13},{19,2,16,14,12},{21,20,3,5,11},{30,22,6,4,10},{29,31,23,7,9},{28,24,32,34,8},{27,26,25,33,35}}); }else { nx(-1,0),nx(-1,0),++x; W(y > 8) rep({{0,-1},{-1,0},{-1,0},{0,-1},{0,-1},{1,1},{0,-1},{1,1},{0,-1}},1); rep({{0,-1},{-1,0},{-1,0},{0,-1},{0,-1},{0,-1},{1,1},{1,1},{-1,0},{1,-1},{0,-1},{-1,0},{1,-1},{-1,0}},1); } } else { assert(m==5&&n>7); ans[x=0][y=0]=tot=1,rep({{1,1},{0,-1}},n-8); rep({{1,1},{1,-1},{-1,0},{1,1},{1,-1},{1,1},{-1,0},{1,1},{1,1},{1,1}},1);W(x)nx(-1,0); rep({{0,-1},{0,-1},{0,-1},{1,1},{1,1},{-1,0},{1,-1}},1); rep({{1,1},{0,-1}},n-7); rep({{1,1},{1,-1},{1,-1},{1,1},{0,-1},{0,-1},{-1,0},{-1,0},{-1,0},{1,1},{1,1},{1,1},{-1,0},{1,1}},1); } U(i, 0, n) U(j, 0, m) if (!ans[i][j]) ans[i][j] = nm - ans[n-i-1][m-j-1] + 1; } else if (c == 73) { if(m%3!=2&&n%3!=1)return{};ans[x=0][y=0]=tot=1;if (m%3!=2){rep({{0,1},{1,-1}},n-1);if(m%3==1){rep({{0,1}},4);rep({{-1,0},{-1,0},{1,-1},{-1,0},{1,-1},{-1,0},{-1,0},{0,1},{0,1}},n/3),nx(0,1);}else{rep({{0,1}},3); rep({{-1,0},{-1,0},{1,-1},{-1,0},{-1,0},{0,1}},n/3),nx(0,1);}}W(true){rep({{0,1},{1,-1}},n-1),nx(0,1);if(y==m-1)break;nx(0,1),rep({{-1,0}},n-1),nx(0,1);} } else if (c == 21) { if(!(n&1))return{};ans[x=0][y=0]=tot=1;L(i,1,n){if(i&1)rep({{0,1}},m-1);else rep({{0,-1}},m-1);if(i!=n)nx(1,0);} } else if (c == 37) { if(n%3!=1&&m%3!=1)return{};ans[x=0][y=0]=tot=1; if(n%3==1){ rep({{0,1}},m-1);L(i,1,n/3)nx(1,0),rep({{1,0},{-1,-1}},m-1),nx(1,0),nx(1,0),rep({{0,1}},m-1); }else{ rep({{1,0}},n-1);L(i,1,m/3)nx(0,1),rep({{0,1},{-1,-1}},n-1),nx(0,1),nx(0,1),rep({{1,0}},n-1); } } else if (c == 41 || c == 43 || c == 57 || c == 169) { if((m&3)!=2)return{};return wrk(n,m,105); } else if (c == 75) { if (m%3==2||n%3==1)return wrk(n,m,73);ans[x=0][y=0]=tot=1;rep({{0,1},{1,-1}},n-1);const int d=m%3?3:2; if((n%3)==2){rep({{0,1}},d+1);}else{nx(0,1),nx(0,1);rep({{-1,0},{1,1}},d-1),nx(-1,0);}W(x){nx(-1,0),rep({{-1,0},{1,-1}},d-1),nx(-1,0),nx(-1,0);if(x!=1)rep({{0,1}},d-1);else rep({{-1,0},{1,1}},d-1),nx(-1,0);}W(y<m-1){nx(0,1),rep({{0,1},{1,-1}},n-1),nx(0,1);if(y<m-1)nx(0,1),rep({{-1,0}},n-1);} } else if (c == 23 || c == 29) { return wrk(n,m,21); } else if (c == 39) { if(n%3==1||m%3==1)return wrk(n,m,37);ans[x=0][y=0]=tot=1; W(m - y > 6) rep({{1,0}},n-1),nx(0,1),rep({{0,1},{-1,-1}},n-1),nx(0,1),nx(0,1); const int d = m - y; W(n - x > 6) rep({{0,1}},d-1),nx(1,0),rep({{1,0},{-1,-1}},d-1),nx(1,0),nx(1,0); if(n-x==5&&d==5){ rep({{0,1},{1,1},{0,1},{-1,-1},{0,1},{0,1},{1,0},{1,0},{1,0},{-1,-1},{1,0},{-1,-1},{-1,-1},{1,0},{-1,-1},{1,0},{1,0},{1,0},{0,1},{0,1},{-1,-1},{0,1},{1,1},{0,1}},1); }else if(n-x==6&&d==5){ rep({{1,0}},5),nx(0,1),rep({{0,1},{-1,-1}},5),rep({{0,1},{0,1},{0,1},{1,0},{1,0},{-1,-1},{1,0},{1,1},{1,0},{-1,-1},{1,0},{1,0},{0,1}},1); }else if(n-x==5&&d==6){ rep({{0,1}},5),nx(1,0),rep({{1,0},{-1,-1}},5),rep({{1,0},{1,0},{1,0},{0,1},{0,1},{-1,-1},{0,1},{1,1},{0,1},{-1,-1},{0,1},{0,1},{1,0}},1); }else{ rep({{0,1},{1,0},{1,0},{-1,-1},{1,0},{1,1},{1,0},{-1,-1},{1,0},{1,0},{0,1}},1); nx(0,1),rep({{0,1},{-1,-1}},5); rep({{0,1},{0,1},{0,1},{1,0},{1,0},{-1,-1},{1,0},{1,1},{1,0},{-1,-1},{1,0},{1,0},{0,1}},1); } } else if (c == 45) { ans[x=0][y=0]=tot=1; auto d1=[&](){rep({{1,0}},n-1);}; auto d2=[&](){rep({{0,1},{1,-1}},n-1),nx(0,1);}; auto u2=[&](){nx(0,1),rep({{0,1},{-1,-1}},n-1),nx(0,1),nx(0,1);}; if((m&3)==3){d1(),u2(),d1(),u2();}else if((m&3)==0){d1(),u2();}W(y<m-2)d2(),u2();y==m-1?d1():d2(); } else if (c == 53) { if (n & 1) return wrk(n,m,21);ans[x=0][y=0]=tot=1; auto r1=[&](){rep({{0,1}},m-1);}; auto l1=[&](){nx(1,0),rep({{0,-1}},m-1),nx(1,0);}; auto l2=[&](){nx(1,0),rep({{1,0},{-1,-1}},m-1),nx(1,0),nx(1,0);}; r1(),l2();L(i,4,n)i&1?l1():r1(); } else if(c==77){ if(m&1)return wrk(n,m,69);ans[x=0][y=0]=tot=1; auto d1=[&](){rep({{1,0}},n-1);}; auto u1=[&](){nx(0,1),rep({{-1,0}},n-1),nx(0,1);}; auto d2=[&](){rep({{0,1},{1,-1}},n-1),nx(0,1);}; d2(),u1();L(i,4,m)i&1?u1():d1(); } else if (c==85){ if (n&1)return wrk(n,m,21); if (m&1)return wrk(n,m,shf(21)); return{}; } else if (c == 89) { if(m%3==2||n%3==1)return wrk(n,m,73);ans[x=0][y=0]=tot=1; rep({{0,1},{1,-1}},n-1),rep({{0,1}},m-3); W(x) nx(-1,0),x==2?rep({{-1,0},{1,-1}},m-5),nx(-1,0):rep({{0,-1}},m-5),nx(-1,0),rep({{0,1}},m-5); nx(0,1),rep({{0,1},{1,-1}},n-1),nx(0,1); } else if (c == 105) { ans[x=0][y=0]=tot=1; auto u1=[&](){nx(0,1),rep({{-1,0}},n-1),nx(0,1);};auto u2=[&](){nx(0,1),rep({{0,1},{-1,-1}},n-1),nx(0,1),nx(0,1);}; auto d2=[&](){rep({{0,1},{1,-1}},n-1),nx(0,1);}; if((m&3)!=2)if((m&3)==1)d2(),u1();else if((m&3)==0)d2(),u1(),d2(),u1();else if(m>=11)d2(),u1(),d2(),u1(),d2(),u1();else if(!(n%3))return{}; else{ d2(),nx(0,1); if((n%3)==2) rep({{0,1},{0,1},{-1,-1},{0,1},{-1,0},{-1,0},{1,-1},{1,-1},{-1,0},{-1,0},{0,1},{-1,-1}},1); rep({{0,1},{0,1},{-1,0},{-1,0},{1,-1},{-1,0},{1,-1},{-1,0},{-1,0}},x/3),nx(0,1),nx(0,1),nx(0,1); } W(y<m-1)x?u2():d2(); } else if (c == 141) { ans[x=0][y=0]=tot=1; W(x!=n-1||y!=m-1){if(x&&y<m-1&&!ans[x-1][y+1])nx(-1,1);else if(y&&x<n-1&&!ans[x+1][y-1])nx(1,-1);else if(x==n-1)nx(0,1);else if(y==m-1)nx(1,0);else if(!x)nx(0,1);else assert(!y),nx(1,0);} } else if (c == 201) { if (n == 5 && m == 6) return {}; if (m % 3 == 2 || n % 3 == 1) return wrk(n, m, 73); ans[x=0][y=0]=tot=1; rep({{0,1},{1,-1}},n-1),nx(0,1); if (m % 3 == 0) { if (n == 5) { rep({{-1,1},{-1,0},{-1,0},{-1,0},{0,1},{0,1},{1,-1},{0,1},{1,-1},{0,1},{1,-1},{1,-1},{0,1},{-1,1},{0,1},{1,-1},{0,1},{0,1},{-1,0},{-1,0},{-1,0},{1,-1},{-1,0},{-1,0},{0,1}},1); } else { nx(0,1),nx(0,1),nx(-1,0),nx(-1,0);rep({{1,-1},{-1,0},{-1,1},{-1,0}},2-!(n%3)); rep({{1,-1},{-1,0},{-1,0},{0,1}},1); } rep({{-1,0},{-1,0},{1,-1},{-1,0},{-1,0},{0,1}},x/3);nx(0,1); } else { rep({{-1,1},{0,1},{1,-1},{0,1},{0,1},{-1,0}},1); rep({{-1,0},{-1,0},{1,-1},{-1,0},{1,-1},{-1,0},{-1,0},{0,1},{0,1}},x/3-!(n%3)); if(n%3==0)rep({{-1,0},{-1,0},{1,-1},{-1,0},{1,-1},{-1,0},{-1,0},{-1,0},{0,1},{0,1},{1,-1},{0,1},{-1,1}},1); else nx(0,1); } W(y != m-1) x?nx(0,1),rep({{-1,0}},n-1):rep({{0,1},{1,-1}},n-1),nx(0,1); } else if (c == 186) { if (!chk(n,m,c)) return{};ans[x=0][y=0]=tot=1; W(x<n-1){if(y)nx(n-x==m-y-1||!x?1:-1,1);if(y){W(x)nx(-1,y&1?-1:1);nx(0,-1);}W(x<n-1)nx(1,y&1?-1:1);if(y!=m-1){nx(0,-1);W(x&&!ans[x-1][y^1])nx(-1,y&1?-1:1);}} } else if (c == 171) { if (m & 1) return {}; if (m & 2) return wrk(n,m,41); ans[x=0][y=0]=tot=1; W(y < m - 3) y & 1 ? nx(0,1), rep({{0,1},{-1,-1}},n-1),nx(0,1) : rep({{0,1},{1,-1}}, n-1), nx(0,1); nx(-!(n&1),1),rep({{-1,1},{-1,-1}},(n-1)/2); nx(0,1); rep({{1,-1},{1,1}},(n-1)/2); if(!(n&1))nx(1,-1),nx(0,1); } else if(c==211&&n<=6&&m<=6&&std::max(n,m)==6){ if(n==5&&m==6){pl({{1,2,25,24,23,22},{15,4,3,26,19,21},{14,16,5,18,27,20},{13,12,17,6,29,28},{11,10,9,8,7,30}});}else if(m==5&&n==6){pl({{1,23,22,21,20},{3,2,24,17,19},{5,4,16,25,18},{13,6,15,27,26},{12,14,7,29,28},{11,10,9,8,30}});}else if(n==6&&m==6){pl({{1,2,29,28,27,26},{5,4,3,30,23,25},{7,6,20,22,31,24},{17,8,19,21,33,32},{16,18,9,10,35,34},{15,14,13,12,11,36}});} } else if(c==122&&n<=6&&m<=6&&std::max(n,m)==6){ if(n==5&&m==6){pl({{1,3,5,13,12,11},{23,2,4,6,14,10},{22,24,16,15,7,9},{21,17,25,27,29,8},{20,19,18,26,28,30}});}else if(m==5&&n==6){pl({{1,3,11,10,9},{26,2,4,12,8},{25,27,13,5,7},{22,24,28,14,6},{21,23,18,29,15},{20,19,17,16,30}});}else if(n==6&&m==6){pl({{1,19,18,16,14,13},{20,2,17,15,10,12},{29,21,3,5,9,11},{28,30,22,4,6,8},{27,23,31,33,35,7},{26,25,24,32,34,36}});} } else if (c == 218 && n == 6 && m == 6) { pl({{1,17,19,28,27,26},{16,2,18,20,29,25},{15,14,3,30,21,24},{11,13,31,4,23,22},{10,12,5,32,35,34},{9,8,7,6,33,36}}); } else if (c == 87) { if ((n | m) & 1) return wrk(n, m, 85);ans[x=0][y=0]=tot=1; W(y<m-3){if(y)nx(0,1);rep({{1,0}},n-1),nx(0,1),rep({{-1,0}},n-1);} nx(1,1),nx(-1,0),nx(0,1),nx(1,0);W(x<n-1)nx(1,0),nx(0,y&1?-1:1); } else { if (mark[c] && shf(c) != c) { vector<vi>res=wrk(m,n,shf(c));if(res.empty())return{};U(i,0,n)U(j,0,m)ans[i][j]=res[j][i]; } else { L(h, 0, c) if ((h | c) == c && chk(n, m, h)) return wrk(n, m, h); return {}; } } return ans; } fv slv(){ int n = ur(), m = ur(), c = ur(); if (c == 256) { u64 v[4] {}; auto sh = [&](u64 &x) {x|=(x&0x5555555555555555)<<1;x|=(x&0x3333333333333333)<<2;x|=(x&0x0f0f0f0f0f0f0f0f)<<4;x|=(x&0x00ff00ff00ff00ff)<<8;x|=(x&0x0000ffff0000ffff)<<16;x|=(x&0x00000000ffffffff)<<32;}; U(i, 0, 256) if (chk(n, m, i)) v[i >> 6] |= 1ull << (i & 63); F(auto &x : v) sh(x); v[1] |= v[0], v[2] |= v[0], v[3] |= v[1] | v[2]; U(i, 0, 256) std::cout << (char)(v[i>>6]>>(i&63)&1|48); std::cout << '\n'; return; } out(wrk(n, m, c)); } ``` :::: ::::info[code by chatgpt] ```cpp #include<bits/stdc++.h> #define F for #define R return #define A auto #define B emplace_back using namespace std;using J=int;using L=long long;using V=vector<pair<J,J>>;using I=vector<J>;J D[]={0,1,1,1,0,-1,-1,-1},H[]={1,1,0,-1,-1,-1,0,1};V aa(V p){F(A&z:p)swap(z.first,z.second);R p;}V ab(J n,J m){V p;F(J i=0;i<n;i++)F(J j=0;j<m;j++)p.B(i,(i&1)?m-1-j:j);R p;}void ac(V&p,J r,J m){F(J j=m-1;j>=0;j--){p.B(r,j);p.B(r+1,j);}F(J j=0;j<m;j++)p.B(r+2,j);}void ad(V&p,J n,J c){F(J i=n-1;i>=0;i--){p.B(i,c);p.B(i,c+1);}F(J i=0;i<n;i++)p.B(i,c+2);}V ae(J n,J m){if(n%3!=1)R aa(ae(m,n));V p;F(J j=0;j<m;j++)p.B(0,j);F(J i=1;i<n;i+=3)ac(p,i,m);R p;}V af(J n,J m){V p;F(J j=0;j<m;j+=2)F(J k=0;k<n;k++){J i=(j/2&1)?n-1-k:k;p.B(i,j);p.B(i,j+1);}R p;}V ag(string s){V p={{0,0}};F(char c:s)p.B(p.back().first+D[c-'0'],p.back().second+H[c-'0']);R p;}V ah(J n,J m,J c){if(n%3==1||m%3==1)R ae(n,m);J a=n%3==2?5:6,b=m%3==2?5:6;string s[2][4]={{"010500222525525222005010","00000225252525252220050105002","00002252525252125220005050022","00210505002222525552522220050501200"},{"000022305355000335220000","00000230533550003352200005002","00002223053550500030335220000","00000222255355000023535220300050020"}};V p=ag(s[c==45][(a-5)*2+b-5]);F(J i=a;i<n;i+=3)ac(p,i,b);F(J j=b;j<m;j+=3)ad(p,n,j);R p;}void ai(V&p,J n,J c){F(J i=n-1;i>=0;i--)p.B(i,c);F(J i=0;i<n;i++)p.B(i,c+1);}V aj(J n,J m,J c){if(m&1)R aa(ab(m,n));V p;J b;if(c==77){b=2;F(J i=0;i<n;i++){p.B(i,0);p.B(i,1);}}else{b=4;p=ae(n,4);}F(J j=b;j<m;j+=2)ai(p,n,j);R p;}V ak(J n,J m){V p;F(J s=0;s<n+m-1;s++){J lo=max(0,s-m+1),hi=min(n-1,s);if(s&1)F(J i=lo;i<=hi;i++)p.B(i,s-i);else F(J i=hi;i>=lo;i--)p.B(i,s-i);}R p;}V al(J n,J m){if(n&1)R ab(n,m);if(m&1)R aa(ab(m,n));V p=ag("000244414200602");F(J i=4;i<n;i+=2){F(J j=3;j>=0;j--)p.B(i,j);F(J j=0;j<4;j++)p.B(i+1,j);}F(J j=4;j<m;j+=2)ai(p,n,j);R p;}V am(J n,J m){if(m%4==2)R af(n,m);V p;J c=m-4;F(J j=0;j<c;j+=2)F(J k=0;k<n;k++){J i=(j/2&1)?n-1-k:k;p.B(i,j);p.B(i,j+1);}if(n&1){F(J i=0;i<n;i++){p.B(i,c);p.B(i,c+1);}F(J i=n-1;i>=0;i--)p.B(i,c+2+((n-1-i)&1));F(J i=0;i<n;i++)p.B(i,c+3-(i&1));}else{p.B(0,c);p.B(0,c+1);F(J i=1;i<n;i++)p.B(i,c+((i+1)&1));F(J i=n-1;i>=1;i--)p.B(i,c+(i&1));F(J i=0;i<n;i++){p.B(i,c+2);p.B(i,c+3);}}R p;}void an(V&p,J n,J c){F(J r=0;r<n;++r){p.B(r,c);p.B(r,c+1);}F(J r=n-1;r>=0;--r)p.B(r,c+2);}void ao(V&p,J r,J l,J rr){F(J c=l;c<=rr;++c)p.B(r,c);p.B(r-1,rr);p.B(r-2,rr);F(J c=rr-1;c>=l;--c){p.B(r-1,c);p.B(r-2,c);}}V ap(J n,J m){V p;if(m%3==2){J c=0;F(;c+2<m;c+=3)an(p,n,c);F(J r=0;r<n;++r){p.B(r,c);p.B(r,c+1);}}else{F(J r=0;r<n;++r){p.B(r,0);p.B(r,1);}F(J r=n-1;r>=3;r-=3)ao(p,r,2,m-3);F(J c=2;c<m;++c)p.B(0,c);F(J r=1;r<n;++r){p.B(r,m-2);p.B(r,m-1);}}R p;}V aq(J n,J m){V p;J of=0;F(;m-of>7;of+=3)an(p,n,of);J w=m-of,W=w-2,h=n%3;if(!h)h=3;if(w==7&&h==3)h=6;F(J r=0;r<n;++r){p.B(r,of);p.B(r,of+1);}F(J r=n-1;r>=h+2;r-=3)ao(p,r,of+2,of+w-3);string ca;if(h==1)F(J c=0;c<W;++c)ca+=c+48;else if(h==2&&W==3)ca="301245";else if(h==2&&W==4)ca="40512367";else if(h==2&&W==5)ca="5061723489";else if(h==3&&W==3)ca="630124578";else if(h==3&&W==4)ca="8401273695:;";else ca="IJE@D?:50126<=>94837;ABHCGKFLM";F(J z:ca){z-=48;p.B(z/W,of+2+z%W);}F(J r=h;r<n;++r){p.B(r,m-2);p.B(r,m-1);}R p;}void ar(V&p,J n,J c,J a){if(a==0){F(J r=0;r<n;++r)p.B(r,c+(r&1));F(J r=n-1;r>=0;--r)p.B(r,c+((r&1)^1));}else{F(J r=a;r>=0;--r)p.B(r,c+((a-r)&1));F(J r=0;r<n;++r)p.B(r,c+(r&1));F(J r=n-1;r>a;--r)p.B(r,c+((r&1)^1));}}V as(J n,J m){V p;ar(p,n,0,0);J o=0;F(J c=2;c<m-2;c+=2){J a=o<n-2?o+1:o-1;ar(p,n,c,a);o=a+1;}ar(p,n,m-2,n-1);R p;}V at(J n,J m,J c){if((1LL*(n-1)*(m+1))%3==0)R ap(n,m);J w=5+(m-5)%3;if(c==105&&n==6&&w==7)w=10;if(c==201&&n==5&&w==6)w=9;J of=m-w;V p;F(J cc=0;cc<of;cc+=3)an(p,n,cc);F(J r=0;r<n;++r){p.B(r,of);p.B(r,of+1);}J W=w-2,h=0,tp=n-1;string ca;if(c==89){h=n%3==0?3:5;if(h==3&&W==4)ca="8954012367:;";if(h==3&&W==5)ca=":;<7650123489=>";if(h==5&&W==4)ca="@A=<8954012367:;>?BC";if(h==5&&W==5)ca="DEFA@?:;<7650123489=>BCGH";}else if(c==105){if(W==4){h=n%3==0?3:2;if(h==2)ca="45012367";else ca="8945012367:;";}else if(W==5){h=n%3==0?9:5;if(h==5)ca="DEF@A<67;?:50123489=>BCGH";else ca="XYZTUPJKOSNIDEF@A<67;?:50123489=>BCGHLMQRVW[\\";}else{h=6;ca="XPH@8901234567>?FMTUVWOGNELSJQIABC:;<=DKRYZ[\\]^_";}}else{if(W==4){h=n%3==0?6:8;if(h==6)ca="DEA=@<9584012367:;>?BCFG";else ca="LMIEHDA=@<9584012367:;>?BCFGJKNO";}else if(W==5){h=2;ca="5012673489";if(n%3==0){J r=n-1,l=of+2;V q={{r-1,l},{r-1,l+1},{r,l},{r,l+1},{r,l+2},{r-1,l+2},{r-2,l+2},{r-3,l+2},{r-2,l+1},{r-3,l+1},{r-2,l},{r-3,l}};p.insert(p.end(),q.begin(),q.end());tp=n-5;}}else{h=5;ca="LE?8>701239:@FGMNOPIBHA;456<=CDJKQR";}}F(J r=tp;r>=h+2;r-=3)ao(p,r,of+2,of+w-3);F(J z:ca){z-=48;p.B(z/W,of+2+z%W);}F(J r=h;r<n;++r){p.B(r,m-2);p.B(r,m-1);}R p;}V au(J m){string a;if(m==6)a="J3210;IK4=<:HNL5>9GMO?68FD@PR7ECBAQS";else a="NP3210;MOQ4=<:LKTR5>9HJSU?68GID@VX7FECBAWY";J ba=m==6?6:7,d=m-ba;V ol(a.size());F(J z=0;z<(J)a.size();++z)ol[a[z]-48]={z/ba,z%ba};V p;F(J i=0;i<(J)ol.size();++i){A[x,y]=ol[i];p.B(x,y+(y>0?d:0));if(i+1==(J)ol.size())break;A[xx,yy]=ol[i+1];if(y==0&&yy==1)F(J c=1;c<=d;++c){p.B(x+1,c);p.B(x,c);}if(y==1&&yy==0)F(J c=d;c>=1;--c)p.B(x,c);}R p;}struct Q{J h,w,p,lo,mi;const char*di;};V av(J h,J m){static const Q a[]={{5,8,1,3,6,"00:oC?Yb30ljo0"},{6,9,1,4,7,"T0X`od`G`Cob30C[o?"},{7,12,3,2,6,"000Xlood300`kood<::B?>N`[^n:"},{7,13,3,3,7,"000Pboo?mTXXdCSSC?]^^l000`[ooo0"},{7,14,3,2,8,"000P:oooA?000lkoo==SRB:ohh0`[^^n3"},{8,7,1,2,5,"0Pb?m0`k3cZd0Cg?mZ2"},{9,8,1,2,6,"0P:o4?0lgCg[B30`o?lE5mo;"},{10,8,1,3,6,"T5:l[30`Coo0P^nd00lo3OEAoo2"},};J w=m,pe=1,lo=m,add=0;const char*ds=0;F(A&t:a)if(t.h==h&&m>=t.mi&&(m-t.w)%t.p==0){w=t.w;pe=t.p;lo=t.lo;add=(m-w)/pe;ds=t.di;break;}if(h==5&&m==5)ds="0:C?a3lj0";if(h==7&&m==5)ds="0:C?`Cl3NAo2";if(h==12&&m==6)ds="PZ8i8l8n8b8n0N3lCd?a7`_>";V p;J x=0,y=0;F(J i=0;i<h*m;++i){p.B(x,y);if(i+1==h*m)break;J yy=y<lo?y:y<lo+(2+add)*pe?lo+(y-lo)%pe:y-add*pe;J z=x*w+yy,d="1346"[(ds[z/3]-48)>>(z%3*2)&3]-48;x+=D[d];y+=H[d];}R p;}V aw(J n){if(n<8)R av(n,5);string a="0DCA?E1B@>GF24=IH53<RJ76;QSK8:PLTV9ONMUW";V ol(40);F(J z=0;z<40;++z)ol[a[z]-48]={z/5,z%5};V p;J d=n-8;F(J i=0;i<40;++i){A[x,y]=ol[i];p.B(x+(x>=4?d:0),y);if(i==39)break;A[xx,yy]=ol[i+1];if(x==3&&xx==4)F(J r=4;r<4+d;++r){p.B(r,y+1);p.B(r,y);}if(x==4&&xx==3)F(J r=3+d;r>=4;--r)p.B(r,y);}R p;}V ax(J n,J m){if(m==5)R aw(n);J h=5+(n-5)%6;if(h==6&&m==6)h=12;A p=av(h,m);if(h<n){A q=au(m);F(J of=h;of<n;of+=6)F(A[x,y]:q)p.B(of+x,y);}R p;}J ay(J c){J to[]={2,1,0,7,6,5,4,3},s=0;F(J d=0;d<8;d++)if(c>>d&1)s|=1<<to[d];R s;}bool az(L n,L m,J c){switch(c){case 21:R n%2==1;case 37:R n%3==1||m%3==1;case 41:R m%4==2;case 73:R n%3==1||m%3==2;case 90:R!(n==6&&(m==5||m==6));case 105:R!(n==6&&m==7);case 201:R!(n==5&&m==6);case 171:R m%2==0;case 186:R n%2==0&&m%2==0&&m>n;case 94:case 122:R n==6&&(m==5||m==6);case 218:R n==6&&m==6;default:R true;}}void ba(L n,L m,J v[256]){fill(v,v+256,-1);F(J c:{21,37,41,73,39,45,75,77,89,101,105,141,201,90,87,171,186,94,122,218}){if(az(n,m,c)&&v[c]<0)v[c]=c*2;J t=ay(c);if(az(m,n,c)&&v[t]<0)v[t]=c*2+1;}F(J d=0;d<8;d++)F(J c=0;c<256;c++)if((c>>d&1)&&v[c]<0)v[c]=v[c^(1<<d)];}V bb(J n,J m,J ty){J c=ty;switch(c){case 21:R ab(n,m);case 37:R ae(n,m);case 41:R af(n,m);case 73:R ap(n,m);case 39:case 45:R ah(n,m,c);case 75:R aq(n,m);case 77:case 101:R aj(n,m,c);case 89:case 105:case 201:R at(n,m,c);case 141:R ak(n,m);case 90:R ax(n,m);case 87:R al(n,m);case 171:R am(n,m);case 186:R as(n,m);case 94:R ag(m==5?"11116666364411331446666121161":"16161116664413414314466661211616612");case 122:R ag(m==5?"16111666441311345613466661111":"16111165616644413131444661656111161");case 218:R ag("17133474671717413433361714717663411");}R{};}J main(){ios::sync_with_stdio(false);cin.tie(0);J T,t;cin>>T>>t;while(T--){L n,m;J c;cin>>n>>m>>c;J v[256];ba(n,m,v);if(c==256){F(J i=0;i<256;i++)cout<<(v[i]>=0);cout<<'\n';}else if(v[c]<0)cout<<"0\n";else{cout<<"1\n";V p=v[c]&1?aa(bb(m,n,v[c]/2)):bb(n,m,v[c]/2);I a(n*m);F(J i=0;i<(J)p.size();i++)a[p[i].first*m+p[i].second]=i+1;F(J i=0;i<n;i++)F(J j=0;j<m;j++)cout<<a[i*m+j]<<" \n"[j+1==m];}}}//GPT6test ``` ::::