F. Boolean Computer
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"".
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"".
Print \(m\) lines. The \(i\)-th line should contain the number of ordered pairs of variables for which the \(i\)-th gate returns zero.
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\).