Cryptarithm Multiplication
Множення
Statement
Write сripto.* that reconstructs a long multiplication of two natural
numbers from a "worked" column-multiplication layout in which the
multiplication and addition signs have been removed and most digits have
been replaced with *. The partial product coming from multiplying the
first factor by a 0 digit of the second factor (if any) is omitted
entirely, since it contributes only zeros.
Input / Output
File сripto.dat holds the obscured column-multiplication layout, one row
per line, each line at most 60 characters, with spaces appearing only before
the encoded numbers. File сripto.res must hold the two decimal factors,
separated by a space. It is guaranteed the layout has at most one valid
solution; if none exists, output No solution!.
Constraints
- each line of the layout is at most 60 characters
- the layout has at most one valid solution
Example
A layout encoding "135 × 979" decodes to:
135 979
Solution idea
The layout's line lengths and the positions of its visible digits and asterisks fix the digit-count of each factor and of every partial product. Treat each unknown digit position as a variable, and propagate constraints from the visible digits and from column-wise addition (with carries) of the partial products, backtracking over the remaining unknown positions. The search space stays small because every quantity's digit count is bounded by the 60-character line width, so a depth-first search with constraint propagation finds the unique consistent assignment (or proves none exists).