Solution. We will number the mailboxes and keys in the same way as the apartments. We need to show that sooner or later the Scholar will take out key 37 from mailbox 37.
The sequence of the numbers of the extracted keys cannot be infinite, since there are only 80 apartments. The sequence must end with the number 37. If this is not the case, and the last number is not 37, then the next mailbox will be opened, and the Scholar will take out the next key. But then the previous number will not be the last. Contradiction. Therefore, the last key is 37, which means the algorithm is effective.
To solve problem b), imagine a directed graph where the vertices represent mailboxes. If a key to mailbox b lies in mailbox a, then there is an edge (a;b) in the graph. There are no other edges in the graph. One edge enters each vertex, and one edge leaves each vertex (it may be the same if the key to the same mailbox lies in the mailbox). Thus, the degree of each vertex is 2. Consequently, this graph is a union of non-intersecting cycles. Each tenant, by means of a stick, "lands" in some vertex of this cycle and completes it by retrieving the key to their mailbox. We need to find the average number of cycles X.
Let I1,2,…,k be the indicator of the event "vertices 1,2,…,k form a cycle, and precisely in this order of vertices," that is, I1,2,…,k=1 if this is the case, and I1,2,…,k=0 if this is not the case.
If the total number of vertices-mailboxes in the graph is n, then the expected value EI1,2,…k is equal to the probability of this event and is equal to
n1⋅n−11⋅n−21⋅⋯⋅n−(k−1)1=n!(n−k)!
Briefly, let's explain why this is so. The first factor is the probability that key 2 lies in mailbox 1. The second factor is the probability that, given this, key 3 will be in mailbox 2, and so on.
The probability that any ordered set of k vertices forms a cycle is exactly the same. The total number Xk of ordered cycles of length k is the sum of the indicators of all possible ordered cycles with exactly k vertices, and there are
Cnk⋅(k−1)!=k(n−k)!n!
Therefore, EXk=k(n−k)!n!⋅n!(n−k)!=k1. In other words, on average, there is 1 cycle of length 1, half a cycle of length 2, a third of a cycle of length 3, and so on, the average number of cycles of length n is n1, and the expected value of the number of all cycles is
EX=1+21+31+…+n1
The obtained sum is called the n-th harmonic number and is denoted by Hn. For n=80, using a computer or calculator, we find that Hn≈4.965. It is known that there is an approximate equality
Hn≈lnn+γ+2n1
where γ is the Euler-Mascheroni constant, equal to the limit of the difference between the harmonic number and the natural logarithm: γ=limn→∞(Hn−lnn). In practical calculations, γ can be taken as 0.58. If we use this approximate formula, we get that the average number of tenants poking with a stick is approximately ln80+0.58+1601≈4.968.