#P2456. [SDOI2006] 二进制方程

    ID: 1462 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>字符串高精度2006各省省选山东进制

[SDOI2006] 二进制方程

Description

An equality of the form:

$$\overline{X_1X_2\dots X_n}= \overline{Y_1Y_2\dots Y_m}$$

is called a binary equation.

On both sides of a binary equation, each XiX_i and YjY_j (1in1 \le i \le n and 1jm1 \le j \le m) is either a binary digit 00 or 11, or a variable (a lowercase letter). Each variable represents a binary string of fixed length; this length is called the length of the variable. To solve a binary equation, we assign suitable binary strings to the variables so that, after replacing the variables with their strings (so both sides become binary strings), the equality holds.

Programming task:

For each given equation, compute how many solutions there are in total.

Input Format

The input consists of four lines.

Line 1: kk, the number of variables. The variables are the first kk lowercase English letters (for example, if k=5k=5, then the variables are a,b,c,d,ea, b, c, d, e).

Line 2: kk positive integers separated by a single space, giving the lengths of the kk variables in order.

Line 3: the expression on the left-hand side of the equation.

Line 4: the expression on the right-hand side of the equation.

Output Format

A single integer, the total number of solutions for the variables that appear in the equation.

2
4 2
1b1
a

4
5
4 2 4 4 2
1bad1
acbe

16

Hint

Explanation for Sample 1

There are 44 solutions, as follows:

  1. a=1001a=1001, b=00b=00.
  2. a=1011a=1011, b=01b=01.
  3. a=1101a=1101, b=10b=10.
  4. a=1111a=1111, b=11b=11.

Explanation for Sample 2

k=5k=5, so the variables are a,b,c,d,ea, b, c, d, e, with lengths: 4 2 4 4 24\space 2\space 4\space 4\space2. The equation is 1bad1=acbe\overline{1bad1}=\overline{acbe}.

Therefore, the output is 1616, meaning there are 1616 solutions for the variables a,b,c,d,ea, b, c, d, e.

Constraints

k26k \le 26, because there are at most 2626 variables (the 2626 lowercase English letters).

On each side of the equation, the sum of the lengths of digits and variables does not exceed 1000010000.

Translated by ChatGPT 5