PROBLEM LINKS
DIFFICULTY
EASY
EXPLANATION
This problem can be solved using dynamic programming, however a more sophisticated solution exists. Denote C(N) as the sum of D(N) over all values from 0 to N-1 (inclusive). Our goal is to calculate C(B+1)-C(A). Note that the first 10 values of D(N) are 0, 1, 4, 3, 8, 5, 2, 7, 6, 9. Each number from 0 to 9 appears exactly once. Also note that D(10 * k + i) = (D (k)+D(i)) % 10, where 0≤i<10. It follows that each number from 0 to 9 will also appear exactly once in D(10 * k + i) as i ranges from 0 to 9 and k is fixed. Therefore, C(10 * k)=45 * k for all k. This allows us to compute C(N) quickly by using C(N) = 45 * N/10 if N is a multiple of 10, and C(N) = C(N-1) + D(N-1) if N is not a multiple of 10.
SETTER'S SOLUTION
Can be found here.
TESTER'S SOLUTION
Can be found here.