2
$\begingroup$

Given $a, d, n, x$.

Suggest me a suitable algorithm to compute the number of times the digit $x$ appearing in the arithmetic sequence $a, a + d, a + 2 \times d, \cdots, a + n \times d$.

For example : If $ a= 10, d=5,n= 2,x = 0$ the sequence is ($10, 15,20$) so the answer is $2$

Domain specification: $- 1 \le a, n \le 10^9. \text{ and } - 1 \le d \le 20,000. $

1 Answers 1

1

I cannot tell you how best to go about this problem, but here is the approach I would use: The number of digits that occur is the sum of the number of digits that occur in the ones place, the tens place, etc. For the ones place, treat all these operations mod 10, and this becomes easy because n cycles from 0 to 9. For other places, I would suggest finding the mth from last digit by looking at the sequence of terms (nth term mod 10^m - nth term mod 10^(m-1))/10^m.