Boolean Computer
Contest: Lyft Level 5 Challenge 2018 - Elimination Round
Difficulty rating: 2800
Tags: bitmasks, brute force, fft, math

Alice has a computer that operates on $$$w$$$-bit integers. The computer has $$$n$$$ registers for values. The current content of the registers is given as an array $$$a_1, a_2, \ldots, a_n$$$.

The computer uses so-called "number gates" to manipulate this data. Each "number gate" takes two registers as inputs and calculates a function of the two values stored in those registers. Note that you can use the same register as both inputs.

Each "number gate" is assembled from bit gates. There are six types of bit gates: AND, OR, XOR, NOT AND, NOT OR, and NOT XOR, denoted "A", "O", "X", "a", "o", "x", respectively. Each bit gate takes two bits as input. Its output given the input bits $$$b_1$$$, $$$b_2$$$ is given below:

$$$\begin{matrix} b_1 & b_2 & A & O & X & a & o & x \\ 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 \\ 0 & 1 & 0 & 1 & 1 & 1 & 0 & 0 \\ 1 & 0 & 0 & 1 & 1 & 1 & 0 & 0 \\ 1 & 1 & 1 & 1 & 0 & 0 & 0 & 1 \\ \end{matrix}$$$

To build a "number gate", one takes $$$w$$$ bit gates and assembles them into an array. A "number gate" takes two $$$w$$$-bit integers $$$x_1$$$ and $$$x_2$$$ as input. The "number gate" splits the integers into $$$w$$$ bits and feeds the $$$i$$$-th bit of each input to the $$$i$$$-th bit gate. After that, it assembles the resulting bits again to form an output word.

For instance, for $$$4$$$-bit computer we might have a "number gate" "AXoA" (AND, XOR, NOT OR, AND). For two inputs, $$$13 = 1101_2$$$ and $$$10 = 1010_2$$$, this returns $$$12 = 1100_2$$$, as $$$1$$$ and $$$1$$$ is $$$1$$$, $$$1$$$ xor $$$0$$$ is $$$1$$$, not ($$$0$$$ or $$$1$$$) is $$$0$$$, and finally $$$1$$$ and $$$0$$$ is $$$0$$$.

You are given a description of $$$m$$$ "number gates". For each gate, your goal is to report the number of register pairs for which the "number gate" outputs the number $$$0$$$. In other words, find the number of ordered pairs $$$(i,j)$$$ where $$$1 \leq i,j \leq n$$$, such that $$$w_k(a_i, a_j) = 0$$$, where $$$w_k$$$ is the function computed by the $$$k$$$-th "number gate".

Input format:
The first line contains three integers: $$$w$$$, $$$n$$$, and $$$m~(1 \leq w \leq 12, 1 \leq n \leq 3\cdot 10^4, 1 \leq m \leq 5\cdot 10^4)$$$ — the word size, the number of variables, and the number of gates.

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ $$$(0 \leq a_i < 2^w)$$$ — the value of variables stored in the registers.

Each of the next $$$m$$$ lines contains a string $$$g_j~(|g_j| = w)$$$ with a description of a single gate. Each character of $$$g_j$$$ is one of "A", "O", "X", "a", "o", "x".

Output format:
Print $$$m$$$ lines. The $$$i$$$-th line should contain the number of ordered pairs of variables for which the $$$i$$$-th gate returns zero.

Constraints:
Time limit: 7.0 s
Memory limit: 256.0 MB

Note:
In the first test case, the inputs in binary are $$$1101$$$, $$$1010$$$, $$$0110$$$. The pairs that return $$$0$$$ are $$$(13, 6)$$$, $$$(6, 13)$$$, and $$$(6, 6)$$$. As it was already mentioned in the problem statement, $$$13 \oplus 10 = 10 \oplus 13 = 12$$$. The other pairs are $$$13 \oplus 13 = 11$$$, $$$10 \oplus 10 = 8$$$ and $$$10 \oplus 6 = 6 \oplus 10 = 4$$$.

Example input:
4 3 1
13 10 6
AXoA

Example output:
3

Example input:
1 7 6
0 1 1 0 1 0 0
A
O
X
a
o
x

Example output:
40
16
25
9
33
24

Editorial:
First we count $$$C[i]$$$ the number of occurrences of $$$i$$$ in the input for each number between $$$0$$$ and $$$2^w - 1$$$. We can then notice that each bit gate $$$G$$$ maps between $$$1$$$ and $$$3$$$ two-bit patterns to $$$0$$$ (e.g. for xor gate, this is 00 and 11, for and gate this is 00, 01 and 10). This means that we can calculate the answer for each number gate in $$$\mathcal O(3^w)$$$, which yields $$$\mathcal O(m \cdot 3^w)$$$ algorithm, that is clearly too slow. How do we make this faster? There are at least two approaches.
1. Interpret each binary input as if it were ternary. For instance, $$$11 = 1011_2 \approx 1011_3$$$. Consider two ternary numbers consisting only of digits $$$0$$$ and $$$1$$$ and their sum  – e.g. $$$1001_3 + 1010_3 = 2011_3$$$. Since there are no carryovers, the $$$i$$$-th digit of the sum tells us how many of the two summands have the respective digit equal to $$$1$$$. In the example above, the most significant digit is $$$2$$$, and we can observe that both inputs have $$$1$$$ as the most significant digit.Each of the six binary gates in the output behaves identically on two bit patterns $$$01$$$ and $$$10$$$. It follows that each number gate $$$G$$$ can be expressed as some function $$$G'$$$, such that $$$G(i,j) = G'(i+j)$$$, i.e. the outcome of the number gate depends only on their sum. For example, the bit gate AND maps $$$0$$$, $$$1$$$ to $$$0$$$ and $$$2$$$ to $$$1$$$, the gate NOT XOR maps $$$0$$$ and $$$2$$$ to $$$1$$$ and $$$1$$$ to $$$0$$$. Furthermore, each gate maps between $$$1$$$ and $$$2$$$ digits to $$$0$$$. For each $$$G'$$$, there are thus at most $$$2^w$$$ values of $$$x$$$ such that $$$G'(x) = 0$$$, and those values can be found efficiently.To find the answer, we need to find for each $$$x \in [0, 3^w)$$$ how many pairs of inputs sum to $$$x$$$ in ternary. This amount is equal to the sum $$$\sum_{i+j = x} C[i] * C[j]$$$ where $$$C[i]$$$ is the number of occurrences of $$$i$$$ in the array $$$A$$$. This can be precomputed once, either by FFT in $$$\mathcal O(w \cdot 3^w)$$$ since it is a convolution, or by brute force in $$$\mathcal O(4^w)$$$. The latter has much better constant factor and is in fact slightly faster.Combined, this yields a $$$\mathcal O(n + w \cdot 3^w + m \cdot 2^w)$$$ algorithm.
2. The second approach was suggested by arsijo. Let's introduce a wildcard in the bit patterns: e.g. $$$1?0?$$$ matches all of $$$1000$$$, $$$1001$$$, $$$1100$$$, $$$1101$$$. This can be found in $$$\mathcal O(w \cdot 3^w)$$$ from the array $$$C$$$ by substituting the bits with the wildcard bit by bit, and denote this array of counts by $$$D$$$.The zero values of each bit gate can be expressed as at most two bit patterns while using wildcards. For instance, the AND gate yields zero with $$$0?$$$ and $$$10$$$. With the help of the arrays $$$C$$$ and $$$D$$$, the answer for each number gate can be found in $$$\mathcal O(2^w)$$$.This also yields a $$$\mathcal O(n + w \cdot 3^w + m \cdot 2^w)$$$ algorithm.