Split and Merge
Time limit1sMemory limit512 MB
Given two tilings of a 1xL board by 1x1 and 1x2 pieces, find the minimum number of split/merge operations to transform one into the other and count the ways.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Combinatorics, String matching, Prefix sum
- Solved
- No attempts yet
Problem
A board is divided into pieces and pieces.
You can perform two kinds of operations. One splits a single piece into two pieces. The other merges two adjacent pieces into a single piece.

Given the initial state and the target state of the board, find the minimum number of operations and the number of ways to change the state with that many operations.
Input
The first line gives the board length ().
The second line gives the number of pieces in the initial state ().
The third line gives numbers . They list each piece length from the leftmost piece, and each number is 1 or 2. The numbers sum to .
The fourth line gives the number of pieces in the target state ().
The fifth line gives numbers in the same format as the third line.
Output
Print the minimum number of operations and the number of ways, separated by a space. Since the number of ways can be large, print it modulo .