#P8577. Substring Digit Sum
Substring Digit Sum
Substring Digit Sum
Given a string constructed by concatenating, in order, 1 copy of 1
, 2 copies of 2
, 3 copies of 3
, 4 copies of 4
, 5 copies of 5
, 6 copies of 6
, 7 copies of 7
, 8 copies of 8
, 9 copies of 9
, 10 copies of 10
, and so on, your task is to compute the sum of the digits from the l-th to the r-th position (1-indexed) in the resulting string.
For example, the beginning of the string is:
1 22 333 4444 55555 ...
If we list the digits: 1,2,2,3,3,3,4,4,4,4,... then for input l = 1 and r = 5, the substring is "12233" and its digit sum is \(1+2+2+3+3=11\).
Note: When k reaches 10 or above, the number is multi-digit; however, the construction rule still applies: append k copies of the string representation of k. For example, for k=10, append "10" 10 times.
inputFormat
The input consists of two space-separated integers: l and r, where 1 ≤ l ≤ r, representing the positions in the string (1-indexed).
outputFormat
Output a single integer which is the sum of the digits in the substring from the l-th to the r-th position.
sample
1 5
11