#P7592. 数树(2021 CoE-II E)

数树(2021 CoE-II E)

Description

Define a tree T\mathcal T to be a k1k2k_1-k_2-ary tree if and only if every node pTp \in \mathcal T that has children has either k1k_1 children or k2k_2 children, with k1k2k_1 \ne k_2. We define two k1k2k_1-k_2 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 k1k2k_1-k_2-ary tree has a fixed root, the children of each node are ordered, but the nodes are unlabeled.

If a k1k2k_1-k_2-ary tree T\mathcal T has a k1k_1-branching node, it gains score aa; if it has a k2k_2-branching node, it gains score bb; leaf nodes give no score. Define the score of a tree as the sum of the scores of all its nodes, denoted by v(T)v(\mathcal T).

Now, among all pairwise non-isomorphic k1k2k_1-k_2-ary trees with nn nodes, we generate one tree T\mathcal T uniformly at random. Let the expected value of v(T)v(\mathcal T) be E(v(T))\mathbb{E}(v(\mathcal T)).

It can be proven that E(v(T))\mathbb{E}(v(\mathcal T)) is a rational number. When E(v(T))0\mathbb{E}(v(\mathcal T)) \ne 0, write E(v(T))=p/q\mathbb{E}(v(\mathcal T)) = p/q, where pp and qq are coprime. You need to output the smallest natural number xx such that pqx(modP)p \equiv qx \pmod P, where P=998244353P = 998244353. It can be proven that such a natural number xx must exist.

Input Format

The input contains five integers k1, k2, n, a, bk_1,\ k_2,\ n,\ a,\ b, with meanings as described above.

Output Format

Output an integer xx, the smallest natural number solution to pqx(modP)p \equiv qx \pmod P, where P=998244353P = 998244353.

2 3 6 1 2
3

Hint

Sample Explanation.

There are 55 non-isomorphic 232-3-ary trees with 66 nodes. Each tree has score 33, so E(v(T))=15/5=3\mathbb{E}(v(\mathcal T)) = 15/5 = 3. Therefore p=3p = 3 and q=1q = 1, so x=3x = 3.


Constraints

There are 1010 test points in total.

For test point 11, 1k1, k2<n101 \le k_1,\ k_2 < n \le 10.

For test point 22, 1k1, k2<n151 \le k_1,\ k_2 < n \le 15.

For test points 343 \sim 4, 1k1, k2<n5×1031 \le k_1,\ k_2 < n \le 5 \times 10^3.

For test points 565 \sim 6, a=b=1, 1k1, k2<n105a = b = 1,\ 1 \le k_1,\ k_2 < n \le 10^5.

For 100%100\% 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 E(v(T))0\mathbb{E}(v(\mathcal T)) \ne 0.

Translated by ChatGPT 5