#P10789. [NOI2024] 登山
[NOI2024] 登山
Problem Description
"Why climb? Because the mountain is there."
There are locations on Muztagh Mountain, numbered from to , where location is the summit. These locations form a rooted tree, with node as the root. For , the parent of node is node .
Let be the number of edges on the path from node to the summit. Formally, , and for , .
A climbing path is a plan that starts from some node among , and after several moves, reaches the summit.
A move starting from node is one of the following two types:
- Sprint: within the given sprint range , choose a positive integer such that , and move steps toward the summit, i.e. move to the -th ancestor of node in the rooted tree. It is guaranteed that .
- Rest: because the terrain of Muztagh Mountain is steep, when resting you will slip down to one of its children. Formally, choose a node such that , and move to node . In particular, if node is a leaf in the rooted tree, then there is no satisfying , so you cannot choose Rest in this case.
The climbing sequence corresponding to a climbing path is the sequence consisting of the starting node and every node reached after each move. Formally, the climbing sequence corresponding to a climbing path starting from node is a node sequence such that for , is either the -th ancestor of , or satisfies .
To ensure that every sprint can get closer to the summit, a legal climbing path must satisfy: for the starting node or any node reached after some move, every later node reached by sprinting must satisfy , where is a given parameter, guaranteed to satisfy . Formally, a legal climbing path with climbing sequence must satisfy: for all , if , then .
For all nodes , find the number of legal climbing paths starting from each of these nodes. Two climbing paths are different if and only if their corresponding climbing sequences are different. Since the answer may be large, you only need to output it modulo .
Input Format
This problem contains multiple test cases.
The first line contains an integer , indicating the test point ID. means this test point is the sample.
The second line contains an integer , the number of test cases.
Then each test case is given as follows:
The first line contains an integer , the number of locations on Muztagh Mountain.
The next lines: the -th line () contains four integers . It is guaranteed that , , and .
Output Format
For each test case, output one line with integers, where the -th integer is the number of ways (modulo ) to reach the summit starting from node (i.e. nodes ).
0
3
5
1 1 1 0
2 1 1 0
2 1 2 1
4 2 3 0
6
1 1 1 0
2 1 2 0
3 1 3 2
4 1 4 1
5 1 5 3
6
1 1 1 0
2 1 2 0
2 1 2 0
3 1 2 0
3 2 3 2
3 3 2 4
5 9 3 21 6
4 10 5 14 1
见 mountain2.in/ans
这个样例满足测试点 2,3 的约束条件
见 mountain3.in/ans
这个样例满足测试点 9 的约束条件
见 mountain4.in/ans
这个样例满足测试点 11,12 的约束条件
见 mountain5.in/ans
这个样例满足测试点 13 的约束条件
Hint
[Sample 1 Explanation]
Sample contains three test cases.
For the first test case, the structure of locations on Muztagh Mountain is as follows:

In this test case, , , , .
There are legal climbing paths starting from :
- Sprint directly to the 2-nd ancestor of , i.e. , reaching the summit. The corresponding climbing sequence is .
- Rest first and slip down to , then sprint from to its 3-rd ancestor, reaching the summit. The corresponding climbing sequence is .
There are legal climbing paths starting from :
- Sprint directly to the 3-rd ancestor of , i.e. , reaching the summit. The corresponding climbing sequence is .
- Sprint first to the 2-nd ancestor of , i.e. ; then sprint from to its 1-st ancestor, reaching the summit. The corresponding climbing sequence is .
- Sprint first to the 2-nd ancestor of , i.e. ; then rest at and slip down to ; then sprint from to its 2-nd ancestor, reaching the summit. The corresponding climbing sequence is .
- Sprint first to the 2-nd ancestor of , i.e. ; then rest at and slip down to ; keep resting and slip down to ; then sprint again from to its 3-rd ancestor, reaching the summit. The corresponding climbing sequence is .
[Constraints]
For all test cases, it is guaranteed that: , .
For any , it is guaranteed that: , , .
::cute-table{tuack}
| Test point ID | Whether there exists | Whether there exists | Whether there exists | |
|---|---|---|---|---|
| No | ||||
| ^ | ||||
| Yes | ||||
| ^ | ^ | ^ | No | |
| No | Yes | |||
| ^ | No | |||
| No | Yes | |||
| ^ | ^ | No | ||
| No | Yes | |||
| ^ | No | |||
Translated by ChatGPT 5