This page is still under construction.

Parts of this page are still being built. What you see may change.

Young diagrams and Young tableaux

Time limit3sMemory limit128 MB

Summary
Count the fillings of the given Young diagram with numbers 1 to N that rise weakly across rows and strictly down columns.
Level

Medium7 of 10

Topics
Dynamic programming, Combinatorics, Backtracking
Solved
No attempts yet

Problem

A Young diagram is an arrangement of boxes that satisfies the following conditions.

  • The boxes are contiguous in every row and in every column.
  • Every row is aligned to the leftmost column.
  • No row is longer than the row directly above it.

An arrangement whose row lengths from top to bottom are 3, 2, 2, 1 is a Young diagram.

[][][]
[][]
[][]
[]

A Young tableau fills the boxes of a Young diagram with numbers under the following conditions.

  • Each box holds an integer from 1 to NN, both ends allowed.
  • The integer in a box is greater than or equal to the integer in the box directly to its left.
  • The integer in a box is greater than the integer in the box directly above it.

With N=3N = 3 and row lengths 3, 2, 1, the arrangement below satisfies all three conditions.

1 1 2
2 3
3

Given NN and the shape of a Young diagram, write a program that computes the number of ways to build a Young tableau.

Input

The input holds several test cases and continues to the end of the file. Each test case takes two lines.

The first line gives the shape of the Young diagram. The leading integer kk is the number of rows, with 1≤k≤71 \le k \le 7. Then come kk integers l1,l2,…,lkl_1, l_2, \dots, l_k, the box count of each row, satisfying 7≥l1≥l2≥⋯≥lk≥17 \ge l_1 \ge l_2 \ge \dots \ge l_k \ge 1.

The second line gives NN, with k≤N≤7k \le N \le 7.

Output

For each test case, print the number of Young tableaux that can be built, one per line.

Examples1

  1. Example 1

    Input
    1 1
    1
    1 1
    2
    2 2 1
    4
    4 3 2 1 1
    4
    
    Expected output
    1
    2
    20
    20