List every way to split S into terms of at most D digits whose concatenated digits form a path on a phone keypad using each digit at most once.
Medium7DFSBacktrackingImplementationNo attempts yetTime limit2sMemory limit512 MBStrike Boy, as his nickname suggests, is a boy obsessed with every kind of computer game. He is spending his vacation on a paradise island where computers are not allowed. He had fun for a while with the games on his cell phone, but the battery ran out and the island has no electricity, so he had to stop playing. Strike Boy then invented a new pastime that uses his phone's keypad. In this two-player game, one player chooses two integers S and D. The opponent must then find a sequence of terms such that:
The keypad layout is shown below. The 0 key is directly below 8, so the neighbors of 0 are 7, 8, and 9.
1 2 3
4 5 6
7 8 9
0
A term is written as the exact string of digits it uses. Any term may therefore start with 0 (for example 074), and so may the last term (for example, with D=2, a last term 07).
For example, if S=230 and D=3, there are only two solutions that obey the rules: [074, 156] and [085, 142, 3]. The sequence [230] is not a solution because key '3' is not a neighbor of key '0'.

Help Strike Boy check whether his opponent's answers are correct: write a program that, given S and D, prints every possible solution.
The input contains several test cases. Each test case is a single line with two integers S and D separated by one space: the desired sum and the number of digits of each term (0≤S≤10000000000, 1≤D≤10).
The end of the input is marked by a line with S=D=−1. Do not process that line.
For each test case, print one answer. The first line of an answer is the test case identifier in the format #i, where i starts at 1 and increases by one for each test case.
If a solution exists, print every possible sequence, one per line. Separate the terms of a sequence with a single space, and print each term exactly as the digits it uses, including leading zeros. If there is no solution, print a line containing only the word impossivel.
Print the sequences in increasing lexicographic order. Sequence Sa=a1a2…am precedes sequence Sb=b1b2…bn if and only if Sb is non-empty and one of the following holds:
Here two terms a1 and b1 are compared as digit strings, not as numeric values: character by character from the left, and if one string is a prefix of the other, the shorter one comes first. For example, the term 08 precedes the term 8, and the term 012 precedes the term 3. This order is the same as comparing, in the same way, the digit strings obtained by concatenating the terms of each sequence without spaces.