Nim积总不能一直打四次暴力吧!

用SG定理等东西,可以证明 \((N, \oplus, \otimes)\) 构成一个域。(证明很难,我不会)

其中 \(\oplus\) 为异或, \(x \otimes y = \mathop{\textrm{mex}}_{1 \leq i < x, 1 \leq j < y} \left\{ (i \otimes y) \oplus (x \otimes j) \oplus (i \otimes j)\right\}\),即暴力对子状态计算。

然后还有优美的性质,能使计算 \(\otimes\) 做到 \(O(\textrm{poly} (\log))\):

对于一个费马数 \(M = 2^{2^a}\),对于一个 \(y\),有以下两个性质:

  1. \(M \otimes y = M \times y ~~~ (M > y)\)
  2. \(M \otimes M = M \oplus \frac{M}{2}\)

有俩 \(\log\) 的做法广为流传。但是因为不太常用,我人也懒,所以搞了一个不知是三个还是两个 \(\log\) 的东西(反正贼好写):

int normalnimproduct(int x, int y) ;
int nimproduct(int x, int y) {
if (x == 1) return y;
if (x < y) return normalnimproduct(y, x);
int M = 1 << (1 << (int) std::log2((int) std::log2(x)));
int d1 = nimproduct(x / M, y / M);
int d2 = nimproduct(x / M, y % M);
return (M * (d1 ^ d2)) ^ nimproduct(M >> 1, d1);
}
int normalnimproduct(int x, int y) {
int res = 0;
for (; x; x &= x - 1) res ^= nimproduct(x & -x, y);
return res;
}

其中 nimproduct 的 \(x\) 是二的幂。貌似存幂会跑得快一点。

根据 09论文 里,nimproduct 正确性证明如下:

我们保证 \(x \geq y\),记 \(x = PM, y = SM + T\),其中 \(M\) 是最大的满足 \(M < x\) 且为费马数的数。

显然有 \(P, S, T < M\)。然后有:

\[\begin{align*}
& (P \times M) \otimes (S \times M + T) \\
= & (P \otimes M) \otimes ((S \otimes M) \oplus T) \\
= & (M \otimes M \otimes P \otimes S) \oplus (M \otimes P \otimes T) \\
= & ((M \oplus \frac{M}{2}) \otimes P \otimes S) \oplus (M \otimes P \otimes T) \\
= & (M \otimes ((P \otimes S) \oplus (P \otimes T))) \oplus (\frac{M}{2} \otimes (P \otimes S)) \\
= & (M \times ((P \otimes S) \oplus (P \otimes T))) \oplus (\frac{M}{2} \otimes (P \otimes S))
\end{align*}
\]

记 \(d1 = P \otimes S, d2 = P \otimes T\),就有

\[x \otimes y = (M \times (d1 \oplus d2)) \oplus (\frac{M}{2} \otimes d1)
\]

normalnimproduct 的正确性因为结合律显然。

然后这样写,就偷懒成功了(好记好写)。

实际上论文里另一个函数推法类似。反正如果考试遇到连预处理一个范围以内这种方法都跑不过的话,那就慢慢推常见做法吧。

最新文章

  1. jquery 的 sort 函数
  2. cookie&amp;session&amp;servletContext
  3. Vue.js实例练习
  4. 压力测试工具siege的用法
  5. IBM Rational-完整的软件工程解决方案工具集
  6. c#转码解码
  7. android studio 程序错误
  8. LeetCode【112. 路径总和】
  9. Delphi中Chrome Chromium、Cef3学习笔记(六)
  10. Dependency injection in .NET Core的最佳实践
  11. 各种jar包
  12. [转帖]Windows DHCPServer远程代码执行漏洞分析(CVE-2019-0626)
  13. Git强制拉取覆盖本地
  14. NO.1食品超市经营管理的数据方案
  15. golang学习笔记6 beego项目路由设置
  16. Java NIO.2 —— 文件或目录删除操作
  17. python接口自动化 -参数关联(一)
  18. form表单重置、清空方法记录
  19. 面试题27:单链表向右旋转k个节点
  20. FWT背板笔记

热门文章

  1. 怎样修改 VS Code 主题?
  2. java八大排序代码
  3. SIP 3pcc
  4. 进程?线程?多线程?同步?异步?守护线程?非守护线程(用户线程)?线程的几种状态?多线程中的方法join()?
  5. Mac命令行提示
  6. TypeScript入门四:TypeScript的类(class)
  7. 微信小程序wxs如何使用
  8. 说说css hack,说真的,我也是才去了解这个东西
  9. redis和memcacahe、mongoDB的区别
  10. 对于写Python学习笔记的看法