这题本质是在问:是否存在一个 n 阶群 以及三个排列 ,使得对所有 都有
关键等价(把题目化简)
如果存在解,利用 是双射,我们可以把元素整体“重命名”(做同构变换),不改变“是否存在解”这个事实,从而 不失一般性地令 。
这样条件变为:
且 都是排列。也就是:找一个排列 ,使得映射
也是排列。群论里这叫 complete mapping / orthomorphism。
已知结论(Hall–Paige 定理的直接推论):
有限群存在 complete mapping 当且仅当 其 Sylow-2 子群 不是循环的(或 2-部分为 1)。
对应到阶 的存在性判定就是:
- YES 当且仅当 为奇数 或
- NO 当且仅当
接下来只要给出 构造 即可。
构造方案
1) 为奇数:用循环群
定义:
- 运算:
因为 奇数时 在模 下可逆,所以 是排列。
2) :用 ,其中 为奇数,且
把元素编号为
定义群运算
排列取:
其中 表示在 中乘以 的线性双射(用一个固定本原多项式实现)。
这样 是排列;同时 等价于乘以 也是排列;而 为奇数保证 是排列,所以 都是排列。
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 名字、只写结论与直观说明),我也可以帮你把题解文字润色成可直接提交到博客/题解区的版本。