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
x
with0 < x < N
andN % x == 0
. - Replacing the number
N
on 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;
}
}