P1611 Cyclic Numbers

Description

Have you ever felt bored with a TV program because you kept seeing the same thing repeat over and over? Well, I do not really care about TV shows, but sometimes numbers also cycle like that. Assume two distinct positive integers $(n, m)$ are cyclic if and only if you can move some digits from the end of $n$ to the front, without changing the order of the moved digits, so that the whole number becomes $m$. For example, $(12345, 34512)$ is a cyclic pair because you can move the trailing $345$ of $12345$ in front of $12$ to get $34512$. Note that to be a cyclic pair, $n$ and $m$ must have the same number of digits. Neither $n$ nor $m$ has leading zeros. Now given positive integers $A$ and $B$, guaranteed to have the same number of digits and no leading zeros, count the number of cyclic pairs $(n, m)$ such that $A \leq n \le m \leq B$.

Input Format

This problem has 10 test points. Each input file contains 1 line. The first line contains two space-separated positive integers A and B.

Output Format

Each output file should contain a single integer x, representing the number of cyclic pairs $(n, m)$ such that $A \leq n \le m \leq B$.

Explanation/Hint

$1\le A,B \leq 2\times 10^6$. Translated by ChatGPT 5