Ent Numbers
Time limit1sMemory limit128 MB
Simulate a Goodstein-like sequence where each step subtracts one, then raises the base while keeping digits, and report the base where the term first hits 0 or that it exceeds 2^60.
- Level
Medium7 of 10
- Topics
- Implementation, Math, Simulation
- Solved
- No attempts yet
Problem
Ents are a race of tree-like beings in Middle-earth, the ancient shepherds of the forest. When Bilbo visits the forest of Fangorn to meet the Ents, he notices some very, very long numbers in their writings and asks Treebeard (the eldest of the Ents) about them. Treebeard explains that the numbers describe the convergence rates of certain sequences (introduced to the Ents by a mathematician named Reuben Goodstein). After a few days of explanation, Bilbo finally understands how these numbers are generated.
Consider a sequence defined by two numbers, and . The first term of the sequence is itself, written in base . To obtain the next term, we decrease the number by 1 and then, keeping the resulting digits unchanged, increase the base by 1.
For example, start with and . We group each digit in parentheses and write it as (4)(2) Base 10. The next term is (4)(1) Base 11, which equals 45 in base 10: we first subtract one (giving (4)(1) Base 10) and then raise the base by one while keeping the same digits. The term after that is (4)(0) Base 12, i.e. 48 in base 10. The table below shows how the sequence continues.
Although it looks like the sequence will keep growing, it eventually reaches zero at base (the term goes to (1) Base 191, and then to (0) Base 192). The next table shows the sequence for a different starting point. As you can see, it can grow extremely fast (at base 100 the term already reaches 1000707070428).
Treebeard explains that this sequence always reaches zero, even though it may first blow up to enormous values. Bilbo finds this hard to believe and asks for your help. Given the two starting numbers and , determine whether the sequence reaches 0 before the base reaches (about the largest number Bilbo can imagine), and if so, at which base it becomes zero.
Input
The first line contains the number of test cases (). Each of the following lines contains two integers and (the starting number and the starting base, with ), separated by a single space.
Output
For each test case, output one line. If the sequence reaches 0 before the base reaches , print
Starting with N = <N> and B = <B>, sequence converges to 0 at Base <base>.
where <base> is the base at which the term first becomes 0. Otherwise, print
Starting with N = <N> and B = <B>, sequence did not converge to 0 before 2^60.