A952 | Tree Depth--Platinum
来源USACO
时间限制1s
内存限制128MB
通过 / 提交0/0
题目描述
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 $a=\\{a_1,a_2,\ldots,a_N\\}$
of the integers $1\ldots N$, where $N\le 300$. He then runs the following
pseudocode with arguments $1$ and $N.$
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 $\\{3,2,5,1,4\\}$ generates the following BST:
4
/ \
2 5
/ \
1 3
Let $d_i(a)$ denote the depth of node $i$ in the tree corresponding to $a,$
meaning the number of nodes on the path from $a_i$ to the root. In the above
example, $d_4(a)=1, d_2(a)=d_5(a)=2,$ and $d_1(a)=d_3(a)=3.$
The number of inversions of $a$ is equal to the number of pairs of integers
$(i,j)$ such that $1\le i<j\le N$ and $a_i>a_j.$ The cows know that the $a$
that FJ will use to generate the BST has exactly $K$ inversions $(0\le K\le
\frac{N(N-1)}{2})$. Over all $a$ satisfying this condition, compute the
remainder when $\sum_ad_i(a)$ is divided by $M$ for each $1\le i\le N.$
tree (BST)!
To generate the BST, FJ starts with a permutation $a=\\{a_1,a_2,\ldots,a_N\\}$
of the integers $1\ldots N$, where $N\le 300$. He then runs the following
pseudocode with arguments $1$ and $N.$
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 $\\{3,2,5,1,4\\}$ generates the following BST:
4
/ \
2 5
/ \
1 3
Let $d_i(a)$ denote the depth of node $i$ in the tree corresponding to $a,$
meaning the number of nodes on the path from $a_i$ to the root. In the above
example, $d_4(a)=1, d_2(a)=d_5(a)=2,$ and $d_1(a)=d_3(a)=3.$
The number of inversions of $a$ is equal to the number of pairs of integers
$(i,j)$ such that $1\le i<j\le N$ and $a_i>a_j.$ The cows know that the $a$
that FJ will use to generate the BST has exactly $K$ inversions $(0\le K\le
\frac{N(N-1)}{2})$. Over all $a$ satisfying this condition, compute the
remainder when $\sum_ad_i(a)$ is divided by $M$ for each $1\le i\le N.$
输入格式
The only line of input consists of three space-separated integers $N, K,$ and
$M$, followed by a new line. $M$ will be a prime number in the range
$[10^8,10^9+9].$
$M$, followed by a new line. $M$ will be a prime number in the range
$[10^8,10^9+9].$
输出格式
Print $N$ space-separated integers denoting $\sum_ad_i(a)\pmod{M}$ for each
$1\le i\le N.$
$1\le i\le N.$
输入输出样例
输入 #1
* Test cases 3-4 satisfy $N\le 8.$ * Test cases 5-7 satisfy $N\le 20.$ * Test cases 8-10 satisfy $N\le 50.$
输出 #1
3 0 192603497
1 2 3
Here, the only permutation is $a=\\{1,2,3\\}.$
Here, the only permutation is $a=\\{1,2,3\\}.$
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted