Given S, count numbers A such that A - reverseA = S

15 Views Asked by At

Given integer $S$ up to $10^9$ count all numbers $A$ such that $A - A' = S \text{ and } A' < A$ and $A'$ is reversed form of $A$ (e.x. $A = 18, A' = 81$).

For example if $S = 9$ the answer is $8$, those numbers are $21, 32, 43, 54, 65, 76, 87, 98$.

One thing I noticed is that the answer is always $0$ if the numbers $S$ is not divisible by $9$. Another thing I know but I don't know how to use it to get a solution is that if we write the digits of $A: x_1, x_2, \dots, x_n, \text{ then } A': x_n, x_{n-1}, ... , x_1$ and $A = 10^nx_1 + 10^{n-1}x_2 \dots, A' = 10^nx_n+10^{n-1}x_{n-1}\dots$.

Now $S = 10^n(x_1 - x_n) + 10^{n-1}(x_2 - x_{n-1}) \dots, S: s_1, s_2, \dots $.

$x_1 - x_n = s_1\\x_2 - x_{n-1} = s_2 \dots$

If we fix first half of digits of $x$ then we get the second half, however is this fast enough and what is its exact complexity, another thing that is not very clear to me is how big can the number $A$ big for $S$ up to $10^9$.