DMPG '16 B4 - The Gambler's Legacy

View as PDF

Submit solution

Points: 7 (partial)
Time limit: 1.0s
Memory limit: 64M

Author:
Problem types

Bob 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 f as seen below, where M is the current value of the stock, and f(M) is the next value it will take.

M \lfloor \log_{10}M \rfloor + 1 f(M)
2016 4 2^4 + 0^4 + 1^4 + 6^4 1313
730 3 7^3 + 3^3 + 0^3 370
370 3 3^3 + 7^3 + 0^3 370

Note: \lfloor x \rfloor denotes \operatorname{floor}(x), defined as the largest integer not greater than x itself.

In other words, the next value f(M) is the sum of every digit of the number M raised to the power of the number of digits in M.

There are only two possible outcomes for an investment in the ZYX stock market: equilibrium and instability. Equilibrium is achieved when M = f(M), 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 T denoting the number of test cases to follow (1 \le T \le 25).

The next T lines each contain a single integer M_i, the amount of money Bob invests in the stock market (1 \le M_i \le 2^{30}).

It is guaranteed that Bob's money, and all amounts the ZYX may be worth until the end of time will fit into a signed 32-bit integer.

Output Specification

Your program should output T lines, with each line containing one of two strings:

If ZYX is successful in reaching equilibrium, output Equilibrium: Bob's investment becomes $D after E second(s)!, where D is the value of the stock at equilibrium and E is the time elapsed to reach this value.

Otherwise, output Instability: Loop of length L encountered after M second(s)!, where L is the length of the cycle and M 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 6 times: 288 \to 1032 \to 98 \to 145 \to 190 \to 730 \to 370 \dots

The second case is reduced from 730 to 370 as shown from the bottom two rows of the chart above.

The third case eventually ends at a cycle of length two: \dots 66\,835 \to 51\,688 \to (cycle) 76\,438 \to 58\,618 \to 76\,438 \dots


Comments

There are no comments at the moment.