DMPG '16 B4 - The Gambler's Legacy
View as PDFBob is now so rich that he can sustain a respectable quality of life from his competition winnings (aka not rich at all). As Bob was resting between his competitions his mind wandered off to doing other things … with all of his money! Suddenly, he remembers his grandfather showing him the ins and outs of the stock market and decides to try "playing" for himself.
After some illegal insider trading business time passes, Bob has observed enough stock market fluctuations per second to draw up the function  as seen below, where 
 is the current value of the stock, and 
 is the next value it will take.
| — | |||
|---|---|---|---|
| … | |||
Note:  denotes 
, defined as the largest integer not greater than 
 itself.
In other words, the next value  is the sum of every digit of the number 
 raised to the power of the number of digits in 
.
There are only two possible outcomes for an investment in the  stock market: equilibrium and instability. Equilibrium is achieved when 
, that is the current value of the stock market will remain unchanged until the end of time. Instability means the value will continue to cycle through values in succession indefinitely.
Given some of Bob's initial investments, determine the amount of time it would take to reach equilibrium, or state the length of the cycle through which the value of the stock will loop.
Input Specification
The first line of the input contains a single integer  denoting the number of test cases to follow 
.
The next  lines each contain a single integer 
, the amount of money Bob invests in the stock market 
.
It is guaranteed that Bob's money, and all amounts the  may be worth until the end of time will fit into a signed 
-bit integer.
Output Specification
Your program should output  lines, with each line containing one of two strings:
If  is successful in reaching equilibrium, output 
Equilibrium: Bob's investment becomes $D after E second(s)!, where  is the value of the stock at equilibrium and 
 is the time elapsed to reach this value.
Otherwise, output Instability: Loop of length L encountered after M second(s)!, where  is the length of the cycle and 
 is the time elapsed to reach the beginning of the cycle for the first time.
Sample Input
3
288
730
876
Sample Output
Equilibrium: Bob's investment becomes $370 after 6 second(s)!
Equilibrium: Bob's investment becomes $370 after 1 second(s)!
Instability: Loop of length 2 encountered after 14 second(s)!
Explanation
The first case is really an extension of the second case, as shown after applying the function  times: 
The second case is reduced from  to 
 as shown from the bottom two rows of the chart above.
The third case eventually ends at a cycle of length two:  (cycle) 
Comments