Carrying Conundrum
Contest: Codeforces Round 742 (Div. 2)
Difficulty rating: 1600
Tags: bitmasks, combinatorics, dp, math

Alice has just learned addition. However, she hasn't learned the concept of "carrying" fully — instead of carrying to the next column, she carries to the column two columns to the left.

For example, the regular way to evaluate the sum $$$2039 + 2976$$$ would be as shown:

However, Alice evaluates it as shown:

In particular, this is what she does:

- add $$$9$$$ and $$$6$$$ to make $$$15$$$, and carry the $$$1$$$ to the column two columns to the left, i. e. to the column "$$$0$$$ $$$9$$$";
- add $$$3$$$ and $$$7$$$ to make $$$10$$$ and carry the $$$1$$$ to the column two columns to the left, i. e. to the column "$$$2$$$ $$$2$$$";
- add $$$1$$$, $$$0$$$, and $$$9$$$ to make $$$10$$$ and carry the $$$1$$$ to the column two columns to the left, i. e. to the column above the plus sign;
- add $$$1$$$, $$$2$$$ and $$$2$$$ to make $$$5$$$;
- add $$$1$$$ to make $$$1$$$.

Alice comes up to Bob and says that she has added two numbers to get a result of $$$n$$$. However, Bob knows that Alice adds in her own way. Help Bob find the number of ordered pairs of positive integers such that when Alice adds them, she will get a result of $$$n$$$. Note that pairs $$$(a, b)$$$ and $$$(b, a)$$$ are considered different if $$$a \ne b$$$.

Input format:
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The description of the test cases follows.

The only line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 10^9$$$) — the number Alice shows Bob.

Output format:
For each test case, output one integer — the number of ordered pairs of positive integers such that when Alice adds them, she will get a result of $$$n$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first test case, when Alice evaluates any of the sums $$$1 + 9$$$, $$$2 + 8$$$, $$$3 + 7$$$, $$$4 + 6$$$, $$$5 + 5$$$, $$$6 + 4$$$, $$$7 + 3$$$, $$$8 + 2$$$, or $$$9 + 1$$$, she will get a result of $$$100$$$. The picture below shows how Alice evaluates $$$6 + 4$$$:

Example input:
5
100
12
8
2021
10000

Example output:
9
4
7
44
99

Editorial:
Note that in every other column, the addition Alice performs is correct. Therefore, we can take our number, split it into alternating digits, and then find the answer.
For example, consider $$$n = 12345$$$. We split it into alternating digits: $$$1\underline{2}3\underline{4}5 \to 135, 24$$$. Now the problem is equivalent to find the number of pairs of numbers which add to $$$135$$$ multiplied by the number of pairs of numbers which add to $$$24$$$. It's clear each pair works: for example, $$$45 + 90 = 135$$$ and $$$\underline{9} + \underline{15} = \underline{24}$$$, so $$$4\underline{9}5 + \underline{1}9\underline{5}0$$$ will be equal to $$$12345$$$ according to Alice.
Now, how many ways are there to find a pair of non-negative integers whose sum is $$$n$$$? There are clearly $$$n+1$$$ ways: $$$n+0,(n-1)+1,(n-2)+2,\dots,0+n$$$.
Therefore, suppose we split $$$n$$$ into two numbers $$$a$$$ and $$$b$$$. Then the answer will be $$$(a+1)(b+1)$$$, but we should subtract $$$2$$$ because those correspond to either the first or second number in the sum being $$$0$$$. As a result, the answer is $$$(a+1)(b+1)-2$$$.
Time complexity: $$$\mathcal{O}(\log_{10}n) = \mathcal{O}(\log n)$$$.