Nim Game
2015-11-26 21:08
225 查看
https://leetcode.com/discuss/63725/theorem-all-4s-shall-be-false 太机智
Proof:
the base case: when
by the hint from the problem, no matter which number that that first player, the second player would always be able to pick the remaining number.
For
the first player can reduce the initial number into 4 accordingly, which will leave the death number 4 to the second player. i.e. The numbers 5, 6, 7 are winning numbers for any player who got it first.
Now to the beginning of the next cycle,
Following the second case, for numbers between (2*4 = 8) and (3*4=12), which are
Proof:
the base case: when
n = 4, as suggested
by the hint from the problem, no matter which number that that first player, the second player would always be able to pick the remaining number.
For
1* 4 < n < 2 * 4, (n = 5, 6, 7),
the first player can reduce the initial number into 4 accordingly, which will leave the death number 4 to the second player. i.e. The numbers 5, 6, 7 are winning numbers for any player who got it first.
Now to the beginning of the next cycle,
n = 8, no matter which number that the first player picks, it would always leave the winning numbers (5, 6, 7) to the second player. Therefore, 8 % 4 == 0, again is a death number.
Following the second case, for numbers between (2*4 = 8) and (3*4=12), which are
9, 10, 11, are winning numbers for the first player again, because the first player can always reduce the number into the death number 8.
class Solution { public: bool canWinNim(int n) { return n %4 !=0; } };
相关文章推荐
- 深入理解dispatch_queue
- C++内存分配原则方法
- Cannot load 32-bit SWT libraries on 64-bit JVM
- Keil MDK V5.0 安装及注册(附软件及注册机下载地址)
- UI-Target-action模式
- linux 远程工具
- Beta版本冲刺计划及安排
- 习题3-2 分子量 解题报告
- 从头认识java-10.5 栈轨迹和重新抛出异常
- c++11使用 async异步函数并传递参数以及auto的使用方法
- 面试9
- iOS AFNetworking 数据缓存
- J2EE开发技术点4:ajax技术
- page-cache层各种标志位之间的转换
- spring MVC学习(二)---配置相关的东西
- 面试8
- 面试7
- 面试6
- node问题之FATAL ERROR: Allocation failed - process out of memory
- 浴室随想——RogueLike随想