PERMS
InterviewTime limit1sMemory limit128 MB
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 is an ordering of those integers. An inversion is a pair with and — 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 have exactly inversions.
For example, when there are permutations, with inversion counts as shown below.
So among the permutations of elements, has inversions, have inversion, have inversions, has inversions, and none have or more.
Input
The input contains one or more queries, one per line. Each line gives two integers: () and a non-negative integer (). The input ends with a line containing , which must not be processed.
Output
For each query, print on its own line the number of permutations of that have exactly inversions.