太难了!!!
了解更多:《On Numbers and Games》——J. H. Conway
定义
两数 x , y x,y x , 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 \} x ⊗ y = mex {( x ⊗ j ) ⊕ ( y ⊗ i ) ⊕ ( i ⊗ j ) ∣ 0 ≤ i < x , 0 ≤ j < y }
⊗ \otimes ⊗ 为 Nim 积,⊕ \oplus ⊕ 为异或。
打表
⊗ \otimes ⊗ 0 1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 2 3 4 5 6 7 8 9 10 2 0 2 3 1 8 10 11 9 12 14 15 3 0 3 1 2 12 15 13 14 4 7 5 4 0 4 8 12 6 2 14 10 11 15 3 5 0 5 10 15 2 7 8 13 3 6 9 6 0 6 11 13 14 8 5 3 7 1 12 7 0 7 9 14 10 13 3 4 15 8 6 8 0 8 12 4 11 3 7 15 13 5 1 9 0 9 14 7 15 6 1 8 5 12 11 10 0 10 15 5 3 9 12 6 1 11 14
性质
证明等我学会了再补 (可能这辈子都学不会)。
运算性质
由表可以看出来,x ⊗ 0 = 0 , x ⊗ 1 = x x \otimes 0 = 0,x \otimes 1 = x x ⊗ 0 = 0 , x ⊗ 1 = x 。
证明 当 y = 0 y=0 y = 0 时,不存在 j j j 满足 0 ≤ j < y 0 \le j <y 0 ≤ 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 ⊗ j ) ⊕ ( y ⊗ i ) ⊕ ( i ⊗ j ) ∣ 0 ≤ i < x , 0 ≤ j < y } = ∅ ,所以 x ⊗ 0 = mex ∅ = 0 x \otimes 0 = \operatorname{mex} \varnothing =0 x ⊗ 0 = mex ∅ = 0 。
当 y = 1 y=1 y = 1 时,j j j 只能取 0 0 0 ,即:
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 {( x ⊗ 0 ) ⊕ ( 1 ⊗ i ) ⊕ ( 0 ⊗ i ) ∣ 0 ≤ i < x } mex { 0 ⊕ ( 1 ⊗ i ) ⊕ 0 ∣ 0 ≤ i < x } mex {( 1 ⊗ i ) ∣ 0 ≤ i < x } 依据归纳法可得 x ⊗ 1 = mex { i ∣ 0 ≤ i < x } = x x \otimes 1 = \operatorname{mex}\{ i\mid 0 \le i <x \} = x x ⊗ 1 = mex { i ∣ 0 ≤ i < x } = x 。
Nim 积和异或运算满足交换律、结合律和分配律,即:
x ⊗ y = y ⊗ x x ⊗ ( y ⊗ z ) = ( x ⊗ y ) ⊗ z x ⊗ ( 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} x ⊗ y x ⊗ ( y ⊗ z ) x ⊗ ( y ⊕ z ) = y ⊗ x = ( x ⊗ y ) ⊗ z = ( x ⊗ y ) ⊕ ( x ⊗ z )
证明 交换律显然。。。。。。
结合律咕咕咕…
分配律对 x x x 进行强归纳。假设对所有 x ′ < x x'<x x ′ < x 分配律成立。再对 y , z y,z y , z 进行双重归纳,假设对更小的 y ′ , z ′ y',z' y ′ , z ′ 分配律成立(即 y ′ < y y'<y y ′ < y 或 z ′ < z z'<z z ′ < z 时命题成立)。
令 w = y ⊕ z w = y \oplus z w = y ⊕ z 。
由定义,x ⊗ w = mex S x \otimes w = \operatorname{mex} S x ⊗ w = 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 \} S = {( x ′ ⊗ w ) ⊕ ( x ⊗ w ′ ) ⊕ ( x ′ ⊗ w ′ ) ∣ 0 ≤ x ′ < x , 0 ≤ w ′ < w } 。
要证明 ( x ⊗ y ) ⊕ ( x ⊗ z ) = mex S (x \otimes y) \oplus (x \otimes z) = \operatorname{mex} S ( x ⊗ y ) ⊕ ( x ⊗ z ) = mex S 。
根据归纳假设,对于 x ′ < x x' < x x ′ < x ,有 x ′ ⊗ w = ( x ′ ⊗ y ) ⊕ ( x ′ ⊗ z ) x' \otimes w = (x' \otimes y) \oplus (x' \otimes z) x ′ ⊗ w = ( x ′ ⊗ y ) ⊕ ( x ′ ⊗ z ) 。
将 S S S 改写成:
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 \} S = {(( x ′ ⊗ y ) ⊕ ( x ′ ⊗ z )) ⊕ ( x ⊗ w ′ ) ⊕ ( x ′ ⊗ w ′ ) ∣ 0 ≤ x ′ < x , 0 ≤ w ′ < w } 然后来证两个东西。
( x ⊗ y ) ⊕ ( x ⊗ z ) ∉ S ( x \otimes y) \oplus (x \otimes z) \notin S ( x ⊗ y ) ⊕ ( x ⊗ z ) ∈ / S 若存在 0 ≤ x ′ < x , 0 ≤ w ′ < w 0 \le x' <x,0 \le w' < w 0 ≤ x ′ < x , 0 ≤ 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 ′ ⊗ y ) ⊕ ( x ′ ⊗ z )) ⊕ ( x ⊗ w ′ ) ⊕ ( x ′ ⊗ w ′ ) ,则可以通过归纳证明某个更小三元组违背分配律,与假设矛盾,故上述结论成立。
这一步的详细证明咕咕咕啦。
所有小于 ( x ⊗ y ) ⊕ ( x ⊗ z ) ( x \otimes y) \oplus (x \otimes z) ( x ⊗ y ) ⊕ ( x ⊗ z ) 的数都属于 S S S 令 u = ( x ⊗ y ) ⊕ ( x ⊗ z ) u = ( x \otimes y) \oplus (x \otimes z) u = ( x ⊗ y ) ⊕ ( x ⊗ z ) 。
对任意 t < u t < u t < u ,要构造 0 ≤ x ′ < x , 0 ≤ w ′ < w 0 \le x' < x,0 \le w' < w 0 ≤ x ′ < x , 0 ≤ w ′ < w ,使得
t = ( x ′ ⊗ w ) ⊕ ( x ⊗ w ′ ) ⊕ ( x ′ ⊗ w ′ ) (1) t = (x' \otimes w) \oplus (x \otimes w') \oplus (x' \otimes w')\tag{1} t = ( x ′ ⊗ w ) ⊕ ( x ⊗ w ′ ) ⊕ ( x ′ ⊗ w ′ ) ( 1 ) 记 p = x ⊗ y , q = x ⊗ z p = x \otimes y,q = x \otimes z p = x ⊗ y , q = x ⊗ z 。
u = p ⊕ q u=p \oplus q u = p ⊕ q ,因为 t < u t < u t < u ,所以存在一个最高位二进制 k k k ,使得 t k = 0 , u k = 1 t_k = 0,u_k = 1 t k = 0 , u k = 1 且对任何更高位 j > k j > k j > k ,都有 t j = u j t_j = u_j t j = u j 。
由此,p p p 和 q q q 在第 k k k 位恰有一个为 1 1 1 。
不妨设 p k = 1 , q k = 0 p_k = 1,q_k = 0 p k = 1 , q k = 0 (若相反,则交换 y , z y,z y , z ,论证对称)。
现在利用 Nim 积的定义作用于 p = x ⊗ y p = x \otimes y p = x ⊗ y 。
根据定义,p p p 是集合 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\} A = {( a ⊗ y ) ⊕ ( x ⊗ b ) ⊕ ( a ⊗ b ) ∣ 0 ≤ a < x , 0 ≤ b < y } 的 mex ,因此所有小于 p p p 的数都属于 A A A 。
特别地,可以选取一个数 r ∈ A r \in A r ∈ A ,使得 r r r 满足以下条件:
r < p r<p r < p 。
r r r 与 p p p 在高于 k k k 的所有位上完全相同。
r r r 的第 k k k 位为 0 0 0 。
此外,通过适当选择 r r r 低于 k k k 的位,可以确保
t ⊕ r < q (2) t \oplus r < q \tag{2} t ⊕ r < q ( 2 )
为什么这样的 r r r 存在?因为 A A A 包含了所有小于 p p p 的数,而我们可以自由选择任何小于 p p p 且具有指定高位模式的数(高位模式由 p p p 固定,第 k k k 位为 0 0 0 ,低位任意)。这些数都小于 p p p ,所以都在 A A A 中。我们只需从这些数中选一个,使得 (2) 成立即可。由于 (2) 只依赖于低位,而低位可以任意调整(因为 A A A 包含了所有这样的数),所以这样的选择总是可行的。
既然 r ∈ A r \in A r ∈ A ,存在 a < x a < x a < x 和 b < y b < y b < y ,使得
r = ( a ⊗ y ) ⊕ ( x ⊗ b ) ⊕ ( a ⊗ b ) (3) r = (a \otimes y) \oplus (x \otimes b) \oplus (a \otimes b) \tag{3} r = ( a ⊗ y ) ⊕ ( x ⊗ b ) ⊕ ( a ⊗ b ) ( 3 ) 接下来,令 t ′ = t ⊕ r t' = t \oplus r t ′ = t ⊕ r 。由 (2) 知 t ′ < q = x ⊗ z t' < q = x \otimes z t ′ < q = x ⊗ z 。由于 q q q 是集合 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 \} B = {( a ′ ⊗ z ) ⊕ ( x ⊗ z ′ ) ⊕ ( a ′ ⊗ z ′ ) ∣ 0 ≤ a ′ < x , 0 ≤ z ′ < z } 的 mex,而 t ′ < q t' < q t ′ < q ,所以 t ′ ∈ B t' \in B t ′ ∈ B ,即存在 a ′ < x a' < x a ′ < x 和 z ′ < z z' < z z ′ < z ,使得
t ′ = ( a ′ ⊗ z ) ⊕ ( x ⊗ z ′ ) ⊕ ( a ′ ⊗ z ′ ) (4) t' = (a' \otimes z) \oplus (x \otimes z') \oplus (a' \otimes z') \tag{4} t ′ = ( a ′ ⊗ z ) ⊕ ( x ⊗ z ′ ) ⊕ ( a ′ ⊗ z ′ ) ( 4 ) 现在,将 (3) 和 (4) 结合起来。由 t = t ′ ⊕ r t = t' \oplus r t = t ′ ⊕ 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} t = ( ( a ′ ⊗ z ) ⊕ ( x ⊗ z ′ ) ⊕ ( a ′ ⊗ z ′ ) ) ⊕ ( ( a ⊗ y ) ⊕ ( x ⊗ b ) ⊕ ( a ⊗ b ) ) ( 5 ) 现在希望将 (5) 化为形如 (1) 的表达式,其中 x ′ x' x ′ 和 w ′ w' w ′ 待定。利用归纳假设,分配律对更小的 x x x 项成立(因为 a < x a < x a < x 和 a ′ < x a' < x a ′ < 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) a ⊗ w = ( a ⊗ y ) ⊕ ( a ⊗ z ) a ′ ⊗ w = ( a ′ ⊗ y ) ⊕ ( a ′ ⊗ z ) 其中 w = y ⊕ z w = y \oplus z w = y ⊕ z 。于是可以重排 (5) 中的各项,并利用异或的交换律和结合律,将其合并。
具体地,令 x ′ = a ⊕ a ′ x' = a \oplus a' x ′ = a ⊕ a ′ (这里的 ⊕ \oplus ⊕ 是异或,但注意 x ′ x' x ′ 必须小于 x x x ,这需要验证,但由归纳构造可保证)以及 w ′ = b ⊕ z ′ ⊕ ( a ⊗ b ) ⊕ ( a ′ ⊗ z ′ ) ⊕ … w' = b \oplus z' \oplus (a \otimes b) \oplus (a' \otimes z') \oplus \dots w ′ = b ⊕ z ′ ⊕ ( a ⊗ b ) ⊕ ( a ′ ⊗ z ′ ) ⊕ … 。
注意到 (5) 是若干项的异或,其中每一项形如 u 1 ⊗ v 1 u_1 \otimes v_1 u 1 ⊗ v 1 。我们利用归纳假设(分配律)将 a ⊗ y a \otimes y a ⊗ y 和 a ⊗ z a \otimes z a ⊗ z 合并为 a ⊗ w a \otimes w a ⊗ w ,将 a ′ ⊗ y a' \otimes y a ′ ⊗ y 和 a ′ ⊗ z a' \otimes z a ′ ⊗ z 合并为 a ′ ⊗ w a' \otimes w a ′ ⊗ w 。为此,在 (5) 中,将 a ⊗ y a \otimes y a ⊗ y 和 a ′ ⊗ z a' \otimes z a ′ ⊗ z 等项配对,利用异或的消去律和分配律的归纳形式,可以证明存在某个 x ′ < x x' < x x ′ < x 和某个 w ′ < w w' < w w ′ < w 使得 (5) 右侧等于 ( x ′ ⊗ w ) ⊕ ( x ⊗ w ′ ) ⊕ ( x ′ ⊗ w ′ ) (x' \otimes w) \oplus (x \otimes w') \oplus (x' \otimes w') ( x ′ ⊗ w ) ⊕ ( x ⊗ w ′ ) ⊕ ( x ′ ⊗ w ′ ) 。
经过一系列合法的项重组,总能得到 (1) 的形式。因此,t ∈ S t \in S t ∈ S 。
经过上述证明可以得到 x ⊗ ( y ⊕ z ) = ( x ⊗ y ) ⊕ ( x ⊗ z ) x \otimes (y \oplus z) = (x \otimes y) \oplus (x \otimes z) x ⊗ ( y ⊕ z ) = ( x ⊗ y ) ⊕ ( x ⊗ z ) 。
有关费马数的性质
经过数学家的艰苦卓绝的努力,有两个十分强大的运算法则。
定义 Fermat 2-power x = 2 2 n x = 2^{2^n} x = 2 2 n ,其中 n ∈ N n \in \mathbb{N} n ∈ N 。
一个 Fermat 2-power 与任意小于 它的数的 Nim 积为一般意义下乘法的积,x ⊗ y = x ( x > y ) x \otimes y = x (x>y) x ⊗ y = x ( x > y ) 。
一个 Fermat 2-power 与自己 的 Nim 积为自己的 3 2 \frac{3}{2} 2 3 倍,x ⊗ x = x ⊕ x 2 = 3 2 x x \otimes x = x \oplus \frac{x}{2} = \frac{3}{2} x x ⊗ x = x ⊕ 2 x = 2 3 x 。
求 Nim 积
暴力求是 O ( x 2 y 2 ) O(x^2 y^2) O ( x 2 y 2 ) 的,利用一些性质能做到 O ( log x log y ) O(\log x \log y) O ( log x log y ) 。
定义 f ( x , y ) = x ⊗ y f(x,y) = x \otimes y f ( x , y ) = x ⊗ y 。
x ∨ y = 0 / 1 x \lor y = 0/1 x ∨ y = 0/1 的情况直接判掉。
定义 g ( x , y ) = 2 x ⊗ 2 y g(x,y)= 2^x \otimes 2^y g ( x , y ) = 2 x ⊗ 2 y ,所以 f ( x , y ) = ⨂ x ′ ∈ x , y ′ ∈ y g ( x ′ , y ′ ) f(x,y) = \bigotimes_{x' \in x,y' \in y} g(x',y') f ( x , y ) = ⨂ x ′ ∈ x , y ′ ∈ y g ( x ′ , y ′ ) 。
接下来求 g ( x , y ) g(x,y) g ( x , y ) 。考虑拆位,变成一个个 Fermat 2-power 。
2 x ⊗ 2 y = ( ⨂ x ′ ∈ x 2 2 x ′ ) ⊗ ( ⨂ y ′ ∈ y 2 2 y ′ ) 2^x \otimes 2^y = \left( \bigotimes_{x' \in x} 2^{2^{x'}} \right) \otimes \left( \bigotimes_{y' \in y} 2^{2^{y'}} \right) 2 x ⊗ 2 y = ( x ′ ∈ x ⨂ 2 2 x ′ ) ⊗ y ′ ∈ y ⨂ 2 2 y ′
从高到低 依次考虑 x , y x,y x , y 的每一位,如果这位都为 0 0 0 显然可以跳过。
若两个都为 1 1 1 ,对于这一位 2 t 2^t 2 t ,设 W = 2 2 t , A = 2 x − 2 t , B = 2 y − 2 t W = 2^{2^t},A = 2^{x - 2^t},B=2^{y - 2^t} W = 2 2 t , A = 2 x − 2 t , B = 2 y − 2 t ,有 A , B < W A,B <W A , B < W 。
答案就是 ( W ⊗ A ) ⊗ ( W ⊗ B ) (W \otimes A) \otimes (W \otimes B) ( W ⊗ A ) ⊗ ( W ⊗ B ) ,化简成 3 2 W ⊗ ( A ⊗ B ) \frac{3}{2} W \otimes (A \otimes B) 2 3 W ⊗ ( A ⊗ B ) 。
接下来将 2 x , 2 y 2^x,2^y 2 x , 2 y 去掉最高位的 t t t ,变成 A , B A,B A , B ,继续向低位递归。
若只有一个 1 1 1 ,不妨设 x x x 此位为 1 1 1 。
同理,对于这一位 2 t 2^t 2 t ,设 W = 2 2 t , A = 2 x − 2 t , B = 2 y W = 2^{2^t},A = 2^{x - 2^t},B=2^y W = 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) ( W ⊗ A ) ⊗ B = W ⊗ ( A ⊗ B ) ,将 2 x 2^x 2 x 的最高位 t t t 去掉,继续递归。
显然可以利用交换律把两种情况分开:
2 x ⊗ 2 y = ( ⨂ i ∈ { x ⊕ y } 2 2 i ) ⊗ ( ⨂ i ∈ { x ∧ y } 3 2 × 2 2 i ) = ( ∏ i ∈ { x ⊕ y } 2 2 i ) ⊗ ( ⨂ i ∈ { x ∧ y } 3 2 × 2 2 i ) \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} = = 2 x ⊗ 2 y i ∈ { x ⊕ y } ⨂ 2 2 i ⊗ i ∈ { x ∧ y } ⨂ 2 3 × 2 2 i i ∈ { x ⊕ y } ∏ 2 2 i ⊗ i ∈ { x ∧ y } ⨂ 2 3 × 2 2 i
对于前者可以直接算,后者可以递归计算。
因为递归只会遍历两个所有二进制位,所以单次复杂度为 O ( log x log y ) O(\log x\log 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 1 ll<< (x| y);
if (~ mp[x][y]) return mp[x][y];
int res= 1 ;
for (int i= 0 ,j= (x^ y);(1 ll<< i)<= j;i++ ) if ((j>> i)& 1 ll ) res<<= (1 ll<< i);
for (int i= 0 ,j= (x& y);(1 ll<< i)<= j;i++ ) if ((j>> i)& 1 ll ) res= f (res,3 ll<< ((1 ll<< 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 ;(1 ll<< i)<= x;i++ ) if ((x>> i)& 1 ll )
for (int j= 0 ;(1 ll<< j)<= y;j++ ) if ((y>> j)& 1 ll ) res^= g (i,j);
return res;
}
题目
在一个二维平面中,有 n n n 个灯亮着并告诉你坐标,每回合需要找到一个矩形,这个矩形 ( x , y ) (x,y) ( x , y ) 坐标最大的那个角落的点必须是亮着的灯,然后我们把四个角落的灯状态反转,不能操作为败。
多测,T ≤ 100 , n ≤ 1000 , x i , y i ≤ 10000 T \le 100,n\le 1000,x_i,y_i \le 10000 T ≤ 100 , n ≤ 1000 , x i , y i ≤ 10000 。
裸的二维 Nim 游戏,O ( T n log x log y ) O(Tn \log x \log y) O ( T n 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 1 ll<< (x| y);
if (~ mp[x][y]) return mp[x][y];
int res= 1 ;
for (int i= 0 ,j= (x^ y);(1 ll<< i)<= j;i++ ) if ((j>> i)& 1 ll ) res<<= (1 ll<< i);
for (int i= 0 ,j= (x& y);(1 ll<< i)<= j;i++ ) if ((j>> i)& 1 ll ) res= f (res,3 ll<< ((1 ll<< 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 ;(1 ll<< i)<= x;i++ ) if ((x>> i)& 1 ll )
for (int j= 0 ;(1 ll<< j)<= y;j++ ) if ((y>> j)& 1 ll ) 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 ;
}
给定一个长度为 n n n 的序列 a a a ,q q q 次操作。
1 l r:对于任意 i ∈ [ l , r ] , a i = a i ⊗ a i i \in [l,r],a_i = a_i \otimes a_i i ∈ [ l , r ] , a i = a i ⊗ a i 。
2 l r:求 ⨁ i = l r a i \bigoplus_{i=l}^r a_i ⨁ i = l r a i 。
3 l r:求 ∑ i = l r a i \sum_{i=l}^r a_i ∑ i = l r a i 。
1 ≤ n ≤ 2.5 × 10 5 , 0 ≤ a i ≤ 2 32 , 1 ≤ q ≤ 10 5 1 \le n \le 2.5 \times 10^5,0\le a_i \le 2^{32},1\le q \le 10^5 1 ≤ n ≤ 2.5 × 1 0 5 , 0 ≤ a i ≤ 2 32 , 1 ≤ q ≤ 1 0 5 。
前方为待施工区域,下次再来吧…