1967: [USACO 2022 US Open Contest Gold] Problem 2. Pair Programming

文件提交:无需freopen 内存限制:256 MB 时间限制:2.000 S
评测方式:普通裁判 命题人:
提交:2 解决:1

题目描述

A program consists of a sequence of instructions, each of which is of one of the following forms:

  1. ×d, where d is a digit in the range [0,9]
  2. s, where s is a string denoting the name of a variable. Within a program, all variable names must be distinct.

The result of executing a program is defined to be the expression that results after applying each instruction in order, starting with 0. For example, the result of executing the program [×3,+x,+y,×2,+z] is the expression (0×3+x+y)×2+z=2×x+2×y+z. Different programs, when executed may produce the same expressions; for example, executing [+w,×0,+y,+x,×2,+z,×1] would also result in the expression 2×x+2×y+z.

Bessie and Elsie each have programs of N (1N2000) instructions. They will interleave these programs to produce a new program of length 2N. Note that there are (2N)!N!×N! ways to do this, but not all such programs, when executed, will produce distinct expressions.

Count the number of distinct expressions that may be produced by executing Bessie and Elsie's interleaved program, modulo 109+7.

Each input contains T (1T10) test cases that should be solved independently. It is guaranteed that the sum of N over all test cases does not exceed 
.

输入

The first line of the input contains T, the number of test cases.

The first line of each test case contains .

The second line of each test case contains Bessie's program, represented by a string of length N. Each character is either a digit d[0,9], representing an instruction of type 1, or the character , representing an instruction of type 2.

The third line of each test case contains Elsie's program in the same format as Bessie's.

Within a test case, the variable names among all instructions are distinct. Note that their actual names are not provided, as they do not affect the answer.

输出

The number of distinct expressions that may be produced by executing Bessie and Elsie's interleaved programs, modulo 109+7.

样例输入

4
1
0
1
3
12+
+02
3
0++
++9
4
5+++
+6+1

样例输出

1
3
9
9

提示

For the first test case, the two possible interleaved programs are[×1,×0] and [×0,×1]. These will both produce the expression 0 when executed.

For the second test case, executing an interleaving of [×1,×2,+x] and [+y,×0,×2] could produce one of the expressions 0x, or 2×x

SCORING:

  • Input 2 satisfies N6.
  • In inputs 3-5, the sum of all N is at most .
  • In inputs 6-8, the sum of all N is at most .
  • Inputs 9-16 satisfy no additional constraints.

来源/分类