#P11159. 【MX-X6-T5】 再生
【MX-X6-T5】 再生
Background
Original problem link: https://oier.team/problems/X6F。
このまま らったった 音に乗って 今きっと世界で僕だけだ 後ろ向きな歌を聴いて 少しだけ 前向きに生きていく
Broken points are recombined according to broken rules. What kind of structure will be “regenerated” in this way?
Problem Description
You are given a labeled rooted tree with nodes, and the array top obtained from its long-chain decomposition. Please output how many different trees can produce this top array after long-chain decomposition. The answer should be taken modulo (a prime).
More precisely, for a tree , define the height of every node :
- If is a leaf, then 。
- Otherwise, let the set of children of be , then 。
Given an array , you need to count how many trees satisfy:
- For the root node , 。
- For every non-leaf node , there exists exactly one child such that and , and every other child satisfies 。
Take the result modulo (a prime).
Two trees are different if and only if their roots are different or their edge sets are different.
It is guaranteed that the answer is not before taking modulo, but it is not guaranteed to be non-zero modulo .
Input Format
The first line contains a positive integer 。
The next line contains positive integers separated by spaces, representing the top array.
Output Format
Output one integer: the answer modulo 。
5
1 1 1 4 4
2
16
1 2 1 4 1 4 1 4 9 1 1 12 1 1 12 1
7181107
Hint
Sample Explanation #1

Only the two trees in the figure satisfy the conditions.
Constraints
For all testdata, it is guaranteed that ,,and the answer before taking modulo is not 。
Bundled test: 5 subtasks, with the following limits:
- Subtask 1 (11 pts): 。
- Subtask 2 (24 pts): 。
- Subtask 3 (17 pts): 。
- Subtask 4 (22 pts): 。
- Subtask 5 (26 pts): no special restrictions.
Translated by ChatGPT 5