eolymp
bolt
Try our new interface for solving problems
Problems

Recurring Decimals

Recurring Decimals

A decimal representation of an integer can be transformed to another integer by rearranging the order of digits. Let us make a sequence using this fact. A non-negative integer \textbf{a_0} and the number of digits \textbf{L} are given first. Applying the following rules, we obtain \textbf{a_i_\{+1\}} from \textbf{a_i}. \begin{enumerate} \item Express the integer \textbf{a_i} in decimal notation with \textbf{L} digits. Leading zeros are added if necessary. For example, the decimal notation with six digits of the number \textbf{2012} is \textbf{002012}. \item Rearranging the digits, find the largest possible integer and the smallest possible integer; In the example above, the largest possible integer is\textbf{ 221000} and the smallest is \textbf{000122 = 122}. \item A new integer \textbf{a_i_\{+1\}} is obtained by subtracting the smallest possible integer from the largest. In the example above, we obtain \textbf{220878} subtracting \textbf{122} from \textbf{221000}. \end{enumerate} When you repeat this calculation, you will get a sequence of integers \textbf{a_0}, \textbf{a_1}, \textbf{a_2}, ... . For example, starting with the integer \textbf{83268} and with the number of digits \textbf{6}, you will get the following sequence of integers \textbf{a_0}, \textbf{a_1}, \textbf{a_2}, ... . \textbf{a_0 = 083268a_1 = 886320 − 023688 = 862632a_2 = 866322 − 223668 = 642654a_3 = 665442 − 244566 = 420876a_4 = 876420 − 024678 = 851742a_5 = 875421 − 124578 = 750843a_6 = 875430 − 034578 = 840852a_7 = 885420 − 024588 = 860832a_8 = 886320 − 023688 = 862632 …} Because the number of digits to express integers is fixed, you will encounter occurrences of the same integer in the sequence \textbf{a_0}, \textbf{a_1}, \textbf{a_2}, ... eventually. Therefore you can always find a pair of \textbf{i} and \textbf{j} that satisfies the condition \textbf{a_i = a_j} (\textbf{i} > \textbf{j}). In the example above, the pair (\textbf{i = 8}, \textbf{j = 1}) satisfies the condition because \textbf{a_8 = a_1 = 862632}. Write a program that, given an initial integer \textbf{a_0} and a number of digits \textbf{L}, finds the smallest \textbf{i} that satisfies the condition \textbf{a_i = a_j} (\textbf{i} > \textbf{j}). \InputFile The input consists of multiple datasets. A dataset is a line containing two integers \textbf{a_0} and \textbf{L} separated by a space. \textbf{a_0} and \textbf{L} represent the initial integer of the sequence and the number of digits, respectively, where \textbf{1} ≤ \textbf{L} ≤ \textbf{6}and \textbf{0} ≤ \textbf{a_0} < \textbf{10^L}. The end of the input is indicated by a line containing two zeros; it is not a dataset. \OutputFile For each dataset, find the smallest number \textbf{i} that satisfies the condition \textbf{a_i = a_j} (\textbf{i} > \textbf{j}) and print a line containing three integers: \textbf{j}, \textbf{a_i} and \textbf{i − j}. Numbers should be separated by a space. Leading zeros should be suppressed. Output lines should not contain extra characters. You can assume that the above \textbf{i} is not greater than \textbf{20}.
Time limit 2 seconds
Memory limit 64 MiB
Input example #1
2012 4
83268 6
1112 4
0 1
99 2
0 0
Output example #1
3 6174 1
1 862632 7
5 6174 1
0 0 1
1 0 1
Source ACM ICPC Asia Regional Contest 2012 Tokyo