Divisor Game
This problem is a classic example of a game theory problem in computer science, specifically in the category of combinatorial games where players alternate turns and the game state can be described as a single integer.
In this problem, the key is to identify a pattern or a rule that governs whether Alice (the first player) will win the game. If we start analyzing from the smallest values of n
, we can find a pattern:
 If
n = 1
, Alice cannot make a move, so she loses.  If
n = 2
, Alice can make a move (she can choosex = 1
), and Bob cannot make a move, so Alice wins.  If
n = 3
, Alice can only choosex = 1
, which makesn = 2
for Bob’s turn. Bob can make a move and Alice cannot, so Alice loses.  If
n = 4
, Alice can choosex = 1
(she could also choosex = 2
, but that would lead to her losing), makingn = 3
for Bob’s turn. Following the same logic as before, Alice will win in the end.  If
n = 5
, no matter what move Alice makes, Bob will end up in a winning position.
From this pattern, we can observe that Alice wins if n
is even and loses if n
is odd. The reason is, Alice can always choose x = 1
to ensure that Bob always gets an odd number, and because there’s no even divisor for an odd number, Bob can only reduce the number by 1. This way, Alice always ends up with an even number on her turn, allowing her to control the game until n = 2
, at which point she wins.
Here is the Python code to solve this problem:


This function will return True
if Alice can win the game, and False
if she can’t, given optimal play from both players.
Key Takeaway
This problem is a great example on how to analyze the small to large cases and identify the pattern, form a statement that is true and apply it to solve the problem.
It’s a common problemsolving strategy called “bottomup analysis” or “inductive reasoning”. This approach is especially useful in game theory problems or in problems where the state space is too large to check exhaustively, but small enough that you can manually solve for the first few cases.
In this case, we started with the smallest values of n
and found a pattern that held for larger values. This helped us form a hypothesis that Alice wins when n
is even and loses when n
is odd. We then reasoned through the game mechanics to confirm this hypothesis and prove that it’s correct for all n
.
This method is particularly powerful when combined with the principles of optimality, which allow us to assume that each player will make the best possible move at each step. This reduces the complexity of the game tree and allows us to make definitive conclusions about the game outcome based on the current state.
This problem also teaches a valuable lesson: Sometimes the most efficient algorithm doesn’t involve any complex data structures or algorithms, but simply identifying and applying the right pattern or rule.
Is this related to proof by induction?
Yes, this process is related to mathematical induction. Here’s a brief overview of how it applies:
In mathematical induction, we seek to prove a statement is true for all natural numbers (or all members of a certain set). This process consists of two steps:
Base Case: We prove the statement is true for the initial case, often where the variable is at its smallest or simplest. In our game theory problem, this was proving the outcomes when
n
was 1 and 2.Inductive Step: We then prove that if the statement is true for some case, it must also be true for the next case. In our problem, we demonstrated that if Alice can win a game at
n = k
(which we’ve assumed to be true), then she can also win atn = k+1
.
In our problem, we used this principle to prove that Alice wins when n
is even and loses when n
is odd. The basis for our inductive step was the reasoning that Alice can always force Bob to play with an odd number, and vice versa, thus controlling the parity of the game state until she wins.
It’s important to note that while our analysis was inductive in nature, it wasn’t a strict proof by induction. We used reasoning about the game mechanics to prove our hypothesis for all n
, rather than explicitly proving the base case and inductive step. Nonetheless, the principles of induction were crucial to our problemsolving process.
Q&A
how are we supposed to know the optimal strategy of the game? is it coming from some number theory?
For the specific problem of Alice and Bob playing a game where they each choose divisors until they can’t anymore, the optimal strategy does not come directly from number theory. It’s more a consequence of the specific rules of the game and the properties of odd and even numbers.
When it comes to finding optimal strategies in games, it often comes down to experience, intuition, and a systematic analysis of the game. For this problem, analyzing the outcomes for small values of n
reveals a pattern: Alice wins when n
is even and loses when n
is odd. This is an observation that doesn’t require deep mathematical theory.
However, understanding why this pattern occurs does involve some mathematical reasoning. In this game, the only move that can change the parity (evenness or oddness) of the number on the chalkboard is subtracting 1. Hence, if Alice starts with an even number, she can always subtract 1 to give Bob an odd number. And since all divisors of an odd number are odd, Bob can only give Alice an even number. This way, Alice can ensure that she always plays on even numbers and Bob always plays on odd numbers, which ensures that Bob will eventually receive a 1 and lose the game.
This kind of mathematical reasoning and strategy deduction is often necessary in competitive programming problems and game theory problems. While deep mathematical theory isn’t always required, a solid understanding of basic mathematical properties and problemsolving skills can be very helpful.
There are plenty of problme like this on leetcode, two players take turns to do something, and they both play optimally.
Are there any general algorithms to solve problem like this? I think it’s about game theory, but I can’t find appropriate materials to study this kind of specific problems.
Yes, problems like these fall into the realm of game theory, specifically combinatorial game theory, which deals with games of perfect information where players take turns to make moves.
There isn’t a universal algorithm that can solve all such problems because the optimal strategy often heavily depends on the specifics of the game. However, there are some common strategies that you can apply:
Minimax Algorithm: Minimax is a type of depthfirst search that is used for decision making in game theory and artificial intelligence. In a twoplayer game, one player aims to maximize the score (the maximizer) while the other aims to minimize it (the minimizer). The algorithm explores all possible states of the game, down to a certain depth, to find the move that leads to the best possible game outcome.
AlphaBeta Pruning: Alphabeta pruning is an optimization of the minimax algorithm that reduces the number of nodes that must be evaluated. It keeps track of the best scoring node found so far, and prunes branches of the game tree that cannot possibly lead to a better outcome.
Dynamic Programming (DP): Many games can be solved using dynamic programming, especially those that have overlapping subproblems. The key idea is to break down the problem into simpler subproblems and use their solutions to construct the solution for the current game state.
Memoization: This strategy involves storing the outcomes of certain game states that have been computed before. When the game reaches a state that has been seen before, the stored outcome can be reused rather than recalculated.
Nimber Theory (or SpragueGrundy Theory): For certain types of games, the SpragueGrundy theorem provides a method to determine the outcome of a game. Each position in the game is assigned a value called a “Grundy number”, and the game can often be analyzed using nimber arithmetic.
Monte Carlo Tree Search (MCTS): For larger games where it’s not possible to evaluate all possible game states, the Monte Carlo Tree Search algorithm can be used. It builds a search tree over time and uses Monte Carlo simulations to estimate the value of each move.
You might find it helpful to study these algorithms and techniques. Specific books like “Algorithmic Game Theory” edited by Nisan, Roughgarden, Tardos, and Vazirani and resources online like MIT’s OpenCourseWare (course 6.254: Game Theory with Engineering Applications) can also be beneficial. Remember, the more problems you solve, the more adept you become at recognizing which strategy to employ for a particular game.