eolymp
bolt
Try our new interface for solving problems
Məsələlər

Sort by sum of digits

Sort by sum of digits

The sum of a number’s digits associated with some of its properties. Particularly, by the sum of digits it is easy determine divisibility by \textbf{3} and \textbf{9}. Computer scientists of Rybinsk have decided to do research of natural numbers and their sums of digits. It is well known, that it’s easier to work with the sorted data than with unsorted. Therefore, first of all researchers decided to sort natural numbers by the sum of their digits. If some numbers have equal sums of digits, then these numbers are written in ascending order. When scientists sorted the numbers from \textbf{1} to \textbf{20}, they have got a series: \textbf{1}, \textbf{10}, \textbf{2}, \textbf{11}, \textbf{20}, \textbf{3}, \textbf{12}, \textbf{4}, \textbf{13}, \textbf{5}, \textbf{14}, \textbf{6}, \textbf{15}, \textbf{7}, \textbf{16}, \textbf{8}, \textbf{17}, \textbf{9}, \textbf{18}, \textbf{19}. For the small quantity of numbers sorting were executed quickly. When they moved research in a wide range some difficulties were appeared to overcome. One of it was to find out the place of number \textbf{K} in the sorted series of \textbf{N} natural numbers. Your task is to solve the problem. Write a program to calculate the position of \textbf{K} in the sorted series of the first \textbf{N} natural numbers. \InputFile The single line of the input file contains two natural numbers separated by space: \textbf{N} and \textbf{K} in their decimal notation (\textbf{1}≤ \textbf{K} ≤ \textbf{N} ≤ \textbf{10^12}). \OutputFile One natural number -- position of \textbf{K}.
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB
Giriş verilənləri #1
20 13
Çıxış verilənləri #1
9