At first, let’s simplify the problem a little bit. If we know the answer for range and the answer for range, we can calculate the answer for range by subtracting from .
Now, let’s simulate the process of building the numbers. Let’s say, currently, the number is , where are the digits we have placed already and are the digits we need to place. Now, if we can compute the count of the numbers we can build by placing the digits in the positions of , we can easily calculate the contribution of in this position of the numbers having a structure like this.
To calculate this, we can apply digit Dynamic Programming approach. Definition like this is enough to define the DP state while building the numbers. Here, is a flag indicating if the current position is the first position of the number; indicates if the number we are building is smaller than ; is the current position we are placing digit and is the value we have placed in the previous position. To get the count of numbers and the amount of rhodium from a state together, we may need to manage two DP arrays. An array of pairs also suffices.
Setter’s solution: https://ideone.com/Z5KAJ8