#P8273. [USACO22OPEN] Pair Programming G
[USACO22OPEN] Pair Programming G
Background
Due to issues with the testdata, in this problem you do not need to consider non-trivial cases where two different groups of numbers have the same product (cases are considered trivial if they both contain , or differ only by some number of 's, or only differ in order), for example . Alternatively, you should treat in the statement as , respectively.
Problem Description
A program consists of a sequence of instructions, and each instruction has one of the following forms:
- , where is a single digit in the range .
- , where is a string representing a variable name. All variable names appearing in a program are distinct.
The result of executing a program is defined as the expression obtained by applying each instruction in order to the expression . For example, executing the program produces the expression . Different programs may produce the same expression after execution; for example, executing also produces the expression .
Bessie and Elsie each have a program with instructions (). They will interleave the instructions of these programs to create a new program with instructions. Note that there are ways to do this, but not all such programs produce different expressions after execution.
Compute the number of distinct expressions that can result from executing an interleaving of Bessie's and Elsie's programs, modulo .
Each test file contains subtestcases () that should be solved independently. The input guarantees that the sum of over all subtestcases does not exceed .
Input Format
The first line of input contains , the number of subtestcases.
For each subtestcase, the first line contains .
The second line of each subtestcase contains Bessie's program, represented as a string of length . Each character is either a single digit , representing an instruction of type 1, or the character , representing an instruction of type 2.
The third line of each subtestcase contains Elsie's program, in the same format as Bessie's program.
Within a subtestcase, all variable names in all instructions are distinct. Note that their actual names are not given here, because they do not affect the answer.
Output Format
Output the number of distinct expressions that can result from executing an interleaving of Bessie's and Elsie's programs, modulo .
4
1
0
1
3
12+
+02
3
0++
++9
4
5+++
+6+1
1
3
9
9
Hint
【Sample Explanation】
For the first subtestcase, the two interleavings that can be created are and . After execution, both produce the expression .
For the second subtestcase, executing the interleavings of and can produce one of the expressions , , and .
【Test Point Properties】
- Test point 2 satisfies .
- In test points 3-5, the sum of all does not exceed .
- In test points 6-8, the sum of all does not exceed .
- Test points 9-16 have no additional constraints.
Translated by ChatGPT 5