#P1831. Lever Numbers

    ID: 15114 Type: Default 1000ms 256MiB

Lever Numbers

Lever Numbers

A positive integer is called a lever number if there exists a digit acting as a pivot such that the sum of the torques (moments) from the digits on the left equals the sum of the torques from the digits on the right. Formally, let the number have digits \(d_0d_1\dots d_{n-1}\) (in usual order). For a pivot position \(i\) (where \(0 \le i < n\)), define:

[ \text{Left Moment} = \sum_{j=0}^{i-1} d_j \times (i - j), \quad \text{Right Moment} = \sum_{j=i+1}^{n-1} d_j \times (j - i). ]

If for at least one index \(i\) the equality

[ \sum_{j=0}^{i-1} d_j \times (i - j) = \sum_{j=i+1}^{n-1} d_j \times (j - i) ]

holds, the number is called a lever number. For example, consider the number (4139). Taking the digit (3) as the pivot (i.e. (i=2)), we have:

[ 4 \times 2 + 1 \times 1 = 9 \times 1 \quad \text{or} \quad 8+1 = 9. ]

Given an interval \([x, y]\), your task is to count how many lever numbers are in this range. Note that single-digit numbers are trivially lever numbers as both the left and right moments are 0.

inputFormat

The input consists of two space-separated integers \(x\) and \(y\) \( (0 \le x \le y)\), representing the inclusive range \([x,y]\).

outputFormat

Output a single integer representing the count of lever numbers in the interval \([x,y]\).

sample

1 50
14