Palindromic Numbers
Time limit2sMemory limit512 MB
Count integers x in [L, R] for which both x-1 and x+1 are palindromes, with leading zeros allowed, for up to 2000 queries with R up to 10^18.
- Level
Medium7 of 10
- Topics
- Dynamic programming, String
- Solved
- No attempts yet
Statement
Vasya loves studying interesting classes of numbers. Today he is studying palindromic numbers.
Vasya calls a number palindromic if it reads the same from left to right and from right to left. Vasya also allows adding any number of leading zeros (possibly none) to a number. For example, the numbers 22, 4554, 12321, and 5050 are palindromic. In particular, 5050 needs one leading zero to become 05050, which reads the same from both ends.
Among other things, Vasya is interested in palindromic numbers that differ by 2. To study them, Vasya considers numbers such that both and are palindromic. Vasya calls these numbers between-palindromic. Vasya wants to count the between-palindromic numbers in the segment , inclusive, for several segments.
Help Vasya with this task.
Input
The input file contains several segments that interest Vasya. The first line contains one integer (), the number of segments. Each of the next lines contains two integers and (), the bounds of the segment.
Output
Print lines. On the -th line, print one integer: the number of between-palindromic numbers in the segment from to , inclusive.
Hint
The palindromic numbers between 17 and 24 are 20 and 22. So the only between-palindromic number in the segment is 21. In the second example, 21 also fits. The palindromic numbers between 49 and 56 are 50 and 55. There are no between-palindromic numbers in the segment .