Files
2020-08-07 17:06:53 +08:00

50 lines
2.7 KiB
Markdown
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [1025. Divisor Game](https://leetcode.com/problems/divisor-game/)
## 题目
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` with `0 < x < N` and `N % x == 0`.
- Replacing the number `N` on the chalkboard with `N - 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. `1 <= N <= 1000`
## 题目大意
爱丽丝和鲍勃一起玩游戏他们轮流行动。爱丽丝先手开局。最初黑板上有一个数字 N 。在每个玩家的回合玩家需要执行以下操作
- 选出任一 x满足 0 < x < N  N % x == 0 
- N - x 替换黑板上的数字 N
如果玩家无法执行这些操作就会输掉游戏只有在爱丽丝在游戏中取得胜利时才返回 True否则返回 false假设两个玩家都以最佳状态参与游戏
## 解题思路
- 两人相互玩一个游戏游戏初始有一个数 N开始游戏的时候任一方选择一个数 x满足 `0 < x < N` 并且 `N % x == 0` 的条件然后 `N-x` 为下一轮开始的数此轮结束该另外一个人继续选择数字两人相互轮流选择直到某一方再也没法选择数字的时候输掉游戏问如果你先手开始游戏给出 N 的时候能否直到这局你是否会必胜或者必输
- 这一题当 `N = 1` 的时候那一轮的人必输因为没法找到一个数字能满足 `0 < x < N` 并且 `N % x == 0` 的条件了必胜策略就是把对方逼至 `N = 1` 的情况题目中假设了对手也是一个很有头脑的人初始如果 `N 为偶数`我就选择 x = 1对手拿到的数字就是奇数。只要最终能让对手拿到奇数他就会输。初始如果 `N 为奇数`N = 1 的时候直接输了N 为其他奇数的时候我们也只能选择一个奇数 x(因为 `N % x == 0` N 为奇数x 一定不会是偶数因为偶数就能被 2 整除了)对手由于是一个很有头脑的人当我们选完 N - x 是偶数的时候他就选择 1那么轮到我们拿到的数字又是奇数对手只要一直保证我们拿到奇数最终肯定会逼着我们拿到 1最终他就会获得胜利所以经过分析可得初始数字如果是偶数有必胜策略如果初始数字是奇数有必输的策略