Kvant Math Problem 1002
The door opens as soon as some block of three consecutive pressed digits coincides with the code.
Verified: no
Verdicts: SKIP + SKIP
Solve time: 1m42s
Source on kvant.digital
Problem
- A scatterbrained mathematician, having forgotten the three-digit code to his apartment building entrance, presses buttons (with digits 0, 1, $\ldots$, 9) at a rate of one per second. The door will open if the three digits of the code are entered consecutively in the correct order. The mathematician is sure that even in the case of “extreme bad luck” (if the required combination is encountered last), he will be able to enter the building no later than 16 min 42 s (1002 s). Is he correct? How should he proceed in order to get home in the shortest possible time?
Answer the last question if
- only the buttons with digits 1, 2, 3 work (the code contains no other digits);
- all buttons work, but the mathematician remembers that all three digits of the code are distinct.
Moscow Mathematical Olympiad
Exploration
The door opens as soon as some block of three consecutive pressed digits coincides with the code. Thus the mathematician must produce a finite digit string with the property that every possible code appears somewhere as a consecutive block of length $3$.
For all $10$ digits there are $10^3=1000$ possible codes. A string of length $L$ contains at most $L-2$ consecutive blocks of length $3$. Hence any universal string must satisfy
$$L-2\ge 1000,$$
so $L\ge 1002$.
This lower bound is exactly the time limit mentioned in the statement. Equality would require that every length-$3$ block occurring in the string be different, and that all $1000$ possible codes occur.
This is the classical problem of constructing a sequence in which every word of length $3$ over a given alphabet occurs exactly once. Such sequences are de Bruijn sequences.
For the first variant, only digits $1,2,3$ may occur. There are $3^3=27$ codes. The same counting argument gives $L\ge 29$. If a de Bruijn sequence over ${1,2,3}$ of order $3$ exists, then $29$ presses suffice.
For the second variant, all digits work but the code has distinct digits. The number of possible codes is
$$10\cdot 9\cdot 8=720.$$
A naive lower bound gives $L\ge 722$. The difficulty is that a string of length $722$ would have exactly $720$ length-$3$ blocks, so every one of those blocks would have to contain three distinct digits. Is such a sequence possible?
Represent each ordered pair of distinct digits $(a,b)$ as a vertex. There are $10\cdot 9=90$ vertices. A code $(a,b,c)$ with distinct digits corresponds to the directed edge
$$(a,b)\to (b,c).$$
Each vertex has indegree and outdegree $8$. Hence the graph is Eulerian. An Euler circuit uses all $720$ edges once, yielding a string of length $720+2=722$. This achieves the lower bound.
The crucial point is verifying that the graph is connected, so that an Euler circuit exists.
Problem Understanding
We must determine whether the mathematician's claim about the worst possible waiting time of $1002$ seconds is correct and, more generally, find an optimal strategy.
A sequence of button presses is optimal if, regardless of the unknown code, the first occurrence of that code is guaranteed as early as possible in the worst case.
This is a Type C problem. We must determine the minimum worst-case number of button presses and exhibit a strategy attaining it.
For the full set of ten digits, there are $1000$ possible codes. Since a string of length $L$ contains only $L-2$ blocks of length $3$, at least $1002$ presses are necessary. A de Bruijn sequence shows that $1002$ presses are sufficient.
For the alphabet ${1,2,3}$, the same idea gives the optimum $29$ presses.
For distinct-digit codes, there are only $720$ admissible codes. The natural lower bound becomes $722$ presses, and an Eulerian construction shows that this bound is attainable.
Proof Architecture
Lemma 1. A string of length $L$ contains exactly $L-2$ consecutive blocks of length $3$; therefore, if $N$ different codes are possible, any strategy requires at least $N+2$ presses in the worst case.
The proof is a counting argument.
Lemma 2. For an alphabet of size $m$, there exists a cyclic sequence in which every length-$3$ word over that alphabet appears exactly once.
This follows from the existence of an Euler circuit in the de Bruijn graph whose vertices are length-$2$ words.
Lemma 3. Converting such a cyclic sequence into a linear string by appending its first two symbols yields a string of length $m^3+2$ containing every length-$3$ word exactly once.
This produces an optimal strategy.
Lemma 4. For distinct-digit codes, the directed graph whose vertices are ordered pairs $(a,b)$ with $a\ne b$, and whose edges are triples $(a,b,c)$ with distinct digits, is Eulerian.
Every vertex has equal indegree and outdegree, and the graph is strongly connected.
The hardest point is proving the connectivity required in Lemma 4.
Solution
Let a strategy be represented by a finite string of pressed digits. If the string has length $L$, then its consecutive blocks of length $3$ are
$$(d_1,d_2,d_3),\ (d_2,d_3,d_4),\ \ldots,\ (d_{L-2},d_{L-1},d_L),$$
so there are exactly $L-2$ such blocks.
Suppose $N$ different codes are possible. If the strategy guarantees opening the door for every code, every possible code must occur among those $L-2$ blocks. Hence
$$L-2\ge N,$$
or
$$L\ge N+2.$$
This lower bound applies to every strategy.
For the original problem, $N=10^3=1000$. Consequently every strategy requires at least
$$1000+2=1002$$
presses in the worst case.
To show that $1002$ presses suffice, consider the de Bruijn graph of order $3$ on the ten digits. Its vertices are all ordered pairs of digits. From vertex $(a,b)$ to vertex $(b,c)$ there is an edge corresponding to the triple $(a,b,c)$.
Each vertex has indegree $10$ and outdegree $10$. The graph is connected, hence Eulerian. An Euler circuit traverses each of the $10^3$ edges exactly once. Reading the successive digits along such a circuit gives a cyclic sequence in which every three-digit code appears exactly once. Appending the first two digits to the end produces a linear string of length
$$10^3+2=1002$$
containing every three-digit code exactly once.
Thus $1002$ presses are both necessary and sufficient. The mathematician is correct.
Now consider the two special cases.
For part 1, only digits $1,2,3$ may occur.
There are
$$N=3^3=27$$
possible codes. The lower bound gives
$$L\ge 27+2=29.$$
Applying the same de Bruijn construction to the alphabet ${1,2,3}$ yields a cyclic sequence containing each of the $27$ length-$3$ words exactly once. After appending the first two digits, we obtain a linear string of length $29$.
Hence the optimal worst-case time is $29$ seconds.
For part 2, all digits are available, but the three digits of the code are distinct.
The number of possible codes equals
$$N=10\cdot 9\cdot 8=720.$$
Therefore every strategy satisfies
$$L\ge 720+2=722.$$
It remains to attain this bound.
Construct a directed graph whose vertices are the ordered pairs $(a,b)$ with $a\ne b$. There are
$$10\cdot 9=90$$
vertices.
For every ordered triple $(a,b,c)$ of distinct digits, draw an edge
$$(a,b)\to(b,c).$$
The edges are in one-to-one correspondence with admissible codes, so there are $720$ edges.
Fix a vertex $(a,b)$. To leave it, the third digit may be any digit different from both $a$ and $b$, giving $8$ outgoing edges. To enter it, the preceding digit may be any digit different from both $a$ and $b$, giving $8$ incoming edges. Thus every vertex has equal indegree and outdegree.
We prove strong connectivity. Let $(a,b)$ and $(c,d)$ be arbitrary vertices.
If $b\ne c$, then
$$(a,b)\to(b,c)$$
is an edge. If also $b,c,d$ are pairwise distinct, then
$$(b,c)\to(c,d)$$
is an edge, yielding a path of length $2$.
If $d=b$, choose a digit $x$ distinct from $a,b,c$; such a digit exists because there are ten digits. Then
$$(a,b)\to(b,c)\to(c,x)\to(x,b)$$
is a path.
If $b=c$, choose a digit $x$ distinct from $a,b,d$. Then
$$(a,b)\to(b,x)\to(x,d)\to(d,\cdot)$$
and, if necessary, one more step reaches $(c,d)=(b,d)$. Since at least ten digits are available, suitable choices always exist.
Hence every vertex can be reached from every other vertex. The graph is strongly connected.
The graph is therefore Eulerian. An Euler circuit traverses all $720$ edges exactly once. Reading the corresponding digit sequence and appending the first two digits yields a linear string of length
$$720+2=722,$$
in which every admissible code with distinct digits appears exactly once.
Thus $722$ presses suffice, and by the lower bound no smaller number can suffice.
The optimal worst-case numbers of presses are
$$\boxed{1002}$$
for arbitrary three-digit codes,
$$\boxed{29}$$
when only digits $1,2,3$ are possible,
and
$$\boxed{722}$$
when the three digits are known to be distinct.
Equality is attained by following a de Bruijn sequence in the first two situations and by following an Eulerian sequence of distinct-digit triples in the third.
Verification of Key Steps
The first delicate point is the lower bound. A string of length $L$ contains exactly $L-2$ length-$3$ windows. If there are $N$ admissible codes and the door must open for every one of them, each admissible code must appear among those windows. Consequently $L-2\ge N$. Forgetting that overlapping windows are allowed can lead to an incorrect larger bound; forgetting that there are only $L-2$ windows can lead to an incorrect smaller bound.
The second delicate point is the passage from an Euler circuit to a button-press sequence. An Euler circuit through a graph whose edges correspond to triples uses every admissible triple exactly once. Writing the successive digits along the circuit produces a cyclic word. Appending the first two digits converts the cycle into a linear string and increases the length by exactly $2$, giving $N+2$ presses.
The third delicate point is connectivity in the distinct-digit graph. Equality of indegree and outdegree alone does not guarantee an Euler circuit. One must verify that all vertices belong to a single strongly connected component. The availability of ten digits is crucial because it always allows choosing an auxiliary digit different from the few digits already prescribed.
Alternative Approaches
Instead of invoking de Bruijn sequences directly, one may formulate the problem entirely in graph-theoretic language. For arbitrary codes, vertices are ordered pairs of digits and edges are three-digit codes. The optimal strategy is obtained by traversing an Euler circuit of the resulting graph. The counting lower bound and the Euler construction then fit together immediately.
For the distinct-digit version, one may regard the graph as the subgraph obtained by deleting all edges corresponding to repeated digits from the ordinary de Bruijn graph. The proof then reduces to showing that the remaining graph is still Eulerian. This viewpoint emphasizes why the optimum drops from $1002$ to $722$: the number of admissible edges decreases from $1000$ to $720$, while the Eulerian structure survives.