Under construction

Still being built. Things may change

Number Assignment

Time limit0.1sMemory limit2048 MB

Summary
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 5×55 \times 5 has 25 cells in total. For convenience, the top-left cell has coordinates (1,1)(1,1) and the bottom-right cell has coordinates (5,5)(5,5). Initially, (1,1)(1,1), (4,5)(4,5), (5,4)(5,4), and (5,5)(5,5) are filled with 00. Every cell in row 1 and every cell in column 1 is already filled with an integer. These are AA through HH. You must fill the remaining empty cells with distinct numbers from 11 to 1313. In other words, aa through mm are distinct numbers from 11 to 1313. The following rules must hold among these numbers.

  • A=a+e+i+lA = a + e + i + l
  • B=b+f+j+mB = b + f + j + m
  • C=c+g+kC = c + g + k
  • D=d+hD = d + h
  • E=a+b+c+dE = a + b + c + d
  • F=e+f+g+hF = e + f + g + h
  • G=i+j+kG = i + j + k
  • H=l+mH = l + m

In other words, except for (1,1)(1,1), 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 AA through HH are given as in the figure on the right. If aa through mm are assigned as shown in the figure, the rules above hold. However, if AA through HH are 3,22,27,9,16,34,27,143, 22, 27, 9, 16, 34, 27, 14, there is no way to assign aa through mm that satisfies the rules.

Input

The input is given through standard input. The integers A,B,C,D,E,F,G,HA, B, C, D, E, F, G, H are given in order on one line. Each integer is between 33 and 4646, inclusive.

Output

Print to standard output the number of ways to assign distinct integers from 11 to 1313 to aa through mm so that the rules are satisfied, on one line.

Examples2

  1. Example 1

    Input
    33 22 27 9 16 34 27 14
    
    Expected output
    126
    
  2. Example 2

    Input
    3 22 27 9 16 34 27 14
    
    Expected output
    0