Alice and Bob take turns playing a game, with Alice starting first.
Initially, there is a number N on the chalkboard. On each player's turn, that player makes a move consisting of:
- Choosing any
xwith0 < x < NandN % x == 0. - Replacing the number
Non the chalkboard withN - x.
Also, if a player cannot make a move, they lose the game.
Return True if and only if Alice wins the game, assuming both players play optimally.
Example 1:
Input: 2 Output: true Explanation: Alice chooses 1, and Bob has no more moves.
Example 2:
Input: 3 Output: false Explanation: Alice chooses 1, Bob chooses 1, and Alice has no more moves.
Note:
1 <= N <= 1000
题目链接:https://leetcode.com/problems/divisor-game/
题目分析:手推几个可以发现偶数是先手必胜,奇数是先手必败,下面证明
奇数的约数肯定是奇数(显然),那么面对奇数,一次操作完后必然变成偶数,先手如果是偶数,只需要每次都把这个数字变成奇数(减1即可),这样下去开始的后手最终必然会将数字变成2
class Solution {
public boolean divisorGame(int N) {
return N % 2 == 0;
}
}

博客围绕除数博弈游戏展开,Alice和Bob轮流操作,从黑板上的数字开始,选择符合条件的数替换原数字,无法操作则输。分析得出偶数先手必胜、奇数先手必败的结论,并给出证明,还给出了题目链接。
285

被折叠的 条评论
为什么被折叠?



