Can I Win

原题:

In the "100 game," two players take turns adding, to a running total, any integer from 1..10. The player who first causes the running total to reach or exceed 100 wins.

What if we change the game so that players cannot re-use integers?

For example, two players might take turns drawing from a common pool of numbers of 1..15 without replacement until they reach a total >= 100.

Given an integer maxChoosableInteger and another integer desiredTotal, determine if the first player to move can force a win, assuming both players play optimally.

You can always assume that maxChoosableInteger will not be larger than 20 and desiredTotal will not be larger than 300.

Example

Input:
maxChoosableInteger = 10
desiredTotal = 11

Output:
false

Explanation:
No matter which integer the first player choose, the first player will lose.
The first player can choose an integer from 1 up to 10.
If the first player choose 1, the second player can only choose integers from 2 up to 10.
The second player will win by choosing 10 and get a total = 11, which is >= desiredTotal.
Same with other integers chosen by the first player, the second player will always win.
可以用记忆化搜索也就是动态规划。

有没有偏数学的方法,如果max+1是奇数那么可以按照Leetcode 上nim game的方法一行写完,直接

return (desiredTotal%(maxChoosableInteger+1) != 0;
如果max+1是偶数,假设max+1 = 2*a;

(1) desiredTotal 是max+1的倍数,即k(2*a)(k为整数),第一个然想赢第一次取数字x1,并且x1不是a,那么第二个人总可以拿max+1-x1,然后第一个人依次取x2、...xi并且xi不为a,第二个人总可以取到max+1-xi,这样首先取数字的最终会输,所以第一个人总是要在某一次取数字时取a,第几次取a不影响结论,因为都可以通过交换次序将某次取a换作第一次取a。然后问题转到第二个取数字的人:desiredTotal不是max+1的倍数并且第一次不能取a该怎么获胜?!

综上:desiredTotal 是max+1的倍数时首先取数字的人应该取a。


(2) desiredTotal不是max+1的倍数,

假设第一个取数字的人依次取数字x1、x2...xn,第二个人依次取数字y1、y2...ym。

假设desiredTotal - (x1+y1)也不是max+1的倍数,那么问题又转到desiredTotal不是max+1的倍数时第一个人该怎么办

假设desiredTotal - (x1+y1)是max+1的倍数,那么问题转为(1),但是对于问题(1)而言只知道第一个人应该取数字a,能不能赢并不知道,所以现在的问题是对于第一个人来说要不要想办法使得desiredTotal最终变为max+1的倍数。

也就是说对于问题1和问题2都没有办法立马解答能不能获胜,仅能确定问题1时应该采取怎样的策略。

数学上几种状态相互交叉最终想要收敛到其中一个状态应该怎么办?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值