This page is still under construction.

Parts of this page are still being built. What you see may change.

Word Equations

Time limit1sMemory limit128 MB

Summary
Count the binary word assignments to variables of fixed lengths that make the two sides of a word equation equal.
Level

Hard8 of 10

Topics
String, Union-find, Graph, Math
Solved
No attempts yet

Problem

Every non-empty sequence of the symbols 0 and 1 is called a binary word. A word equation has the form

x1x2…xl=y1y2…yr,x_1 x_2 \dots x_l = y_1 y_2 \dots y_r,

where each xix_i and each yjy_j is either a binary digit (0 or 1) or a variable (a lowercase letter of the English alphabet).

Every variable has a fixed length: the number of binary digits of the words that may be substituted for it. To solve a word equation you must assign to every variable a binary word of exactly that variable's length, so that after substituting the assigned words for all variables, the left side and the right side become the same binary word.

For example, let a,b,c,d,ea, b, c, d, e be variables of lengths 4,2,4,4,24, 2, 4, 4, 2 respectively, and consider the equation

1bad1=acbe.1bad1 = acbe.

It has exactly 1616 distinct solutions.

For a given equation, compute how many distinct solutions it has. Your program should:

  • read the number of equations and their descriptions from standard input;
  • find the number of solutions of each equation;
  • write the results to standard output.

Input

The first line contains an integer xx (1≤x≤51 \le x \le 5), the number of equations. The descriptions of the xx equations follow, with no blank lines between them. Each description consists of exactly six lines:

  1. An integer kk (0≤k≤260 \le k \le 26), the number of distinct variables in the equation. The variables are the first kk lowercase letters of the English alphabet.
  2. A sequence of kk positive integers separated by single spaces: the lengths of the variables a,b,…a, b, \dots in order (the first number is the length of aa, the second the length of bb, and so on). When k=0k = 0 this line is empty.
  3. An integer ll, the length of the left side of the equation, that is, the number of digits and variable letters written in it.
  4. The left side of the equation, written as a string of digits and variable letters with no spaces.
  5. An integer rr, the length of the right side of the equation.
  6. The right side of the equation, encoded in the same way as the left side.

On each side, the number of digits plus the sum of the lengths of the variables (counting every occurrence of a variable) is at most 10 00010\,000.

Output

For each ii from 11 to xx, write on the ii-th line the number of distinct solutions of the ii-th equation.

Examples3

  1. Example 1

    Input
    1
    5
    4 2 4 4 2
    5
    1bad1
    4
    acbe
    
    Expected output
    16
    
  2. Example 2

    Input
    1
    1
    3
    1
    a
    1
    a
    
    Expected output
    8
    
  3. Example 3

    Input
    1
    1
    2
    1
    a
    2
    01
    
    Expected output
    1