The Next Number (Large)
Time limit5sMemory limit512 MB
Given N, find the next integer whose count of each nonzero digit matches that of N, with zeros unrestricted.
- Level
Medium6 of 10
- Topics
- Combinatorics, Greedy, Math, Implementation
- Solved
- No attempts yet
Problem
For each digit from 1 to 9, let be the number of times occurs in the decimal representation of . You write out, in ascending order, every positive integer whose decimal representation contains the digit exactly times for every from 1 to 9. The number of times the digit 0 occurs is not restricted. No number has a leading zero.
For example, if you are writing every number with two 1s and one 5, your list starts 115, 151, 511, 1015, 1051.
You are given , the last number you wrote. 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 a single integer .
Limits
Output
For each test case, print one line in the following format.
Case #X: K
is the test case number starting from 1, and is the number that follows in the list.