Vera and Sorting
Time limit2sMemory limit256 MB
Count permutations of size N for which a recursive quicksort-like routine performs exactly K comparison steps, modulo 1e9+7.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Combinatorics, Recursion, Math
- Solved
- No attempts yet
Problem
Vera invented a new sorting algorithm. She wrote the Python function below to count how many steps her algorithm takes.
def steps(array):
if len(array) == 0:
return 0
pivot = array[0]
count = 0
lesser = []
greater = []
for element in array:
count += 1
if element < pivot:
lesser.append(element)
elif element > pivot:
greater.append(element)
return count + steps(lesser) + steps(greater)
A permutation of size is an ordered sequence of integers whose entries are distinct positive integers, each of them at most .
You are given integers and . Count the permutations of size for which returns . The count can be large, so print it modulo .
Input
The first line contains two integers and separated by a space.
Output
Print the number of such permutations modulo on a single line.
Note
For and the two valid permutations are and .