Number Assignment
Time limit0.1sMemory limit2048 MB
Count the ways to assign distinct numbers 1 to 13 to cells a through m so that each row and column total matches the given values A through H.
- Level
Medium5 of 10
- Topics
- Backtracking, Math, Brute force
- Solved
- No attempts yet
Problem

As shown in the figure above, a grid of has 25 cells in total. For convenience, the top-left cell has coordinates and the bottom-right cell has coordinates . Initially, , , , and are filled with . Every cell in row 1 and every cell in column 1 is already filled with an integer. These are through . You must fill the remaining empty cells with distinct numbers from to . In other words, through are distinct numbers from to . The following rules must hold among these numbers.

In other words, except for , each number in the first row equals the sum of the numbers in its column, and each number in the first column equals the sum of the numbers in its row.
For example, suppose through are given as in the figure on the right. If through are assigned as shown in the figure, the rules above hold. However, if through are , there is no way to assign through that satisfies the rules.
Input
The input is given through standard input. The integers are given in order on one line. Each integer is between and , inclusive.
Output
Print to standard output the number of ways to assign distinct integers from to to through so that the rules are satisfied, on one line.