luogu#P7802. [COCI 2015/2016 #6] SAN

[COCI 2015/2016 #6] SAN

Problem Description

Anica\text{Anica} has a mysterious infinite table with infinitely many rows and infinitely many columns. Interestingly, each number appears only a finite number of times in the table.

Define the function rev(i)\mathrm{rev}(i), which returns the new number obtained by reversing ii in decimal. For example, rev(213)=312\mathrm{rev}(213)=312, rev(406800)=008604=8604\mathrm{rev}(406800)=008604=8604.

The number A(i,j)A(i,j) in row ii and column jj of the table is defined as follows:

  • A(i,1)=iA(i,1)=i

  • $A(i, j) = A(i, j − 1)+\mathrm{rev}\big(A(i,j-1)\big)$, j>1j>1

Now Anica\text{Anica} gives QQ queries. Each query provides two integers LL and RR. Please find how many numbers in the infinite table have values within [L,R]\big[L,R\big].

Input Format

The first line contains an integer QQ.

The next QQ lines each contain two integers LL and RR.

Output Format

Output QQ lines, each containing one integer. The ii-th line should be the answer to the ii-th query.

2
1 10
5 8
18
8
3
17 144
121 121
89 98
265
25
10
1
1 1000000000
1863025563

Hint

Constraints

For 50%50\% of the testdata, 1L,R1061\le L,R\le 10^6.

For 100%100\% of the testdata, 1Q1051\le Q\le 10^5, 1L,R10101\le L,R\le 10^{10}.

Source

Translated from COCI 2015-2016 CONTEST #6 T6 SAN.

This problem uses the original COCI scoring. Full score is 160160..

Translated by ChatGPT 5