Combination Lock
InterviewTime limit2sMemory limit1024 MB
Given an n-digit number m in base k, find the smallest larger n-digit number in base k with the same digit sum, or report that none exists.
Problem
Vasya really likes using combination locks. A combination lock has a drum with several rings. Each ring has several positions, and each position corresponds to a digit in base . So the state of a lock with rings can be written as an -digit number in base . The lock opens in only one fixed state.
Because codes are easy to forget, Vasya uses a trick. After locking the lock, he turns it so that its number is the previous one (in increasing order) among all numbers with exactly the same digit sum as the number that opens the lock.
To open the lock, Vasya only needs to find the next number (in increasing order) with the same digit sum as the current one. That number opens the lock.
After a month of using this lock, Vasya got tired of solving this tricky problem by hand every time, so he asked you for help.
Write a program that, given a number in base , finds the next number (in increasing order) in the same base with the same digit sum.
Input
The first line contains two natural numbers and (). The second line contains the number in base . It has digits and no leading zeros. Digits 10 to 35 are written as the uppercase Latin letters A to Z. The number does not exceed .
Output
Print the answer: the required number in base with exactly digits and no leading zeros. If Vasya made a mistake and no such number exists, print the single word <<Impossible>>.