Countdown#11845

Countdown (Editorial)

Let denote the number of segments that need to be lit to display digit and the number of times digit appears in the decimal representations of all numbers from to , inclusive. Then the total number of segments equals

\sum_{i=0}^{9} cnt[i]\cdot segment[i], \qquad segment=\{6,2,5,5,4,5,6,3,7,6\}.

So, it remains to compute . For position ((10^j)-place) define

pow=10^{j},\quad \mathrm{high}=\bigl\lfloor n/10\,pow\bigr\rfloor,\quad \mathrm{current}=\bigl\lfloor n/pow\bigr\rfloor\bmod10,\quad \mathrm{low}=n\bmod pow.

For i\neq0:

cnt[i][j]= \mathrm{high}\cdot pow+ \begin{cases} pow, & \mathrm{current}>i,\\ \mathrm{low}+1, & \mathrm{current}=i,\\ 0, & \mathrm{current}<i. \end{cases}

For i=0:

cnt[0][j]= \begin{cases} 0,&\mathrm{high}=0,\\ (\mathrm{high}-1)\cdot pow,&\mathrm{high}\ge1, \end{cases} + \begin{cases} pow,&\mathrm{current}>0,\\ \mathrm{low}+1,&\mathrm{current}=0,\\ 0,&\mathrm{current}<0. \end{cases}

Finally ( cnt[i]=_j=0^9cnt[i][j] ). Total complexity O(10\log_{10}n).