Problem Description
Community difficulty rating: 5 kyu (medium)
"Blind box match" is a game where the host opens blind boxes to reveal random gifts. There are types of gifts, and empty slots on the table, each holding at most one gift. Each round, the host opens a blind box and places the gift into an empty slot. If a gift of the same type is already on the table, then all gifts of that type are removed (including the newly placed one). The game continues until all slots are filled with gifts of distinct types. At that point, the player keeps every gift that was opened during the game (including those that were later removed). Gift types are drawn uniformly at random. The question is: what is the expected total number of gifts the player receives in one game?
Below is an example with slots and gift types: A, B, C, D.
-> Initially all slots are empty:
_ _ _
-> Host opens a blind box, gets gift A:
A _ _
-> Host opens a blind box, gets gift B:
A B _
-> Host opens a blind box, gets gift A:
A B A
-> A matches, all A's are removed:
_ B _
-> Host opens a blind box, gets gift C:
C B _
-> Host opens a blind box, gets gift D:
C B D
No empty slots remain. The player received 5 gifts.
Input: number of slots and number of gift types . Guaranteed:
Output: a floating-point number, the expected total number of gifts received.
Solutions
Linear Equations

Consider the game mechanics. Since duplicate gifts are removed as soon as they appear, the gifts on the table are always of distinct types. Therefore, we don't need to track the count of each gift type — tracking the number of distinct gift types currently on the table is sufficient. We can model this as a Markov chain, where the state is the number of distinct gifts on the table. Each time a blind box is opened, a state transition occurs. When there are gifts on the table, there is a probability of drawing an existing type, which removes the duplicates and reduces the count by 1. Conversely, there is a probability of drawing a new type, increasing the count by 1. The process stops upon reaching state , i.e., all slots are filled.
How does this relate to the total number of gifts received? Each time a gift is placed on the table, the gift count increases by 1. Therefore, the number of state transitions is exactly the total number of gifts received. In other words, what we need is the expected hitting time from state 0 to state in this Markov chain.
Define as the expected number of remaining blind boxes to open before reaching the terminal state , given that there are currently distinct gifts on the table. Our goal is to compute . The state transition equations are as follows. Each transition consumes one step, so we add 1 and multiply by the respective transition probabilities:
This looks like a recurrence suitable for dynamic programming, but state transitions go both ways: computing requires both and , which in turn depend on . This means we cannot solve it via DP recurrence. Instead, we solve the following system of linear equations:
import numpy as np
def blind_box_match(n, m):
A, b = [[1, -1] + [0]*(n-1)], [1]
for k in range(1, n):
row = [0]*(n+1)
row[k-1:k+2] = [-k/m, 1, -(m-k)/m]
A.append(row)
b.append(1)
A.append([0]*n + [1])
b.append(0)
return np.linalg.solve(A, b)[0]
Alternatively, we can use the standard formula for the expected hitting time of a Markov chain. Let be the vector of expected hitting times, be the transition submatrix after removing the absorbing state (where ), and be the all-ones column vector:
import numpy as np
def blind_box_match(n, m):
Q = np.zeros((n, n))
if n > 1:
Q[0][1] = 1
for k in range(1, n):
Q[k][k-1] = k / m
if k + 1 < n:
Q[k][k+1] = (m-k) / m
return np.linalg.solve(np.eye(n) - Q, np.ones(n))[0]
Difference of Expectations
We can define to simplify the computation. The meaning of becomes the expected time to go from state to state for the first time. The target value can then be expressed as:
From this interpretation, we can derive a recurrence for :
After taking one step from state , if we directly reach (with probability ), no extra steps are needed. With probability , however, we fall back to , in which case we must first go from to , and then from to . Rearranging:
Now depends only on . This elegant property allows us to compute all values via dynamic programming recurrence. The sum of all values gives .
def blind_box_match(n, m):
z = 1
ans = z
for k in range(1, n):
z = (m + k * z) / (m - k)
ans += z
return ans