Multi-base happiness (small)
Time limit5sMemory limit512 MB
Find the smallest integer above 1 that reaches 1 under the digit-square-sum process in every base of each test case.
- Level
Medium4 of 10
- Topics
- Simulation, Hash map, Math
- Solved
- No attempts yet
Problem
Replace an integer by the sum of the squares of its digits. A happy number is a number that reaches 1 when you repeat this process. Starting from 82:
8*8 + 2*2 = 64 + 4 = 68, repeat:
6*6 + 8*8 = 36 + 64 = 100, repeat:
1*1 + 0*0 + 0*0 = 1 + 0 + 0 = 1 (happy!)
The process reached 1, so 82 is a happy number.
Being happy in base means writing the number in base , adding the squares of those digits, and repeating until 1 appears. The same number can be happy in one base and not happy in another. The decimal number 82 is written 10001 in base 3, and in base 3 it is not happy.
Some of the bases got together (yes, they are organized) and hired you for a job: find the smallest integer greater than 1 that is happy in all of the given bases.
Input
The first line contains the number of test cases . Each of the next lines holds one test case: a space separated list of distinct bases in increasing order.
Limits
- each line lists 2 or 3 bases
- every base in the input satisfies
Output
For each test case, print one line:
Case #X: K
where is the test case number starting from 1, and is the decimal form of the smallest integer greater than 1 that is happy in all of the given bases.