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
.
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.
Function Signature
def divisor_game(n: int) -> bool:
Rules
-
x
must be strictly less than the current number, so
x = n
is never allowed.
-
Both players play optimally: each one picks a move that wins whenever such a move exists.
-
Return
True
when Alice wins and
False
when Bob wins.
Constraints
Examples
Input: n = 2
Output: True
Alice chooses x = 1, leaving 1 on the board. Bob has no valid move and loses, so Alice wins.
Input: n = 3
Output: False
Alice must choose x = 1 (2 does not divide 3), leaving 2. Bob then chooses x = 1, leaving 1. Alice has no valid move and loses.
Input: n = 1
Output: False
No x satisfies 0 < x < 1, so Alice cannot move on her first turn and loses.