#FT0000098. Chọn ghế ngẫu nhiên tạo dãy tăng (Random IS)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Chọn ghế ngẫu nhiên tạo dãy tăng (Random IS)
Random IS
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
There are chairs in a row from left to right. The chair at position has ID . The IDs form a permutation of and are all distinct.
Initially no chair is marked. A selection of marked chairs is good when their IDs, read from left to right, form a strictly increasing sequence.
Repeat the following procedure:
- Consider all unmarked chairs. A chair is eligible if marking it would keep the marked selection good.
- If no eligible chair remains, discard all unmarked chairs and stop.
- Otherwise, if chairs are eligible, choose one uniformly at random among them, mark it, and return to step 1.
Let be the expected number of chairs that remain when the procedure stops. Write in lowest terms. Let ; the statement guarantees that has a multiplicative inverse modulo .
Output the unique integer in satisfying , equivalently .
Input
The first line contains . The second line contains integers , the chair IDs from left to right.
Output
Print one integer , representing the expected number of remaining chairs modulo .
Subtasks
- Subtask 1 (20 points): ; is a permutation of , with no repeated value.
- Subtask 2 (30 points): ; is a permutation of , with no repeated value.
- Subtask 3 (50 points): ; is a permutation of , with no repeated value.
Examples
Example 1
Input
3
3 1 2
Output
666666673
Explanation
Initially all three chairs are eligible, each with probability of being chosen first. Choosing ID at the first position prevents choosing IDs and later, leaving chair. Choosing ID or ID first eventually marks both chairs with IDs , leaving chairs. The expectation is . Since the inverse of modulo is , the required residue is .
Example 2
Input
2
1 2
Output
2
Explanation
Whichever chair is marked first, the other can still be marked because is strictly increasing. Every run leaves exactly chairs, giving expectation and modular answer .