Divisor Game - Problem
Welcome to the Divisor Game! This is a classic game theory problem where two players, Alice and Bob, compete in a strategic number game.
Game Rules:
- ๐ฎ Alice always goes first
- ๐ There's a number
nwritten on a chalkboard - โ
On each turn, a player must choose a divisor
xwhere0 < x < nandn % x == 0 - ๐ Replace
nwithn - x - โ If a player cannot make a valid move, they lose!
Your Task: Determine if Alice can win this game, assuming both players play optimally (always making the best possible move).
Example: If n = 2, Alice can choose x = 1 (since 2 % 1 == 0), leaving Bob with n = 1. Bob cannot make any valid moves since there's no divisor of 1 that's less than 1, so Alice wins!
Input & Output
example_1.py โ Basic Win
$
Input:
n = 2
โบ
Output:
true
๐ก Note:
Alice chooses x = 1 (since 2 % 1 == 0), leaving Bob with n = 1. Bob cannot make any moves since there's no valid divisor of 1, so Alice wins.
example_2.py โ Basic Loss
$
Input:
n = 3
โบ
Output:
false
๐ก Note:
Alice can only choose x = 1 (since 3 % 1 == 0), leaving Bob with n = 2. Now Bob can choose x = 1, leaving Alice with n = 1. Alice cannot make any moves, so Bob wins.
example_3.py โ Edge Case
$
Input:
n = 1
โบ
Output:
false
๐ก Note:
Alice starts with n = 1 and cannot make any valid moves (no divisor x where 0 < x < 1), so Alice loses immediately.
Visualization
Tap to expand
Understanding the Visualization
1
Even Cookies = Win
With even cookies, always take 1 cookie, leaving odd for opponent
2
Odd Cookies = Lose
With odd cookies, can only take odd amounts, leaving even for opponent
3
Alice's Strategy
If Alice starts with even cookies, she controls the game and wins
Key Takeaway
๐ฏ Key Insight: Alice wins if and only if n is even! Even numbers provide winning control, odd numbers force losing positions.
Time & Space Complexity
Time Complexity
O(1)
Single check if number is even or odd
โ Linear Growth
Space Complexity
O(1)
No additional space needed
โ Linear Space
Constraints
- 1 โค n โค 1000
- Both players play optimally
- Alice always starts first
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code