#P15672. [ICPC 2024 Jakarta R] Narrower Passageway
[ICPC 2024 Jakarta R] Narrower Passageway
题目描述
You are a strategist of The ICPC Kingdom. You received an intel that there will be monster attacks on a narrow passageway near the kingdom. The narrow passageway can be represented as a grid with rows (numbered from to ) and columns (numbered from to ). Denote as the cell in row and column . A soldier with a power of is assigned to protect every single day.
It is known that the passageway is very foggy. Within a day, each column in the passageway has a chance of being covered in fog. If a column is covered in fog, the two soldiers assigned to that column are not deployed that day. Otherwise, the assigned soldiers will be deployed.
Define a connected area ( ) as a maximal set of consecutive columns from to (inclusive) such that each column in the set is not covered in fog. The following illustration is an example of connected areas. The grayed cells are cells covered in fog. There are connected areas: , , , and .
:::align{center}
:::
The strength of a connected area can be calculated as follows. Let and be the maximum power of the soldiers in the first and second rows of the connected area, respectively. Formally, $ m_r = \max (P_{r, u}, P_{r, u + 1}, \dots, P_{r, v}) $ for . If , then the strength is . Otherwise, the strength is .
The total strength of the deployment is the sum of the strengths for all connected areas. Determine the expected total strength of the deployment on any single day.
输入格式
The first line consists of an integer ( ).
Each of the next two lines consists of integers ( ).
输出格式
Let . It can be shown that the expected total strength can be expressed as an irreducible fraction such that and are integers and . Output an integer in a single line such that and .
3
8 4 5
5 4 8
249561092
5
10 20 5 8 5
5 20 7 5 8
811073541
提示
Explanation for the sample input/output #1
There are possible scenarios for the passageway.
:::align{center}
:::
Each scenario is equally likely to happen. Therefore, the expected total strength is $ (0 + 5 + 10 + 5 + 5 + 0 + 5 + 0) / 8 = \frac{15}{4} $ . Since $ 249\,561\,092 \cdot 4 \equiv 15 \pmod{998\,244\,353} $ , the output of this sample is .
Explanation for the sample input/output #2
The expected total strength is .