跳到正文

题解

OJ5803 GPT-5.2 Thinking

HsCyril约 11 分钟
原文

这是 2026年1月11日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

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

版本

  1. v2当前
  2. v1

这题本质是在问:是否存在一个 n 阶群 (S,×)(S,\times) 以及三个排列 A,B,CA,B,C,使得对所有 x∈Sx\in S 都有

A(x)×B(x)=C(x).A(x)\times B(x)=C(x).

关键等价(把题目化简)

如果存在解,利用 AA 是双射,我们可以把元素整体“重命名”(做同构变换),不改变“是否存在解”这个事实,从而 不失一般性地令 A(x)=xA(x)=x。
这样条件变为:

x×B(x)=C(x),x\times B(x)=C(x),

且 B,CB,C 都是排列。也就是:找一个排列 BB,使得映射

x↦x×B(x)x\mapsto x\times B(x)

也是排列。群论里这叫 complete mapping / orthomorphism。

已知结论(Hall–Paige 定理的直接推论):

有限群存在 complete mapping 当且仅当 其 Sylow-2 子群 不是循环的(或 2-部分为 1)。
对应到阶 nn 的存在性判定就是:

  • YES 当且仅当 nn 为奇数 或 4∣n4\mid n
  • NO 当且仅当 n≡2(mod4)n\equiv 2 \pmod 4

接下来只要给出 构造 即可。


构造方案

1) nn 为奇数:用循环群 Zn\mathbb Z_n

定义:

  • 运算:i×j=(i+j) mod ni\times j=(i+j)\bmod n
  • A(x)=xA(x)=x
  • B(x)=xB(x)=x
  • C(x)=2x mod nC(x)=2x\bmod n

因为 nn 奇数时 22 在模 nn 下可逆,所以 x↦2xx\mapsto 2x 是排列。


2) 4∣n4\mid n:用 (Z2)k×Zm(\mathbb Z_2)^k \times \mathbb Z_m,其中 n=2km, mn=2^k m,\ m 为奇数,且 k≥2k\ge 2

把元素编号为

x=a⋅m+b,a∈[0,2k−1], b∈[0,m−1]x = a\cdot m + b,\quad a\in[0,2^k-1],\ b\in[0,m-1]

定义群运算

(a,b)×(a′,b′)=(a⊕a′, (b+b′) mod m)(a,b)\times(a',b')=(a\oplus a',\ (b+b')\bmod m)

排列取:

  • A(x)=xA(x)=x
  • B(a,b)=(mulX(a), b)B(a,b)=(\text{mulX}(a),\,b)
  • C(a,b)=(a⊕mulX(a), 2b mod m)C(a,b)=(a\oplus \text{mulX}(a),\,2b\bmod m)

其中 mulX(a)\text{mulX}(a) 表示在 GF(2k)GF(2^k) 中乘以 xx 的线性双射(用一个固定本原多项式实现)。
这样 a↦mulX(a)a\mapsto \text{mulX}(a) 是排列;同时 a↦a⊕mulX(a)a\mapsto a\oplus \text{mulX}(a) 等价于乘以 x+1x+1 也是排列;而 mm 为奇数保证 b↦2bb\mapsto 2b 是排列,所以 B,CB,C 都是排列。

C++ 代码(直接可交,输出乘法表与三行排列)

C++
#include <bits/stdc++.h>
using namespace std;

// primitive/irreducible polynomials for GF(2^k) (standard choices)
// stored as bitmasks including the x^k term.
// k up to 9 is enough; here n <= 500 so k <= 8 actually.
static int primPoly[10] = {
    0, 0,
    0b111,        // k=2:  x^2 + x + 1
    0b1011,       // k=3:  x^3 + x + 1
    0b10011,      // k=4:  x^4 + x + 1
    0b100101,     // k=5:  x^5 + x^2 + 1
    0b1000011,    // k=6:  x^6 + x + 1
    0b10001001,   // k=7:  x^7 + x^3 + 1
    0x11D,        // k=8:  x^8 + x^4 + x^3 + x^2 + 1
    0x211         // k=9:  x^9 + x^4 + 1
};

// multiply by x in GF(2^k) with modulus primPoly[k]
static inline int mulX(int a, int k) {
    int mask = (1 << k) - 1;
    int msb = 1 << (k - 1);
    int poly = primPoly[k] & mask; // drop x^k term
    int res = (a << 1) & mask;
    if (a & msb) res ^= poly;
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;

        if (n % 2 == 1) {
            // YES: cyclic group Z_n
            cout << "YES\n";
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (j) cout << ' ';
                    cout << (i + j) % n;
                }
                cout << "\n";
            }
            // A = id
            for (int i = 0; i < n; i++) {
                if (i) cout << ' ';
                cout << i;
            }
            cout << "\n";
            // B = id
            for (int i = 0; i < n; i++) {
                if (i) cout << ' ';
                cout << i;
            }
            cout << "\n";
            // C(i)=2i mod n
            for (int i = 0; i < n; i++) {
                if (i) cout << ' ';
                cout << (2LL * i) % n;
            }
            cout << "\n";
        } else if (n % 4 == 2) {
            // NO
            cout << "NO\n";
        } else {
            // YES: n divisible by 4
            // n = 2^k * m, with m odd, k>=2
            int k = 0;
            int tmp = n;
            while ((tmp & 1) == 0) { k++; tmp >>= 1; }
            int m = tmp;                 // odd part
            int pw = 1 << k;             // 2^k
            // build table for group (Z2)^k x Z_m
            cout << "YES\n";
            for (int i = 0; i < n; i++) {
                int ai = (m == 0 ? 0 : i / m);
                int bi = (m == 0 ? 0 : i % m);
                for (int j = 0; j < n; j++) {
                    int aj = (m == 0 ? 0 : j / m);
                    int bj = (m == 0 ? 0 : j % m);
                    int a = ai ^ aj;
                    int b = (m == 1 ? 0 : (bi + bj) % m);
                    int val = a * m + b;
                    if (j) cout << ' ';
                    cout << val;
                }
                cout << "\n";
            }

            // A = id
            for (int i = 0; i < n; i++) {
                if (i) cout << ' ';
                cout << i;
            }
            cout << "\n";

            // B(x) = (mulX(a), b)
            for (int x = 0; x < n; x++) {
                int a = (m == 0 ? 0 : x / m);
                int b = (m == 0 ? 0 : x % m);
                int a2 = mulX(a, k);
                int bx = a2 * m + b;
                if (x) cout << ' ';
                cout << bx;
            }
            cout << "\n";

            // C(x) = x * B(x) = (a xor mulX(a), 2b mod m)
            for (int x = 0; x < n; x++) {
                int a = (m == 0 ? 0 : x / m);
                int b = (m == 0 ? 0 : x % m);
                int a2 = mulX(a, k);
                int a3 = a ^ a2;
                int b3 = (m == 1 ? 0 : (2LL * b) % m);
                int cx = a3 * m + b3;
                if (x) cout << ' ';
                cout << cx;
            }
            cout << "\n";
        }
    }
    return 0;
}

这个程序满足:

  • 输出的乘法表一定是群(循环群或直积群)
  • 输出的 A/B/C 都是 (0..n-1) 的排列
  • 且对所有 (x):(A(x)\times B(x)=C(x))

如果你还想把证明写得更“比赛友好”(不引用 Hall–Paige 名字、只写结论与直观说明),我也可以帮你把题解文字润色成可直接提交到博客/题解区的版本。