#P7592. 数树(2021 CoE-II E)
数树(2021 CoE-II E)
Description
Define a tree to be a -ary tree if and only if every node that has children has either children or children, with . We define two trees to be isomorphic if and only if the strings returned by the following pseudocode are the same:
$$\begin{array}{ll} 1 & \textbf{Input. } \text{The edges of the tree } \mathcal T \\ 2 & \textbf{Output. } \text{The eigenvalue of tree } \mathcal T.\\ 3 & \textbf{Algorithm. } \text{dfs}(u)\\ 4 & \qquad result \gets \texttt{'('} \\ 5 & \qquad \textbf{for} \text{ each } (u, v) \text{ in the } \mathcal T \\ 6 & \qquad \qquad \textbf{if } v \text{ is not the father of } u \\ 7 & \qquad \qquad\qquad result \gets result\ +\ \mathrm{dfs}(v) \\ 8 & \qquad result\gets result\ +\ \texttt{')'} \\ 9 & \qquad \textbf{return } result \\ 10 & \textbf{Method. } \text{check}(\mathcal T) \\ 11 & \qquad \textbf{return } \text{dfs(the root of the tree } \mathcal T\text{)} \end{array}$$Formally, a -ary tree has a fixed root, the children of each node are ordered, but the nodes are unlabeled.
If a -ary tree has a -branching node, it gains score ; if it has a -branching node, it gains score ; leaf nodes give no score. Define the score of a tree as the sum of the scores of all its nodes, denoted by .
Now, among all pairwise non-isomorphic -ary trees with nodes, we generate one tree uniformly at random. Let the expected value of be .
It can be proven that is a rational number. When , write , where and are coprime. You need to output the smallest natural number such that , where . It can be proven that such a natural number must exist.
Input Format
The input contains five integers , with meanings as described above.
Output Format
Output an integer , the smallest natural number solution to , where .
2 3 6 1 2
3
Hint
Sample Explanation.
There are non-isomorphic -ary trees with nodes. Each tree has score , so . Therefore and , so .

Constraints
There are test points in total.
For test point , .
For test point , .
For test points , .
For test points , .
For of the testdata, $1 \le k_1,\ k_2 < n \le 10^7,\ k_1 \ne k_2,\ k_1 + k_2 \le n,\ 1 \le a,\ b \le 10^7$.
Notes
- The testdata guarantees that .
Translated by ChatGPT 5
京公网安备 11011102002149号