For nearly a thousand years, Grifelda, in her shrivelled, frog-stained robes, has toiled away in her fortress most forboden, in the pursuit of a potion to grant her powers great enough to earn the awe, terror, and jealousy of every spellcaster in the land. Unfortunately for her, as it goes for all great villains crafting their magnificent doomsday devices... a hero would arrive, at the eleventh hour, to vanquish her, and banish her potion to the chasms.
Grifelda was not without her defenses, though, for at the top of her tower she stood, on the one-hundred and first floor, no less. Below her was a floor with but a single door, and below that, a floor with two doors, and below that, a floor with three... and so on, floor after floor, until the base floor of the tower, with a hundred doors.
Normally, these doors were quite reasonable and predictable in their purposes, but not today, for today was Grifelda's day! Upon the hero's arrival, she cast a great spell, which transformed each and every door into a one-way portal to some other floor, completely unbound by the laws of physics and Euclidean Geometry.
Each door looked exactly the same as every other door, and there was no way to tell to which floor it led without stepping through. She was, however, bound as all villains are bound, not to cheat the hero of his chance at victory. No matter what the hero did, no matter which path he took, she had to leave a way for him to eventually reach the top floor and confront her, lest her defense be dishonorable.
The hero arrives on the bottom floor with one hundred doors just after the spell upon them all is cast. With no distinguishing features to go by, no way to mark the enchanted doors for memory's sake, and no knowledge of the layout of the massive tower, the hero has no choice but to simply enter a single door at random, step onto the floor it opens to, and repeat ad infinitum, until he eventually reaches Grifelda.
Grifelda is, naturally, a perfect mathematician, and so the spell she casts on the doors is optimal, in the sense that it maximizes the expected amount of time it would take for the hero to reach her lair, given that he walks through doors at a constant rate. If is the expected number of doors the hero must walk through before reaching Grifelda's lair, what is the decimal digit sum of ?
Details (Clarifications):
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
This problem has a mind-bogglingly enormous solution space, in terms of the number of distinct spells that could be cast by the Witch. The challenge of even counting the number of possible spells she could cast, is, in and of itself, a difficult combinatorics problem! Fortunately, we are searching for spells that have a specific property we wish to maximize, and that turns out to be enough to make the problem tractable.
The difficulty of this problem rises from the fact that Grifelda is an honorable villain - if she weren't, the answer would trivially be ∞ , for she could just route all the doors on the bottom floor back to the bottom floor, preventing the hero from ever reaching her lair. So, if we are to maximize the expected time for the hero to reach her lair, we must first get a grasp on the space of spells she might cast, and narrow it down to the optimal one as efficiently as possible.
While the solution actually turns out to be mostly intuitive, with a small little 'gotcha' cherry on top, getting there rigorously is actually a fairly onerous process. I'll present the TL;DR version in the next paragraph with the answer, with the following paragraphs rigorously proving the assertions in the first one.
TL;DR Version
Any optimal spell has the following property:
Choose any ordering { t n } n = 1 1 0 1 of the floors in the tower, with t 1 0 1 equal to Grifelda's lair, subject to the constraint D ( t n , t 1 0 1 ) ≥ D ( t n + 1 , t 1 0 1 ) for all n < 1 0 1 , where D is the shortest-path distance function in the graphical sense (where t n are nodes, and doors are directed edges).
It must be the case that, if d n is the number of doors on floor t n , then t n must have ( d n − 1 ) doors that point directly to t 1 , and just one door that points to t n + 1 , except for t 1 0 1 , which has no doors.
Now, for a given { t n } , let E ( t n ) be the expected number of doors to be opened by the hero before he reaches t 1 0 1 , given that he starts on floor t n . Subject to the constraint that d k = 1 0 0 for some fixed k , E ( t k ) is maximized when d a < d b for all 1 ≤ a < b ≤ 1 0 0 , a , b = k . It can be computed that E ( t k ) is maximal when k = 5 9 , and for that unique configuration, we have:
E ( t 5 9 ) = 2 5 3 6 8 6 9 5 5 5 6 0 1 2 7 2 9 7 4 1 5 2 7 0 7 4 8 2 1 2 2 8 0 2 2 0 4 4 5 1 4 7 5 7 8 5 6 6 2 9 8 1 4 2 2 3 2 7 7 5 1 8 5 9 8 7 4 4 9 2 5 3 9 0 8 3 8 6 4 3 9 8 4 8 9 8 5 4 1 6 8 1 9 7 2 5 5 2 4 7 0 0 7 0 4 3 4 6 7 6 7 4 4 3 4 2 0 4 9 4 6 6 9 3 9 4 3 6 2 6 2 7 0 2 7 8 0 9 8 7 2 5 9 1 3 8 2 9 9 6 3 7 7 4 5 4 2 1 9
E ( t 5 9 ) ≈ 2 . 5 3 6 8 6 9 5 5 5 6 0 1 2 7 3 ⋅ 1 0 1 5 8
And the digit sum of E ( t 5 9 ) is 7 5 5 .
Long Version
Let's first reduce the problem to mathematical terms so that we can rigorously reason about it. Let the floors in the tower be { F k } k = 1 1 0 1 , where floor F k has precisely k doors, with the exception of Grifelda's lair, F 1 0 1 , which has no doors. Let the spell on the doors be arbitrary to start, and let E ( F k ) be the expected number of doors the hero must open to get to Grifelda's lair, given that he is currently on floor F k .
Lemma 1: All floors must be accessible to the hero, and Grifelda's lair should be accessible from all floors.
Let k such that Grifelda's lair is not ultimately accessible from F k . F k must then not be accessible to the hero from his initial location on F 1 0 0 , otherwise, upon entering, he would never be able to reach Grifelda's lair, which violates the villain's honor code.
Now take F k , and set all of it's doors to point to Grifelda's lair. Take all doors that previously pointed to Grifelda's lair, and point them at F k instead. This has the effect of making all doors that opened directly to Grifelda's lair in the previous graph now count as 2 doors instead of one, which increases E ( F k ) by one for all accessible F k in the previous graph. So the previous graph could not have been optimal. Therefore, any optimal graph must render all floors accessible to the hero, and so Grifelda's lair must be accessible from all floors.
Lemma 2: Every floor should have exactly one door to its topological successor, and all remaining doors should point to the topological root of the tower.
For an arbitrary spell, we can construct an ordering { t m } m = 1 1 0 1 , consisting of all floors in { F k } , starting with F 1 0 1 , and continuing backwards in breadth-first order of the reverse graph of the doors (that is, the directed graph of floors connected by doors, with each door edge reversed), such that every t m , for m < 1 0 1 , has at least one door opening to a floor t k , with k > m . For floors in the same tier, we can sort them descending with respect to the number of doors in each floor, which renders { t m } unique for any spell.
Thus, every spell which meets the conditions we have proved thus far necessarily has a unique topological order, and so it follows, that if we can prove that the optimum spell across all spells that satisfy some arbitrary topological order fits the conditions of the stated lemma, then the optimum spell across all spells in total must also satisfy the lemma.
Now, let { t m } be an arbitrary topological ordering of the floors, and let { d m } describe the number of doors on each floor correspondingly. We want to maximize E ( t s ) , for the starting position of the hero, t s , and if we can maximize all E ( t m ) while we're at it, great! We first recognize that the value E ( t m ) , for any m , is representable as the convergent, infinite sum across all finite paths P starting at t m , of the probability of such a path being taken, times the length of that path in number of doors.
Now, let D be the set of doors that point to t 1 0 1 , with ∣ D ∣ > 1 . Consider E ( t m ) , represented as the infinite sum we just described, for some arbitrary m . Then, select some arbitrary d ∈ D , redirect it to some other floor which has ultimate access to at least one x ∈ ( D − { d } ) (to preserve Lemma 1's results), and compare this new E ( t m ) with the previous one. All paths originally terminating at some x ∈ ( D − d ) remain as they are, all paths that terminated at d are removed.
For each such path P removed, a swath of new paths are added, which are prefixed by P , and terminating at some new x ∈ ( D − d ) . For each P removed, the set of new paths added all occur with probabilities that sum to the probability of P occurring originally, and since each new path is longer than the original P , the sum of all these new addends necessarily at least matches the original addend for P , and so the new E ( t m ) is ≥ the old E ( t m ) . Since this process can be applied repeatedly so long as ∣ D ∣ > 1 , it follows that the optimal spell must have ∣ D ∣ = 1 , and so only have one door that points to t 1 0 1 . It must be the case that t 1 0 0 houses this door.
Now, assume that, for some k , 2 ≤ k ≤ 1 0 0 , it is the case that all t m , k ≤ m ≤ 1 0 0 , have but one door pointing directly to t m + 1 , and that there are no other doors pointing to any t m for k < m ≤ 1 0 0 . Let D be the set of doors that point to t k , and suppose ∣ D ∣ > 1 .
For any E m , it holds that any finite path P terminating at t 1 0 1 must, in fact, either be itself a suffix sequence of the doors connecting t m to t m + 1 for k ≤ m ≤ 1 0 0 , or, the path P must be representable as a finite sequence of paths that terminate at some x ∈ D , eventually terminated by the full sequence of doors that connect those t m to t m + 1 .
Select some arbitrary d ∈ D , and redirect that door to some other floor which has ultimate access to t k . As with the preceding argument, the change between E ( t m ) for all m before the change, and after the change, is that all paths P which contained d have their addends removed from the sum, and new paths, which contain the redirected d , are added. We will show this change strictly increases E ( t m ) . Let P q x denote the set of all finite paths which start at floor q , and terminate at floor k with a pass through door x , having touched no other door in D .
Let P q final denote the set of paths starting at floor q which never touch any x ∈ D , and thus terminate in < 1 0 1 steps at t 1 0 1 . P r ( p ) denotes the probability of path p being taken exactly, given the hero at the start of it, and ∣ p ∣ denotes the length of the path, in doors. Then we have the recursive definition:
S ( q , 0 , L ) = ∑ p ∈ P q final ( P r ( p ) ⋅ ( ∣ p ∣ + L ) )
S ( q , r , L ) = ∑ x ∈ D ∑ p ∈ P q x ( P r ( p ) ⋅ S ( k , r − 1 , ∣ p ∣ + L ) )
And the summation definition:
E ( t m ) = ∑ r = 0 ∞ S ( m , r , 0 )
Our first argument proved that S ( q , 1 , L ) strictly increases in value when k = 1 0 0 , for any q , and by trivial arithmetic, and L when d is removed from D . Because S ( k , 0 , L ) remains constant between the two spells regardless of L or k , it follows that S ( q , 1 , L ) increases for all possible q , k , and L , and so, inductively onwards, S ( q , i , L ) is strictly increased for the new spell. Since these values sum together to completely define E ( t m ) , E ( t m ) must also be increased for all m by the redirected door.
The preceding paragraph allows us to apply mathematical induction to claim that the optimal spell, for our given topological order, has exactly one door from each t m to t m + 1 for all 1 ≤ m ≤ 1 0 0 , and so by process of elimination, all other doors must point back to t 1 . Given the arguments stated in the first paragraph for Lemma 2, this is sufficient to prove that the optimal spell in general must have this property for its given topological order, and so we prove Lemma 2.
Lemma 3: The optimal spell has a topology that is ascending in terms of the number of doors for all floors, excepting the floor with 100 doors.
First, we'll note that Lemma 2 actually provides us a way to compute E ( t m ) for all m in a simple, recursive expression. Recall that d m is equal to the number of doors on floor t m . Thus, for all m , we have the linear equation:
E ( t m ) = 1 + d m 1 E ( t m + 1 ) + d m d m − 1 E ( t 1 )
Starting from t 1 , working our way up, and then plugging our way back down, we'll find that a pattern emerges. Specifically:
E ( t m ) = ∑ i = 1 1 0 0 ( ∏ j = i 1 0 0 d j ) − ∑ i = 1 m − 1 ( ∏ j = i m − 1 d j )
Grouped another way:
E ( t m ) = ∑ i = m 1 0 0 ( ∏ j = i 1 0 0 d j ) + ∑ i = 1 m − 1 ( ∏ j = i 1 0 0 d j − ∏ j = i m − 1 d j )
The second form is quite useful. Suppose, in this sequence, there exist a and b such that a > b , but d a < d b . Observe that if we swap the values d a and d b , the value of E ( t m ) is guaranteed to increase. This is because, in all summands in the two summations, d b is present as a factor if and only if d a is present, and in the cases where a and b are on opposite sides of m , swapping the values decreases the magnitude of the negative addend in the second summand, thus increasing the value of the summand.
Therefore, while in some terms, swapping d a and d b has no effect, in the rest of them, the addend increases in value. So making d m sorted ascending does in fact optimize all E ( t m ) individually.
However, we must be careful here, for while E ( t 1 0 0 ) is indeed maximal when the sequence { d n } is fully sorted, this does not necessarily produce the best possible E ( t k ) for all k where it could be that d k = 1 0 0 . In fact, it can be shown that E ( t m ) > E ( t m + 1 ) for all m on a given spell! We can only conclude that the optimal spell has d k = 1 0 0 for some k , and for all other n = k , the sequence { d n = k } is sorted ascending. This concludes Lemma 3.
With Lemmas 1, 2, and 3 under our belt, we are ready to put down the pencil and paper and go to the keyboard instead, as we are, unfortunately, not quite done yet. We still need to figure out which k in the constraint d k = 1 0 0 yields the maximal E ( t k ) . Using the formula from Lemma 3, we can compute the maximal E ( t k ) for all possible k , and we'll find that k = 5 9 yields the maximum result. As noted earlier:
E ( t 5 9 ) = 2 5 3 6 8 6 9 5 5 5 6 0 1 2 7 2 9 7 4 1 5 2 7 0 7 4 8 2 1 2 2 8 0 2 2 0 4 4 5 1 4 7 5 7 8 5 6 6 2 9 8 1 4 2 2 3 2 7 7 5 1 8 5 9 8 7 4 4 9 2 5 3 9 0 8 3 8 6 4 3 9 8 4 8 9 8 5 4 1 6 8 1 9 7 2 5 5 2 4 7 0 0 7 0 4 3 4 6 7 6 7 4 4 3 4 2 0 4 9 4 6 6 9 3 9 4 3 6 2 6 2 7 0 2 7 8 0 9 8 7 2 5 9 1 3 8 2 9 9 6 3 7 7 4 5 4 2 1 9
E ( t 5 9 ) ≈ 2 . 5 3 6 8 6 9 5 5 5 6 0 1 2 7 3 ⋅ 1 0 1 5 8
And the digit sum of E ( t 5 9 ) is 7 5 5 .
To put things in perspective, if the hero is able to open a new door every Planck time , and we assume that he is able to proceed in doing so from the big bang, until the projected heat death of the universe in 1 0 1 0 0 years, the hero would need, on average, 4 3 million lifetimes of the universe to reach Grifelda's lair. So maybe this defense isn't so honorable after all.
APPENDIX
After all the analysis, the amount of code needed to get the answer is quite minimal. In it, I used an alternative, recursive representation of E ( t k ) to compute it.