Tree Depth
Time limit2sMemory limit512 MB
For each node i, sum its depth over all permutations of 1..N with exactly K inversions, using the Cartesian-tree BST built by recursively taking the minimum. Output each sum mod a large prime.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Combinatorics, Divide and conquer, Math
- Solved
- No attempts yet
Problem
For the new year, Farmer John decided to give his cows a festive binary search tree (BST)!
To generate the BST, FJ starts with a permutation of the integers , where . He then runs the following pseudocode with arguments and .
generate(l,r):
if l > r, return empty subtree;
x = argmin_{l <= i <= r} a_i; // index of min a_i in {a_l,...,a_r}
return a BST with x as the root,
generate(l,x-1) as the left subtree,
generate(x+1,r) as the right subtree;
For example, the permutation generates the following BST:
4
/ \
2 5
/ \
1 3
Let denote the depth of node in the tree corresponding to , meaning the number of nodes on the path from to the root. In the above example, , and .
The number of inversions of is equal to the number of pairs of integers such that and . The cows know that the that FJ will use to generate the BST has exactly inversions . Over all satisfying this condition, compute the remainder when is divided by for each .
Input
The only line of input consists of three space-separated integers , and , followed by a new line. will be a prime number in the range .
Output
Print space-separated integers denoting for each .