#P1179. Counting Digit Two in a Range

    ID: 13886 Type: Default 1000ms 256MiB

Counting Digit Two in a Range

Counting Digit Two in a Range

Given a range \([L, R]\), count the total number of times the digit 2 appears in all the integers within the range.

For example, for the range \([2, 22]\):

  • The integer 2 contains 1 occurrence of the digit 2.
  • The integer 12 contains 1 occurrence of the digit 2.
  • The integer 20 contains 1 occurrence of the digit 2.
  • The integer 21 contains 1 occurrence of the digit 2.
  • The integer 22 contains 2 occurrences of the digit 2.

Thus, the total count is \(1+1+1+1+2=6\).

inputFormat

The input consists of two integers \(L\) and \(R\) (\(0 \leq L \leq R \leq 10^{9}\)), separated by a space, representing the range \([L, R]\).

outputFormat

Output a single integer representing the total number of times the digit 2 appears in all the integers within the range.

sample

2 22
6