#P1611. Cyclic Number Pairs

    ID: 14897 Type: Default 1000ms 256MiB

Cyclic Number Pairs

Cyclic Number Pairs

Have you ever grown tired of watching the same thing repeat over and over again on TV? While I'm not too concerned about TV shows, sometimes numbers can be just as cyclic!

We say that two different positive integers \(n\) and \(m\) are cyclic if and only if you can take several digits from the end of \(n\) and attach them to the front (keeping their order) to form \(m\). For example, \((12345, 34512)\) is a pair of cyclic numbers because by moving the ending \(345\) of \(12345\) to the beginning, you obtain \(34512\). Note that to be cyclic, \(n\) and \(m\) must have the same number of digits, and neither \(n\) nor \(m\) can have leading zeros.

Now, given two positive integers \(A\) and \(B\) (which have the same number of digits and no leading zeros), count the number of cyclic pairs \((n, m)\) such that \(A \leq n < m \leq B\). All pairs must have distinct numbers.

Note: Any formula is rendered in \(\LaTeX\) format. For instance, the cyclic condition is described by:

[ \text{There exists an index } i \ (1 \leq i \leq d-1) \text{ such that if } n = s[0\ldots d-1] \text{ then } m = s[i\ldots d-1]s[0\ldots i-1] ]

inputFormat

The input consists of a single line containing two positive integers \(A\) and \(B\), separated by a space. It is guaranteed that \(A\) and \(B\) have the same number of digits and no leading zeros.

Example:

100 500

outputFormat

Output a single integer representing the number of cyclic pairs \((n, m)\) such that \(A \leq n < m \leq B\).

Example:

156

sample

100 500
156