#P4427. [BJOI2018] 求和
[BJOI2018] 求和
Description
master is very interested in sums on trees. He generated a rooted tree and wants to query, multiple times, the sum of the -th power of the depths of all nodes on a path, and may be different each time. Here, the depth of a node is defined as the number of edges on the path from this node to the root. He handed this problem to pupil, but pupil does not know how to perform such complex operations. Can you help him?
Input Format
The first line contains a positive integer , the number of nodes in the tree.
Each of the next lines contains two space-separated positive integers , indicating an edge between nodes and in the tree.
The next line contains a positive integer , the number of queries.
Each of the next lines contains three space-separated positive integers , representing a query asking for the sum of the -th power of the depths of all nodes on the path from node to node . Since the result can be very large, output it modulo .
Nodes are numbered starting from , and node is the root.
Output Format
For each query, output one line with a single integer, the result modulo .
5
1 2
1 3
2 4
2 5
2
1 4 5
5 4 45
33
503245989
Hint
Sample Explanation
Let denote the depth of node .
For the sample tree, $d (1) = 0, d (2) = 1, d (3) = 1, d (4) = 2, d (5) = 2$.
Therefore, the answer to the first query is ; the answer to the second query is $(2^{45} + 1^{45} + 2^{45}) \bmod 998244353 = 503245989$.
Constraints
- For of the testdata, .
- For of the testdata, .
- For of the testdata, , .
Additionally, there are unscored hack testdata.
Tip
The input size is large; please use fast I/O.
Translated by ChatGPT 5
京公网安备 11011102002149号