#P1497. Counting Valid Layouts

    ID: 14783 Type: Default 1000ms 256MiB

Counting Valid Layouts

Counting Valid Layouts

Zhuge Liang challenges Pang Tong by arranging kk wooden pieces (often referred to as "wooden oxen and gliding horses") on an n×nn \times n grid. To make the challenge tougher, he imposes a special rule: no two pieces may share the same row or the same column. In addition, each piece is painted with one of three colors (red, green, or blue). Two arrangements are considered different if either the positions of the pieces or their colors differ.

The total number of valid layouts is given by:

$$\text{Answer} = \binom{n}{k}^2 \times k! \times 3^k $$

where (1 \leq k \leq n \leq 10).

inputFormat

The input consists of two integers (n) and (k) (with (1 \leq k \leq n \leq 10)) separated by a space.

outputFormat

Output a single integer, which is the number of valid layouts.

sample

3 2
162