#P10792. 『SpOI - R1』笑起来最帅的小孩
『SpOI - R1』笑起来最帅的小孩
Problem Description
This problem contains multiple test cases.
There is a digit sequence of length . Each element in the sequence is a digit from to .
There is also an empty digit sequence . A cursor will appear in (you can think of it as a thin line that can appear between digits, before the whole digit sequence, or after the whole digit sequence). At this time, there are no digits on either side of the cursor.
Now input the digit sequence into in order. Each time you input one digit, the digit immediately appears after the cursor.
Then the cursor immediately moves randomly to a position before any digit or to after all digits. The randomness is uniform. In other words, the probability that the cursor moves to any movable position is equal.
You are given the digit sequence . You need to output the expected value of the final interpreted directly as a decimal number (ignoring leading zeros), modulo the prime .
Since may be very long, this problem uses compressed input.
Specifically, initially is an empty digit sequence. The input will give you an array of pairs, where the -th item is , meaning the digit appears consecutively times appended to the end of the previous . You can use this method to decompress the real , and then solve the problem.
How to understand expectation in this problem: For a variable with possible outcomes , if the value of outcome is and the probability of obtaining it is , then for the outcome set , the expectation is .
If you do not know how to take a rational number modulo: please see this problem.
Input Format
The first line contains an integer , the number of test cases.
For each test case:
One line contains an integer , the number of items in the pair array after compressing .
Then there are lines, each containing two integers , meaning that based on the sequence obtained from the previous item, you append digits at the end to get the new sequence. You can decompress the real sequence in this way.
Output Format
For each test case, output one integer per line, representing the expected value of the possible (interpreted as a decimal number, ignoring leading zeros) when the cursor moves randomly each time, modulo the prime .
1
2
4 1
2 1
33
1
3
1 2
3 1
7 2
1204285426
Hint
Constraints
This problem uses subtask bundling and subtask dependencies.
Let .
For of the testdata, it is guaranteed that , , , and for any , , .
| Subtask | Special Property | Score | Subtask Dependencies | ||
|---|---|---|---|---|---|
| 1 | None | ||||
| 2 | None | ||||
| 3 | 2 | ||||
| 4 | 2,3 | ||||
| 5 | 1,2,3,4 |
Special Property : it is guaranteed that in the decompressed , every digit appears at most once.
Translated by ChatGPT 5