This page is still under construction.

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

PERMS

Interview

Time limit1sMemory limit128 MB

Summary
For each query (n, k), count permutations of 1..n having exactly k inversions, with n up to 18 and k up to 200.
Level

Medium5 of 10

Topics
Dynamic programming, Combinatorics, Prefix sum, Implementation
Solved
No attempts yet

Problem

A permutation of the integers 1,2,3,…,n1, 2, 3, \ldots, n is an ordering a1,a2,a3,…,ana_1, a_2, a_3, \ldots, a_n of those nn integers. An inversion is a pair (ai,aj)(a_i, a_j) with i<ji < j and ai>aja_i > a_j — that is, a larger value appearing before a smaller one. The number of inversions in a permutation measures how "unsorted" it is, and it is often useful when analyzing the average running time of sorting algorithms.

Your task is to compute how many permutations of {1,2,…,n}\{1, 2, \ldots, n\} have exactly kk inversions.

For example, when n=3n = 3 there are 66 permutations, with inversion counts as shown below.

PermutationInversions
1230
1321 (3>23 > 2)
2131 (2>12 > 1)
2312 (2>12 > 1, 3>13 > 1)
3122 (3>13 > 1, 3>23 > 2)
3213 (3>23 > 2, 3>13 > 1, 2>12 > 1)

So among the permutations of 33 elements, 11 has 00 inversions, 22 have 11 inversion, 22 have 22 inversions, 11 has 33 inversions, and none have 44 or more.

Input

The input contains one or more queries, one per line. Each line gives two integers: nn (1≤n≤181 \le n \le 18) and a non-negative integer kk (0≤k≤2000 \le k \le 200). The input ends with a line containing n=k=0n = k = 0, which must not be processed.

Output

For each query, print on its own line the number of permutations of {1,2,…,n}\{1, 2, \ldots, n\} that have exactly kk inversions.

Examples2

  1. Example 1

    Input
    3 0
    3 1
    3 2
    3 3
    4 2
    4 10
    13 23
    18 80
    0 0
    
    Expected output
    1
    2
    2
    1
    5
    0
    46936280
    184348859235088
    
  2. Example 2

    Input
    4 0
    4 1
    4 2
    4 3
    4 4
    4 5
    4 6
    4 7
    0 0
    
    Expected output
    1
    3
    5
    6
    5
    3
    1
    0