Beautiful Numbers (Large)
Time limit5sMemory limit512 MB
For each N up to 1e18, find the base B that writes N with all digits 1, preferring the base that gives the most 1 digits.
- Level
Hard8 of 10
- Topics
- Number theory, Math, Binary search, Brute force
- Solved
- No attempts yet
Problem
Call a number beautiful if every one of its digits is 1. A number that is not beautiful in base 10 can still be made beautiful by writing it in another base.
You are given an integer . Find a base (with ) such that every digit of written in base is 1. If several bases satisfy this, choose the one that maximizes the number of 1 digits.
Input
The first line contains the number of test cases . Each of the next lines contains one integer .
Limits
Output
For each test case, print one line in the form Case #x: y, where is the test case number starting from 1 and is the base described above.
Hint
For , the answer is 2, because 3 is written as 11 in base 2.
For , the answer is 3, because 13 is written as 111 in base 3. You could also write 13 as 11 in base 12, but that representation has fewer 1 digits.