#P5965. Miscomputed Vertical Addition
Miscomputed Vertical Addition
Miscomputed Vertical Addition
When adding two decimal numbers using vertical (column) addition, people sometimes make a mistake by adding each column separately without carrying over. For instance, consider the example below:
In the picture, the correct addition of 248 + 208
should yield 456
, but due to the mistake the sum is computed column by column as follows:
- Hundreds column: 2 + 2 = 4
- Tens column: 4 + 0 = 4
- Ones column: 8 + 8 = 16
These numbers are then concatenated in order (from the most significant column to the least) to give the mistaken result 4416.
More formally, let the two numbers be represented with the same number of digits (by adding leading zeros if necessary). For each column, let the sum be computed as a number (which may have one or two digits) and then all these column sums are concatenated (without carrying over) to form the final result. Given a positive integer n, count the number of ordered pairs of non-negative integers a and b such that a + b would be miscomputed as n by the above procedure. Note that the answers (a, b)
and (b, a)
are considered distinct if a ≠ b.
The sums for each column can be any integer between 0 and 18, but note that:
- If the column sum is a single digit (0–9), then the number of digit pairs producing that sum is f(x) = x + 1.
- If it is two digits (10–18), then the number of pairs is f(x) = 19 - x.
Your task is to correctly count the total number of pairs for all valid ways to segment the erroneous result n into column sums (each segment being either 1 or 2 digits, with a segment of 2 digits representing a value between 10 and 18). Each segmentation corresponds to a multiplication of possibilities from each column.
inputFormat
The input consists of a single line containing a positive integer n — the miscomputed sum obtained by the erroneous vertical addition.
outputFormat
Output a single integer representing the number of ordered pairs (a, b) of non-negative integers such that their miscomputed vertical addition equals n.
sample
4416
2
</p>