The Next Number
Time limit5sMemory limit512 MB
Given N, the count of each nonzero digit in N defines the whole list; find the next number that has exactly those digit counts.
- Level
Medium7 of 10
- Topics
- Greedy, Backtracking, Combinatorics, Implementation
- Solved
- No attempts yet
Problem
You write out a list of numbers. For each digit from 1 to 9, the list holds every positive integer whose decimal representation contains exactly copies of the digit . The number of zeros is not restricted. You write the numbers in ascending order.
For example, if you write every number with two 1s and one 5, the list starts 115, 151, 511, 1015, 1051.
You are given , the last number you wrote. The counts are the digit counts of itself. Compute the next number in the list.
Input
The first line contains an integer , 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: K
where is the test case number starting from 1 and is the number that follows in the list.