#P9536. [YsOI2023] Prüfer 序列
[YsOI2023] Prüfer 序列
Background
A counting problem for Ysuperman’s template testing.
As everyone knows, the Prüfer sequence has almost never appeared in official contests, so it needs to appear in a Luogu Monthly Contest.
Problem Description
As everyone knows, a labeled unrooted tree with nodes corresponds one-to-one with its Prüfer sequence. If you do not know what a Prüfer sequence is, you can refer to the explanation in the Hint section below.
Now you are given a positive integer sequence of length , where . Uniformly at random, choose a subsequence of this sequence with length (as long as the chosen indices are different, the two subsequences are considered different) and use it as a Prüfer sequence to construct a tree . For all , you need to compute the expected value of (where is defined as the number of edges on the simple path between nodes and ).
Output the answer modulo .
Input Format
The first line contains two positive integers .
The second line contains integers representing the sequence , and it is guaranteed that .
Output Format
Output non-negative integers. The -th integer denotes the expected value of .
4 3
2 4 3
2 666666673 1
6 4
4 6 5 2
4 1 1 3 2
10 12
6 9 2 10 1 5 5 9 10 7 8 3
585858594 60606064 8080810 834343444 638383846 785858595 913131322 595959602 286868692
Hint
Explanation of the Prüfer sequence
For a given unrooted labeled tree, the construction process of its Prüfer sequence is as follows: each time, select the leaf node with the smallest label and delete it, then record in the sequence the node it is connected to. Repeat this times; after that, only two nodes remain, and the recorded sequence is the Prüfer sequence of this unrooted labeled tree.
We can prove that an unrooted labeled tree with more than node corresponds one-to-one with a Prüfer sequence, and that any Prüfer sequence of length with each number being an integer in has a unique corresponding tree. Similarly, we can also restore a tree from a Prüfer sequence.
For more detailed content about the Prüfer sequence, you can refer to the description of the Prüfer sequence on OI-wiki.
Sample 1 explanation
There are three equally likely subsequences to choose from: , , .
- The tree corresponding to is a chain , and the distances from to are , respectively.
- The tree corresponding to is a chain , and the distances from to are , respectively.
- The tree corresponding to is a chain , and the distances from to are , respectively.
Therefore, the expected value of is , the expected value of is , and the expected value of is .
Sample 2 explanation
There is only one possible subsequence , and the corresponding tree is a chain . The distances from to are in order, which is the answer.
Constraints
This problem has a total of test points:
| Test Point ID | ||
|---|---|---|
In addition, for all testdata, it is guaranteed that .
Please note that the memory limit for the first test points is , and the memory limit for the last test point is .
Translated by ChatGPT 5