eolymp
bolt
Try our new interface for solving problems
Problems

Graded lexicographic order

Graded lexicographic order

Consider the integers from \textbf{1} to \textbf{n}. Called the \textit{weight} of its sum of digits, and let the weight of \textbf{x} for \textbf{w}(\textbf{x}). Then we'll arrange the numbers in the so-called \textit{graded lexicographic order}. Let two numbers \textbf{a} and \textbf{b}. If \textbf{w}(\textbf{a}) < \textbf{w}(\textbf{b}), then the number \textbf{a} is the graded lexicographic order to the number \textbf{b}. If \textbf{w}(\textbf{a}) = \textbf{w}(\textbf{b}), if \textbf{a} number is in the graded lexicographic order to the number of \textbf{b} if and only if the decimal representation of \textbf{a} lexicographically less than the decimal representation of \textbf{b}. For example, in this order: \begin{itemize} \item number of \textbf{120} goes to number \textbf{4}; \item number of \textbf{555} goes to number \textbf{78}; \item number of \textbf{20} goes to number \textbf{200}. \end{itemize} For given \textbf{n} and \textbf{k}, find the number \textbf{k} and the number, located on the \textbf{k}-th position in the graded lexicographic ordering of natural numbers from \textbf{1} to \textbf{n}. \InputFile In the input file contains \textbf{n} and \textbf{k} (\textbf{1} ≤ \textbf{k} ≤ \textbf{n} ≤ \textbf{10^18}). \OutputFile The first line of the output file output the number of \textbf{k}. The second row output digits in the \textbf{k}-th place.
Time limit 3 seconds
Memory limit 32 MiB
Input example #1
20
10
Output example #1
2
14