Restore Calculation
Time limit8sMemory limit512 MB
Count ways to fill each ? in equal-length strings A, B, and C with digits, leading digits nonzero, so A plus B equals C, modulo 1,000,000,007.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Math
- Solved
- No attempts yet
Problem
The Animal School is a primary school for animal children. You are a fox attending this school.
One day the rabbit teacher, Hanako, hands you a problem called "Arithmetical Restorations". An arithmetical restoration looks like this.
- You are given three positive integers , and .
- Several digits in these numbers have been erased.
- You assign a digit to each blank position so that holds.
- The first digit of each number must not be zero. The same applies to a single-digit number.
You are good at mathematics, so you solved the problem right away. Then you thought of a harder one: count how many assignments a given arithmetical restoration admits. Solving that will earn you a good grade.
Shortly after starting the new task you noticed that there may be far too many assignments to enumerate by hand. Since you are also the best programmer in the school, you are now writing a program that counts the assignments.
Input
The input is a sequence of datasets. The number of datasets is less than 100. Each dataset has the following format.
A
B
C
A dataset consists of the three strings , and , meaning that the sum of and must be . Each string consists of digits (0-9) and question marks (?). A question mark marks an erased digit. The first character of each string is not 0, and every dataset contains at least one question mark.
Each string has between 1 and 50 characters, and the three strings of a dataset have the same length.
The end of the input is a line holding a single zero.
Output
For each dataset, print the number of possible assignments modulo 1,000,000,007 on its own line. Ms. Hanako is a careless rabbit, so some datasets have no assignment at all.
Hint
The first dataset of the example has 2 assignments.
- 384 + 122 = 506
- 394 + 122 = 516