#P5853. [USACO19DEC] Tree Depth P

    ID: 4832 Type: RemoteJudge 1000ms 125MiB Tried: 0 Accepted: 0 Difficulty: 6 Uploaded By: Tags>数学2019USACO组合数学生成函数,GF

[USACO19DEC] Tree Depth P

题目背景

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={a1,a2,,aN}a=\{a_1,a_2,\ldots,a_N\} of the integers 1N1\ldots N, where N300N\le 300. He then runs the following pseudocode with arguments 11 and N.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}\{3,2,5,1,4\} generates the following BST:

    4
   / \
  2   5
 / \ 
1   3

Let di(a)d_i(a) denote the depth of node ii in the tree corresponding to a,a, meaning the number of nodes on the path from aia_i to the root. In the above example, d4(a)=1,d2(a)=d5(a)=2,d_4(a)=1, d_2(a)=d_5(a)=2, and d1(a)=d3(a)=3.d_1(a)=d_3(a)=3.

The number of inversions of aa is equal to the number of pairs of integers (i,j)(i,j) such that 1i<jN1\le i<j\le N and ai>aj.a_i>a_j. The cows know that the aa that FJ will use to generate the BST has exactly KK inversions (0KN(N1)2)(0\le K\le \frac{N(N-1)}{2}). Over all aa satisfying this condition, compute the remainder when adi(a)\sum_ad_i(a) is divided by MM for each 1iN.1\le i\le N.

题目描述

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

为了生成这个二叉搜索树,Farmer John 从一个 1N1 \dots N 的排列 a={1,2,,N}a= \{1,2, \dots ,N\} 开始,然后以参数 llrr 开始运行如下的伪代码:

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}\{ 3,2,5,1,4 \} 将产生如下的二叉搜索树:

di(a)d_i(a) 表示节点 ii 在用排列 aa 生成的二叉搜索树中的深度。深度定义为这个节点到根节点的路径上的点数。在上述例子中,d4(a)=1,d2(a)=d5(a)=2,d1(a)=d3(a)=3d_4(a)=1,d_2(a)=d_5(a)=2,d_1(a)=d_3(a)=3

aa 中的逆序对数等于满足 1i<jN1 \le i<j \le Nai>aja_i>a_j 的数对 (i,j)(i,j) 的个数。奶牛们知道 Farmer John 用来生成二叉搜索树的排列 aa 中恰好有 KK 个逆序对。对于所有满足条件的 aa,请计算对于每个 1iN1 \le i \le Nadi(a)\sum_a d_i(a)MM 取模后的结果。

输入格式

输入只有一行,包含三个整数 N,K,MN,K,M

输出格式

输出一行 NN 个整数,第 ii 个整数表示 adi(a)modM\sum_a d_i(a) \bmod M。两个整数之间用一个空格隔开。

3 0 192603497

1 2 3

3 1 144408983

3 4 4

提示

样例解释 1

对于这个样例,唯一满足条件的排列为 a={1,2,3}a=\{1,2,3\}

样例解释 2

对于这个样例,满足条件的两个排列分别为 a={1,3,2}a=\{1,3,2\}a={2,1,3}a=\{2,1,3\}

数据范围

对于全部数据,1N3001\le N\le 3000KN(N1)20\le K\le \frac{N(N-1)}{2},保证 MM 是一个 [108,109+9]\left[ 10^8,10^9+9 \right] 范围中的质数。

对于测试点 3,43,4,满足 N8N \le 8

对于测试点 575-7,满足 N20N \le 20

对于测试点 8108-10,满足 N50N \le 50

USACO 2019 December 铂金组T3