跳到正文

算法·理论

学习笔记——Nim 积

r_syh0213约 18 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v2当前
  2. v1

太难了!!!

了解更多:《On Numbers and Games》——J. H. Conway

定义

两数 x,yx,y 的 Nim 积定义为:

x⊗y=mex⁡{(x⊗j)⊕(y⊗i)⊕(i⊗j)∣0≤i<x,0≤j<y}x \otimes y = \operatorname{mex}\{ (x \otimes j) \oplus (y \otimes i) \oplus (i \otimes j) \mid 0 \le i < x,0 \le j < y \}

⊗\otimes 为 Nim 积,⊕\oplus 为异或。

打表

⊗\otimes012345678910
000000000000
1012345678910
20231810119121415
3031212151314475
40481262141011153
505101527813369
6061113148537112
7079141013341586
8081241137151351
9091471561851211
100101553912611114

性质

证明等我学会了再补 (可能这辈子都学不会)。

运算性质

由表可以看出来,x⊗0=0,x⊗1=xx \otimes 0 = 0,x \otimes 1 = x。

证明

当 y=0y=0 时,不存在 jj 满足 0≤j<y0 \le j <y,所以 {(x⊗j)⊕(y⊗i)⊕(i⊗j)∣0≤i<x,0≤j<y}=∅\{ (x \otimes j) \oplus (y \otimes i) \oplus (i \otimes j) \mid 0 \le i < x,0 \le j < y \} =\varnothing,所以 x⊗0=mex⁡∅=0x \otimes 0 = \operatorname{mex} \varnothing =0。

当 y=1y=1 时,jj 只能取 00,即:

x⊗1=mex⁡{(x⊗0)⊕(1⊗i)⊕(0⊗i)∣0≤i<x}=mex⁡{0⊕(1⊗i)⊕0∣0≤i<x}=mex⁡{(1⊗i)∣0≤i<x}\begin{aligned} &x \otimes 1 \\ =& \operatorname{mex}\{(x \otimes 0) \oplus (1 \otimes i) \oplus (0 \otimes i) \mid 0 \le i <x \} \\ =& \operatorname{mex}\{0 \oplus (1 \otimes i) \oplus 0 \mid 0 \le i <x \} \\ =& \operatorname{mex}\{(1 \otimes i) \mid 0 \le i <x \} \\ \end{aligned}

依据归纳法可得 x⊗1=mex⁡{i∣0≤i<x}=xx \otimes 1 = \operatorname{mex}\{ i\mid 0 \le i <x \} = x。

Nim 积和异或运算满足交换律、结合律和分配律,即:

x⊗y=y⊗xx⊗(y⊗z)=(x⊗y)⊗zx⊗(y⊕z)=(x⊗y)⊕(x⊗z)\begin{aligned} x \otimes y &= y \otimes x \\ x \otimes (y \otimes z) &= (x \otimes y) \otimes z \\ x \otimes (y \oplus z) &= (x \otimes y) \oplus (x \otimes z) \end{aligned}
证明

交换律

显然。。。。。。

结合律

咕咕咕…

分配律

对 xx 进行强归纳。假设对所有 x′<xx'<x 分配律成立。再对 y,zy,z 进行双重归纳,假设对更小的 y′,z′y',z' 分配律成立(即 y′<yy'<y 或 z′<zz'<z 时命题成立)。

令 w=y⊕zw = y \oplus z。
由定义,x⊗w=mex⁡Sx \otimes w = \operatorname{mex} S。
其中,S={(x′⊗w)⊕(x⊗w′)⊕(x′⊗w′)∣0≤x′<x,0≤w′<w}S = \{ (x'\otimes w)\oplus(x\otimes w')\oplus(x'\otimes w') \mid 0 \le x'< x,0 \le w'<w \}。
要证明 (x⊗y)⊕(x⊗z)=mex⁡S(x \otimes y) \oplus (x \otimes z) = \operatorname{mex} S。

根据归纳假设,对于 x′<xx' < x,有 x′⊗w=(x′⊗y)⊕(x′⊗z)x' \otimes w = (x' \otimes y) \oplus (x' \otimes z)。
将 SS 改写成:

S={((x′⊗y)⊕(x′⊗z))⊕(x⊗w′)⊕(x′⊗w′)∣0≤x′<x,0≤w′<w}S = \{ ((x' \otimes y) \oplus (x' \otimes z)) \oplus (x \otimes w') \oplus (x' \otimes w') \mid 0 \le x' < x,0 \le w' < w \}

然后来证两个东西。

(x⊗y)⊕(x⊗z)∉S( x \otimes y) \oplus (x \otimes z) \notin S

若存在 0≤x′<x,0≤w′<w0 \le x' <x,0 \le w' < w,使得 (x⊗y)⊕(x⊗z)=((x′⊗y)⊕(x′⊗z))⊕(x⊗w′)⊕(x′⊗w′)( x \otimes y) \oplus (x \otimes z) = ((x' \otimes y) \oplus (x' \otimes z)) \oplus (x \otimes w') \oplus (x' \otimes w'),则可以通过归纳证明某个更小三元组违背分配律,与假设矛盾,故上述结论成立。

这一步的详细证明咕咕咕啦。

所有小于 (x⊗y)⊕(x⊗z)( x \otimes y) \oplus (x \otimes z) 的数都属于 SS

令 u=(x⊗y)⊕(x⊗z)u = ( x \otimes y) \oplus (x \otimes z)。
对任意 t<ut < u,要构造 0≤x′<x,0≤w′<w0 \le x' < x,0 \le w' < w,使得

t=(x′⊗w)⊕(x⊗w′)⊕(x′⊗w′)(1)t = (x' \otimes w) \oplus (x \otimes w') \oplus (x' \otimes w')\tag{1}

记 p=x⊗y,q=x⊗zp = x \otimes y,q = x \otimes z。
u=p⊕qu=p \oplus q,因为 t<ut < u,所以存在一个最高位二进制 kk,使得 tk=0,uk=1t_k = 0,u_k = 1 且对任何更高位 j>kj > k,都有 tj=ujt_j = u_j。
由此,pp 和 qq 在第 kk 位恰有一个为 11。
不妨设 pk=1,qk=0p_k = 1,q_k = 0(若相反,则交换 y,zy,z,论证对称)。

现在利用 Nim 积的定义作用于 p=x⊗yp = x \otimes y。
根据定义,pp 是集合 A={(a⊗y)⊕(x⊗b)⊕(a⊗b)∣0≤a<x,0≤b<y}A=\{ (a \otimes y) \oplus (x \otimes b) \oplus (a \otimes b) \mid 0 \le a < x,0 \le b < y\} 的 mex,因此所有小于 pp 的数都属于 AA。
特别地,可以选取一个数 r∈Ar \in A,使得 rr 满足以下条件:

  • r<pr<p。
  • rr 与 pp 在高于 kk 的所有位上完全相同。
  • rr 的第 kk 位为 00。
  • 此外,通过适当选择 rr 低于 kk 的位,可以确保 t⊕r<q(2)t \oplus r < q \tag{2}

为什么这样的 rr 存在?因为 AA 包含了所有小于 pp 的数,而我们可以自由选择任何小于 pp 且具有指定高位模式的数(高位模式由 pp 固定,第 kk 位为 00,低位任意)。这些数都小于 pp,所以都在 AA 中。我们只需从这些数中选一个,使得 (2) 成立即可。由于 (2) 只依赖于低位,而低位可以任意调整(因为 AA 包含了所有这样的数),所以这样的选择总是可行的。

既然 r∈Ar \in A,存在 a<xa < x 和 b<yb < y,使得

r=(a⊗y)⊕(x⊗b)⊕(a⊗b)(3)r = (a \otimes y) \oplus (x \otimes b) \oplus (a \otimes b) \tag{3}

接下来,令 t′=t⊕rt' = t \oplus r。由 (2) 知 t′<q=x⊗zt' < q = x \otimes z。由于 qq 是集合 B={(a′⊗z)⊕(x⊗z′)⊕(a′⊗z′)∣0≤a′<x,0≤z′<z}B = \{ (a' \otimes z) \oplus (x \otimes z') \oplus (a' \otimes z') \mid 0 \le a' < x,0 \le z' < z \} 的 mex,而 t′<qt' < q,所以 t′∈Bt' \in B,即存在 a′<xa' < x 和 z′<zz' < z,使得

t′=(a′⊗z)⊕(x⊗z′)⊕(a′⊗z′)(4)t' = (a' \otimes z) \oplus (x \otimes z') \oplus (a' \otimes z') \tag{4}

现在,将 (3) 和 (4) 结合起来。由 t=t′⊕rt = t' \oplus r,有

t=((a′⊗z)⊕(x⊗z′)⊕(a′⊗z′))⊕((a⊗y)⊕(x⊗b)⊕(a⊗b))(5)t = \bigl( (a' \otimes z) \oplus (x \otimes z') \oplus (a' \otimes z') \bigr) \oplus \bigl( (a \otimes y) \oplus (x \otimes b) \oplus (a \otimes b) \bigr) \tag{5}

现在希望将 (5) 化为形如 (1) 的表达式,其中 x′x' 和 w′w' 待定。利用归纳假设,分配律对更小的 xx 项成立(因为 a<xa < x 和 a′<xa' < x),我们有

a⊗w=(a⊗y)⊕(a⊗z)a′⊗w=(a′⊗y)⊕(a′⊗z)a \otimes w = (a \otimes y) \oplus (a \otimes z) \\ a' \otimes w = (a' \otimes y) \oplus (a' \otimes z)

其中 w=y⊕zw = y \oplus z。于是可以重排 (5) 中的各项,并利用异或的交换律和结合律,将其合并。
具体地,令 x′=a⊕a′x' = a \oplus a'(这里的 ⊕\oplus 是异或,但注意 x′x' 必须小于 xx,这需要验证,但由归纳构造可保证)以及 w′=b⊕z′⊕(a⊗b)⊕(a′⊗z′)⊕…w' = b \oplus z' \oplus (a \otimes b) \oplus (a' \otimes z') \oplus \dots。
注意到 (5) 是若干项的异或,其中每一项形如 u1⊗v1u_1 \otimes v_1。我们利用归纳假设(分配律)将 a⊗ya \otimes y 和 a⊗za \otimes z 合并为 a⊗wa \otimes w,将 a′⊗ya' \otimes y 和 a′⊗za' \otimes z 合并为 a′⊗wa' \otimes w。为此,在 (5) 中,将 a⊗ya \otimes y 和 a′⊗za' \otimes z 等项配对,利用异或的消去律和分配律的归纳形式,可以证明存在某个 x′<xx' < x 和某个 w′<ww' < w 使得 (5) 右侧等于 (x′⊗w)⊕(x⊗w′)⊕(x′⊗w′)(x' \otimes w) \oplus (x \otimes w') \oplus (x' \otimes w')。

经过一系列合法的项重组,总能得到 (1) 的形式。因此,t∈St \in S。

经过上述证明可以得到 x⊗(y⊕z)=(x⊗y)⊕(x⊗z)x \otimes (y \oplus z) = (x \otimes y) \oplus (x \otimes z)。

有关费马数的性质

经过数学家的艰苦卓绝的努力,有两个十分强大的运算法则。

定义 Fermat 2-power x=22nx = 2^{2^n},其中 n∈Nn \in \mathbb{N}。

  1. 一个 Fermat 2-power 与任意小于它的数的 Nim 积为一般意义下乘法的积,x⊗y=x(x>y)x \otimes y = x (x>y)。
  2. 一个 Fermat 2-power 与自己的 Nim 积为自己的 32\frac{3}{2} 倍,x⊗x=x⊕x2=32xx \otimes x = x \oplus \frac{x}{2} = \frac{3}{2} x。

求 Nim 积

暴力求是 O(x2y2)O(x^2 y^2) 的,利用一些性质能做到 O(log⁡xlog⁡y)O(\log x \log y)。

定义 f(x,y)=x⊗yf(x,y) = x \otimes y。
x∨y=0/1x \lor y = 0/1 的情况直接判掉。

定义 g(x,y)=2x⊗2yg(x,y)= 2^x \otimes 2^y,所以 f(x,y)=⨂x′∈x,y′∈yg(x′,y′)f(x,y) = \bigotimes_{x' \in x,y' \in y} g(x',y')。
接下来求 g(x,y)g(x,y)。考虑拆位,变成一个个 Fermat 2-power 。

2x⊗2y=(⨂x′∈x22x′)⊗(⨂y′∈y22y′)2^x \otimes 2^y = \left( \bigotimes_{x' \in x} 2^{2^{x'}} \right) \otimes \left( \bigotimes_{y' \in y} 2^{2^{y'}} \right)

从高到低 依次考虑 x,yx,y 的每一位,如果这位都为 00 显然可以跳过。

若两个都为 11,对于这一位 2t2^t,设 W=22t,A=2x−2t,B=2y−2tW = 2^{2^t},A = 2^{x - 2^t},B=2^{y - 2^t},有 A,B<WA,B <W。
答案就是 (W⊗A)⊗(W⊗B)(W \otimes A) \otimes (W \otimes B),化简成 32W⊗(A⊗B)\frac{3}{2} W \otimes (A \otimes B)。
接下来将 2x,2y2^x,2^y 去掉最高位的 tt,变成 A,BA,B,继续向低位递归。

若只有一个 11,不妨设 xx 此位为 11。
同理,对于这一位 2t2^t,设 W=22t,A=2x−2t,B=2yW = 2^{2^t},A = 2^{x - 2^t},B=2^y。
答案是 (W⊗A)⊗B=W⊗(A⊗B)(W \otimes A) \otimes B=W \otimes (A \otimes B),将 2x2^x 的最高位 tt 去掉,继续递归。

显然可以利用交换律把两种情况分开:

2x⊗2y=(⨂i∈{x⊕y}22i)⊗(⨂i∈{x∧y}32×22i)=(∏i∈{x⊕y}22i)⊗(⨂i∈{x∧y}32×22i)\begin{aligned} &2^x \otimes 2^y \\ =& \left( \bigotimes_{i \in \{ x \oplus y \}} 2^{2^i} \right) \otimes \left( \bigotimes_{i \in \{ x \land y \}} \frac{3}{2} \times 2^{2^i} \right) \\ =& \left( \prod_{i \in \{ x \oplus y \}} 2^{2^i} \right) \otimes \left( \bigotimes_{i \in \{ x \land y \}} \frac{3}{2} \times 2^{2^i} \right) \end{aligned}

对于前者可以直接算,后者可以递归计算。
因为递归只会遍历两个所有二进制位,所以单次复杂度为 O(log⁡xlog⁡y)O(\log x\log y)。

C++
int mp[256][256];
int f(int,int);
int g(int x,int y){
	if(!x||!y) return 1ll<<(x|y);
	if(~mp[x][y]) return mp[x][y];
	int res=1;
	for(int i=0,j=(x^y);(1ll<<i)<=j;i++) if((j>>i)&1ll) res<<=(1ll<<i);
	for(int i=0,j=(x&y);(1ll<<i)<=j;i++) if((j>>i)&1ll) res=f(res,3ll<<((1ll<<i)-1));
	return mp[x][y]=res;
}
int f(int x,int y){
	if(!x||!y) return x|y;
	if(x==1||y==1) return max(x,y);
	int res=0;
	for(int i=0;(1ll<<i)<=x;i++) if((x>>i)&1ll) 
		for(int j=0;(1ll<<j)<=y;j++) if((y>>j)&1ll) res^=g(i,j);
	return res;
}

题目

HDU3404 Switch lights

在一个二维平面中,有 nn 个灯亮着并告诉你坐标,每回合需要找到一个矩形,这个矩形 (x,y)(x,y) 坐标最大的那个角落的点必须是亮着的灯,然后我们把四个角落的灯状态反转,不能操作为败。
多测,T≤100,n≤1000,xi,yi≤10000T \le 100,n\le 1000,x_i,y_i \le 10000。
裸的二维 Nim 游戏,O(Tnlog⁡xlog⁡y)O(Tn \log x \log y)。

代码
C++
#include<cstring>
#include<iostream>
#define int long long
#define max(a,b) (a>b?a:b)
using namespace std;
int n,t,ans,mp[256][256];
int f(int,int);
int g(int x,int y){
	if(!x||!y) return 1ll<<(x|y);
	if(~mp[x][y]) return mp[x][y];
	int res=1;
	for(int i=0,j=(x^y);(1ll<<i)<=j;i++) if((j>>i)&1ll) res<<=(1ll<<i);
	for(int i=0,j=(x&y);(1ll<<i)<=j;i++) if((j>>i)&1ll) res=f(res,3ll<<((1ll<<i)-1));
	return mp[x][y]=res;
}
int f(int x,int y){
	if(!x||!y) return x|y;
	if(x==1||y==1) return max(x,y);
	int res=0;
	for(int i=0;(1ll<<i)<=x;i++) if((x>>i)&1ll) 
		for(int j=0;(1ll<<j)<=y;j++) if((y>>j)&1ll) res^=g(i,j);
	return res;
}
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> t;
	memset(mp,-1,sizeof mp);
	while(t--){
		cin >> n;ans=0;
		for(int i=1;i<=n;i++){
			int x,y;cin >> x >> y;
			ans^=f(x,y);
		}
		if(ans) cout << "Have a try, lxhgww.\n";
		else cout << "Don't waste your time.\n";
	}
	return 0;
}

P11278 绝世丑角

给定一个长度为 nn 的序列 aa,qq 次操作。

  • 1 l r:对于任意 i∈[l,r],ai=ai⊗aii \in [l,r],a_i = a_i \otimes a_i。
  • 2 l r:求 ⨁i=lrai\bigoplus_{i=l}^r a_i。
  • 3 l r:求 ∑i=lrai\sum_{i=l}^r a_i。

1≤n≤2.5×105,0≤ai≤232,1≤q≤1051 \le n \le 2.5 \times 10^5,0\le a_i \le 2^{32},1\le q \le 10^5。

前方为待施工区域,下次再来吧…

原文