跳到正文

个人记录

草

xxxxxzy约 117 分钟
原文
文章信息与版本

文章信息

发布
2026年8月5日
收录
2026年11月8日
更新
2026年11月14日
篇幅
约 117 分钟

版本

  1. v1当前

首先一个事情是方向存在某种对称性,由于 00 和 22 是对称的,33 和 77 是对称的,44 和 66 是对称的,所以我们的讨论量可以减半。

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

(0,2,4)(0,2,4):

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

(0,2,5)(0,2,5):

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

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

(0,3,5)(0,3,5):

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

*(0,3,6)(0,3,6):

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

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

(0,1,2,4)(0,1,2,4):

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

(0,1,2,5)(0,1,2,5):

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

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

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

5×55 \times 5:

5×65 \times 6:

6×66 \times 6:

(0,1,3,5)(0,1,3,5):

和 (0,3,5)(0,3,5) 等价。

*(0,1,3,6)(0,1,3,6):

首先继承来自 (0,3,6)(0,3,6) 的答案,[(m+1)(n−1)≡0 mod 3][(m+1)(n-1) \equiv 0 \bmod 3]。但是我们发现其实比这个强得多。

问题规模难以缩减,但是我们发现,对于 n,mn,m 较大的情况,构造一定存在,具体来说有以下构造,以 10×1010 \times 10 举例子:

大概就是先沿着外面绕一圈,然后进入内部的一个小矩形,最后从右下方出去。但是这个还需要进行调整,调整是针对 nn 的,主要针对内部小矩阵的 nn 调整:

  • 从 47→6447 \to 64 可以看作一个 3×m3 \times m 的子结构,同理有一个平凡的 4×m4 \times m 的子结构,由裴蜀定理可知用 3a+4b+1/23a+4b+1/2 可以表示所有 n≥5n \ge 5。需要单独讨论一下 n=6n=6。

刚刚我们讨论完了 n≥5∧m≥4n \ge 5 \wedge m\ge 4 的情况,现在讨论一下比较小的情况。

  • m=3m=3:比较平凡,有解当且仅当 nn 为奇数。

  • n=3n=3:首先对于 3×33 \times 3 有解:

然后 3×43 \times 4 无解,剩下的全部有解,大概思路是对 mm 讨论,如果 mm 模 33 的余数为 00,那么用下图中间 3×33 \times 3 的小矩阵循环。否则中间再加一个形如 10→16→14→12→1810 \to 16\to 14 \to 12 \to 18 进行 11 的调整即可。

  • n=4n=4:首先 (4,4)(4,4) 无解,对于 n>4n >4 的情况比较平凡,大概就是下图:

然后我们终于做完了这个情况!

(0,2,3,4)(0,2,3,4):

和 (0,2,4)(0,2,4) 等价。

(0,2,3,5)(0,2,3,5):

首先继承 (0,2,5)(0,2,5) 的 [(n−1)(m−1)≡0 mod 3][(n-1)(m-1) \equiv 0 \bmod 3],然后考虑我们可以让 mm 的规模减少 33 或者 44,那么可以全覆盖的 mm 可以表示为 3a+4b+13a+4b+1 或者 3a+4b+23a+4b+2 的形式,可以覆盖所有 mm,那么所有都可行。

还需要判一些细节,对于 (3,3)(3,3) 无解,然后考虑 m=3m=3,除了 (3,3)(3,3) 和 (6,3)(6,3) 都无解,构造和上一个情况的 n=3n=3 类似,大概长这样,总体来说是拿 3×33 \times 3 循环,再做微调:

(0,2,3,6)(0,2,3,6):

和上面那个基本上同理,要弱智一点。

(0,2,3,7)(0,2,3,7):

一定可行,因为可以一个斜行一个斜行走。

(0,2,4,5)(0,2,4,5):

一定可行,和 (0,2,3,6)(0,2,3,6) 同理。

(0,2,4,6)(0,2,4,6):

网格图哈密顿路,当且仅当 n,mn,m 有一个奇数有解。

(0,3,4,5)(0,3,4,5):

和 (0,3,5)(0,3,5) 等价。

(0,3,4,6)(0,3,4,6):

首先如果 m≥5m \ge 5 一定有解,因为大概长这样,nn 可以通过调整达到:

如果 m=4m=4 可以从 (0,3,6)(0,3,6) 继承答案,如果 m=3m=3 一定无解。

*(0,3,5,6)(0,3,5,6):

首先 m=3,4m=3,4 无解,然后继承 (0,3,5)(0,3,5) 的 [m≡2 mod 4][m \equiv 2 \bmod 4],和 (0,3,6)(0,3,6) 的 [(m+1)(n−1)≡0 mod 3][(m+1)(n-1) \equiv 0 \bmod 3]。

首先对 mm 可以缩减规模,如果 (n,m)(n,m) 合法,那么 (n,m+3)(n,m+3) 必定合法。显然有一些 n×3,n×4n \times 3, n \times 4 的结构,合法的 mm 可以写为 3a+4b+23a+4b+2,那么除了 77 以外的 mm 都是合法的。对 m=7m=7 单独讨论。

首先 n=3k+1n=3k+1 对于 m=7m=7 肯定有解,对于 3k+23k+2 的构造大概长这样,n=3kn=3k 时无解(存疑):

(0,3,5,7)(0,3,5,7):

和 (0,3,5)(0,3,5) 等价。

*(0,3,6,7)(0,3,6,7):

首先继承 (0,3,6)(0,3,6) 的 [(m+1)(n−1)≡0 mod 3][(m+1)(n-1) \equiv 0 \bmod 3]。

很明显如果 (n,m)(n,m) 合法,那么 (n,m+3)(n,m+3) 合法。首先 m≤4m \le 4 显然无解,讨论 m≥5m \ge 5 的情况:

m=5m=5 显然合法,对 m=6m=6 讨论,首先 (3,6)(3,6) 和 (5,6)(5,6) 无解,对于 n≥6n \ge 6,我们注意到有构造如下图,其中中间两列可以一直循环 2×22 \times 2,而且可以把 18→1918 \to 19 改为新占一行来调整奇偶性,所以对于 n≥6n \ge 6 均有解:

最后对 m=7m=7 讨论,再次根据 nn 模 33 的值讨论:

  • nn 模 33 为 11:这个 (0,3,6)(0,3,6) 已经讨论了。
  • nn 模 33 为 22:大概长这样:

  • nn 模 33 为 00:大概长这样:

会意即可吧,懒得写了,总之就是对于 m≥5m \ge 5,除了 (3,6),(5,6)(3,6),(5,6) 都有解。

*(1,3,4,6)(1,3,4,6):

注:这一部分 nullpt2 的构造比我的简洁很多,具体见代码。

首先考虑 n,mn,m 都比较大的情况,我们断言此时一定有合法构造,具体来说,拿 11×1111 \times 11 举例子,如下图:

构造的基本思路,是左上角一块 a×2a \times 2,左下一块 b×3b \times 3(bb 是奇数),右边同理,中间是若干 3c+4d+23c+4d+2 拼合成的块。

首先考虑 3c+4d+23c+4d+2 无法表示哪些数,1,3,4,71,3,4,7 无法表示,1,3,41,3,4 先不考虑,考虑 n=7n=7 怎么进行特殊构造,对 mm 对 33 取模的值讨论即可,具体来说如下面三张图,懒得写了:

接下来考虑 n≠7n \ne 7,并且 nn 比较大的情况,两边会有一点细节的调整(22 列和 33 列的交界处)。

关于这个细节调整,简直是灾难性的,笔者写完了才意识到这个问题,首先对 n=9n=9 有构造:

然后对于 n=5n=5 有构造:

然后大的部分有两个 33 结构和两个 44 结构的调整都是平凡的,问题缩减到小的部分,对 n=6,8,10n=6,8,10 继续调整,可以用一种针对偶数的构造解决。

这个构造应该对 n≥5,m≥6n \ge 5,m \ge 6 是通用的,n<5n < 5 无解(存疑),也就是我们只需要对 mm 较小进行一些讨论:

  • m=3m=3:这个比较简单,有解当且仅当 nn 是奇数并且 n≥5n \ge 5,构造和上面大情况的左侧类似,略去。
  • m=4m=4:构造如下图所示,先把左上角填满,下面的构造就是一直循环 ,当且仅当 nn 是偶数有解。

  • m=5m=5:按 nn 模 44 讨论有点精神污染,事实上有一个非常好调整的构造:

五个方向向量

首先大部分都可以直接枚举子集得到构造,少部分特例如下,注意有很多 5×5,5×6,6×5,6×65\times 5, 5 \times 6, 6 \times 5, 6 \times 6 的特判下文未列出。

(0,1,2,4,6)(0,1,2,4,6):

由 (0,2,4,6)(0,2,4,6) 知道 n,mn,m 不全是偶数时有解。全是偶数有一个比较平凡的构造。

(0,1,3,4,6)(0,1,3,4,6):

从 (0,1,3,6)(0,1,3,6) 先继承大部分的答案,然后对于 n×3n \times 3 和 3×43 \times 4 单独构造即可。

(0,1,3,4,7)(0,1,3,4,7):

这个的任意一个子集都无解,所以考虑手动讨论一下。

有解当且仅当 n=3,4n=3,4 且 mm 为偶数,构造如下:

(0,1,3,5,7)(0,1,3,5,7):

有解当且仅当 mm 是偶数,根据 mm 模 44 的值进行讨论:

  • 当 mm 是 44 的倍数:

  • 当 m≡2 mod 4m \equiv 2 \bmod 4:

*(1,3,4,5,7)(1,3,4,5,7):

有解当且仅当 m>nm>n 并且 n,mn,m 都是偶数,我觉得我放一张图就能看懂了:

大概解释一下,先在最左边嵌进去若干个 n×2n \times 2 调整到 n×(n+2)n \times (n+2),然后构造即可。

(1,3,4,6,7)(1,3,4,6,7):

首先从 (1,3,4,6)(1,3,4,6) 继承答案,那么现在没确定的是 n≤4n \le 4 和 m≤4m \le 4,分开讨论:

  • n=3n=3:当且仅当 mm 是奇数有解,构造如下:

  • n=4n=4:对于 m≥5m \ge 5,全部有解。讨论 mm 的奇偶性即可:

然后对于 m=3/4m=3/4,这个操作方向集合本身是对称的,交换 n,mn,m 构造即可。

方向向量超过五个

直接枚举子集即可。


讲得太狗屎了,给一份 std,std 写得慢且臭长,写了 30K,nullpt2 只写了大概 10K 左右:

代码
C++
#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}));
      if (m != 7) {
        return 1;
      } else {
        if (n % 3 == 0) return 0;
      }
      return 1;
    } 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) return cl;
        rep (i, 1, n) id[i][1] = ++tot, id[i][2] = ++tot;
        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("5-1.in", "r", stdin);
  // freopen("1.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();
}
code by nullpt2
C++
#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));
}
原文