luogu#P8057. D ToPTree
D ToPTree
Background
ToPTree stands for ToothPaste Tree. It works like squeezing toothpaste: it moves only when you squeeze it.
Problem Description
You have a sequence of numbers . Initially, .
There is an operation sequence consisting of operations. The -th operation is chosen uniformly at random from all possible operations:
- With probability , randomly choose one of the following two types of operations as this operation.
- Randomly choose positive integers , and add to every number in the interval .
- Randomly choose positive integers , and set every number in the interval to .
- It is easy to see that there are possible operations each time.
Since this tree is a ToothPaste Tree, it will execute only the operations related to your current query that have not been executed yet, and only when you query. Specifically, suppose you query the values of in order, then:
- When querying , execute all operations in that involve this number (i.e. whose interval contains ) in chronological order (i.e. in the order in ), and delete them from . Then output the current value of .
For example, currently contains the following four operations in order, and all elements in are currently :
- Add to interval .
- Add to interval .
- Assign interval to .
- Add to interval .
Now query the value of . Then operations will be executed in order, causing to become . Therefore ToPTree outputs . The operation sequence becomes:
- Add to interval .
Query the value of next. Then operation will be executed, making the operation sequence empty, and becomes , so the output is . Note that this result is not the same as the result obtained by executing all operations in order.
Given and the sequence , ToPTree will output values in the query order. Find the expected value of each of these outputs, modulo .
(Before the first query, no operations have been executed.)
Input Format
The first line contains four positive integers .
The next line contains positive integers .
Output Format
Output one line with non-negative integers separated by spaces, as the answers modulo .
2 2 2 2
1 1
665496237 665496237
Hint
Constraints
This problem uses bundled testdata.
For all testdata: , , . The detailed constraints are as follows:
- Subtask #1 (4 pts): .
- Subtask #2 (10 pts): .
- Subtask #3 (3 pts): , .
- Subtask #4 (3 pts): , .
- Subtask #5 (12 pts): , .
- Subtask #6 (27 pts): .
- Subtask #7 (9 pts): .
- Subtask #8 (16 pts): .
- Subtask #9 (16 pts): No additional constraints.
Translated by ChatGPT 5