Interleaving Sequences
Time limit9sMemory limit1024 MB
Given sequences A and B, count for each x the pairs of intervals A' and B' whose minimum interleaving stability equals x.
- Level
Hard9 of 10
- Topics
- Array, Two pointers, Combinatorics
- Solved
- No attempts yet
Problem
An interval of a sequence is any nonempty contiguous subsequence of . A sequence of length has intervals, because each interval is determined by its start and end positions.
The stability of a sequence is the largest integer such that there is an index () with or . For example, the stability of is 4, because the strictly monotone interval exists and no longer one does.
An interleaving of two sequences and is any sequence of length that contains a subsequence (not necessarily contiguous) equal to , where the elements outside that subsequence form in order. For example, the interleavings of and include , , and . Neither nor is one.
Let be the minimum possible stability of an interleaving of and .
Given two sequences and of lengths and , instead of , count for each integer from 1 to the number of pairs such that is an interval of , is an interval of , and . These counts can be very large, so print them modulo .
Input
The first line contains two integers and (), the lengths of and .
The second line contains integers ().
The third line contains integers ().
All elements of and are pairwise distinct. In other words, the concatenation of and is a permutation of to .
Output
Print integers separated by single spaces. The -th number is the number of pairs with , modulo .
Hint
The intervals does not apply here. For the whole sequences and , , and one interleaving with stability 2 is .
For and , , and one interleaving with stability 3 is . No interleaving of this pair has stability below 3.
For and , , and both interleavings and are valid examples.
Every pair of intervals in this example can be interleaved so that the stability is at most 3. Therefore the answers for are all 0.