Route Calculator Returns
Time limit2sMemory limit512 MB
Sum the values of every right-or-down grid path, where each path reads as an expression with + and * and digits, modulo M.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Math, String
- Solved
- No attempts yet
Problem
The grid has rows and columns. Each cell contains one of 11 characters: an addition operator '+', a multiplication operator '*', or a digit from 1 to 9.
A path starts at the top-left cell and ends at the bottom-right cell, moving only right or down. The value of a path is the result of the expression formed by concatenating the characters in the cells on the path, in order. Find the sum of the values of all possible paths. Since the sum can be large, print it modulo .
The top-left and bottom-right cells always contain digits. Also, at least one of any two cells that share an edge contains a digit. So every expression obtained from a path is a valid expression.
Input
The first line contains three integers , , and (, ). The next lines describe the grid. is the character in the cell at row and column . Each is '+', '*', or a digit from 1 to 9. Both and are digits. At least one of any two cells that share an edge is a digit.
Output
Print the sum of the values of all possible paths, modulo .