Inversions
InterviewTime limit1sMemory limit128 MB
Count permutations of size n that have exactly k inversions, modulo 30011, using the Mahonian number recurrence with a sliding window over prefix sums.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Combinatorics, Prefix sum, Sliding window
- Solved
- No attempts yet
Problem
A sequence of distinct integers is a permutation of size when holds for every . For a permutation, an inversion is a pair of indices with .
Given and , count how many permutations of size have exactly inversions.
Input
A single line with two integers and (, ), separated by one space: the size of the permutation and the target number of inversions.
Output
Print one line with the number of permutations of size that have exactly inversions, taken modulo .
Hint
The permutations of size with inversions are and , so the answer for , is .