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, then 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.

30:00

Divisor Game
easy
Free written solution
Topics
Companies

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, then 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: {"n":2}
Output: true
Constraints:
  • 1≤n≤10001 \leq n \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":2}

Alice and Bob take turns. On a turn with the number k you pick a divisor x of k with 0 < x < k and replace k with k - x. Whoever cannot move loses. Who wins from n = 2?

dp[k]: does the player to move from k win?

solved through 1

1

LOSE

2

?

■ solving

■ where the move lands

Variables
VariableValue
n2
DepthFunction Call
Stack empty
0/4