#FT0000108. Dãy con có cách chọn duy nhất (Unique Subsequence)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Dãy con có cách chọn duy nhất (Unique Subsequence)
Unique Subsequence
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an integer array . A non-empty subsequence is extracted from by selecting indices with for every .
Because may contain equal elements, the same sequence of values may be obtained from several distinct sets of indices. We accept only if there is exactly one increasing index sequence that extracts it.
For example, when , the subsequence is excluded because it can use position or position . In contrast, has exactly one extraction, using positions .
Count the distinct non-empty value sequences with a unique extraction. Do not count the same value sequence more than once. Print the answer modulo .
Input
- The first line contains the integer , the array length.
- The second line contains exactly integers . Values may repeat.
Output
Print one integer: the number of non-empty value sequences with exactly one extraction from , modulo .
Subtasks
- Subtask 1 (20 points): ; for every ; repeated values are allowed.
- Subtask 2 (30 points): ; for every ; repeated values are allowed.
- Subtask 3 (50 points): ; for every ; repeated values are allowed.
Examples
Example 1
Input
3
1 2 1
Output
5
Explanation
The five uniquely extractable value sequences are from indices , from , from , from index , and from . The value sequence has two extractions (index or ), so it is excluded. Hence the answer is 5.
Example 2
Input
4
4 2 1 3
Output
15
Explanation
All four values are distinct, so two different index selections cannot yield the same value sequence. Every non-empty subsequence is uniquely extractable. There are non-empty index subsets, and the answer is 15.
Example 3
Input
12
1 2 3 6 9 2 3 3 9 6 1 6
Output
1178
Explanation
Process all 12 positions using the DP of uniquely extractable index sequences. The per-position values are . After removing the outdated state at each repeated value, the sum of active states, including the empty selection, is . Removing the empty selection gives . Repeated values explain why the naive count is incorrect.