QOJ.ac

QOJ

حد الوقت: 2 s حد الذاكرة: 256 MB مجموع النقاط: 100

#1911. 树的深度

الإحصائيات

为了迎接新年,Farmer John 决定给他的奶牛们一棵节日的二叉搜索树(BST)!

为了生成这棵 BST,FJ 从整数 $1 \ldots N$ 的一个排列 $a=\{a_1, a_2, \ldots, a_N\}$ 开始,其中 $N \le 300$。然后,他使用参数 $1$ 和 $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;

例如,排列 $\{3,2,5,1,4\}$ 生成如下 BST:

    4
   / \
  2   5
 / \ 
1   3

令 $d_i(a)$ 表示节点 $i$ 在排列 $a$ 对应的树中的深度,即从 $a_i$ 到根节点的路径上的节点数。在上述例子中,$d_4(a)=1, d_2(a)=d_5(a)=2, d_1(a)=d_3(a)=3$。

排列 $a$ 的逆序对数量等于满足 $1 \le i < j \le N$ 且 $a_i > a_j$ 的整数对 $(i,j)$ 的数量。奶牛们知道 FJ 用来生成 BST 的排列 $a$ 恰好有 $K$ 个逆序对 $(0 \le K \le \frac{N(N-1)}{2})$。对于所有满足此条件的排列 $a$,请计算对于每个 $1 \le i \le N$,$\sum_a d_i(a)$ 除以 $M$ 的余数。

输入格式

输入仅一行,包含三个空格分隔的整数 $N, K$ 和 $M$。$M$ 是一个在 $[10^8, 10^9+9]$ 范围内的质数。

输出格式

输出 $N$ 个空格分隔的整数,表示对于每个 $1 \le i \le N$,$\sum_a d_i(a) \pmod{M}$ 的值。

子任务

  • 测试点 3-4 满足 $N \le 8$。
  • 测试点 5-7 满足 $N \le 20$。
  • 测试点 8-10 满足 $N \le 50$。

样例

样例输入 1

3 0 192603497

样例输出 1

1 2 3

此处唯一的排列是 $a=\{1,2,3\}$。

样例输入 2

3 1 144408983

样例输出 2

3 4 4

此处两个排列分别为 $a=\{1,3,2\}$ 和 $a=\{2,1,3\}$。

题目来源:Yinzhan Xu

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.