atcoder#ABC130E. [ABC130E] Common Subsequence
[ABC130E] Common Subsequence
Score : points
Problem Statement
You are given two integer sequences and of length and , respectively, both consisting of integers between and (inclusive).
In how many pairs of a subsequence of and a subsequence of do the two subsequences are the same in content?
Here the subsequence of is a sequence obtained by removing zero or more elements from and concatenating the remaining elements without changing the order.
For both and , we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.
Since the answer can be tremendous, print the number modulo .
Constraints
- The length of is .
- The length of is .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the number of pairs of a subsequence of and a subsequence of such that the subsequences are the same in content, modulo .
2 2
1 3
3 1
3
has four subsequences: .
has four subsequences: .
There are pair of subsequences in which the subsequences are both , pair of subsequences in which the subsequences are both , and pair of subsequences in which the subsequences are both , for a total of three pairs.
2 2
1 1
1 1
6
has four subsequences: .
has four subsequences: .
There are pair of subsequences in which the subsequences are both , pairs of subsequences in which the subsequences are both , and pair of subsequences in which the subsequences are both , for a total of six pairs. Note again that we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.
4 4
3 4 5 6
3 4 5 6
16
10 9
9 6 5 7 5 9 8 5 6 7
8 6 8 5 5 7 9 9 7
191
20 20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
846527861
Be sure to print the number modulo .