#P5853. [USACO19DEC] Tree Depth P
[USACO19DEC] Tree Depth P
Description
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 Format
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 Format
Print space-separated integers denoting for each
3 0 192603497
1 2 3
3 1 144408983
3 4 4
Hint
Sample Explanation
For the first example,the only permutation is .
For the second example,the two permutations are and .
Data range
Test cases satisfy .
Test cases satisfy .
Test cases satisfy .
京公网安备 11011102002149号