#P17363. [ECNA 2024] Pascal Meets Boole

[ECNA 2024] Pascal Meets Boole

Problem Description

Many people are familiar with Pascal's Triangle, a triangular arrangement of integers named after the French mathematician and philosopher Blaise Pascal (1623--1662). If we number the rows of Pascal's Triangle 1,2,3,…,1, 2, 3, \ldots, starting from the top, then row rr contains rr elements, which we will number 1,2,…,r1, 2, \ldots, r from left to right. The 1st1^\textrm{st} and rthr^\textrm{th} elements in row r\textrm{row}~r are set equal to 1\textrm{to}~1, and for r≥3r \geq 3 and 1<i<r1 < i < r, element i\textrm{element}~i in row r\textrm{row}~r is the sum of elements i−1i-1 and ii in row r−1.r-1. More informally, each "non-edge" element is the sum of the two elements immediately above it. Figure 1(a) depicts the first 8 rows8~\textrm{rows} of Pascal's Triangle. But what if we consider a rule other than standard addition for combining values? Since the edge elements are bits (1’s),\textrm{bits}~(1\textrm{'s}), a natural option is to use any two-input Boolean function, named after the English mathematician and philosopher George Boole (1815--1864). For example, the Boolean function given by the following truth table generates the triangle depicted in Figure 1(b) (where we also show the first 8 rows).8~\textrm{rows}). In this truth table, xx and yy correspond to elements i−1i-1 and i,i, respectively, in row r−1\textrm{row}~r-1, and f(x,y)f(x,y) is the resulting element i\textrm{element}~i in row r\textrm{row}~r.

xx yy f(x,y)f(x,y)
00 00 11
11 00
11 00
11

In general, if we label the bits in the rightmost column of any such truth table b00,b01,b10,b11b_{00}, b_{01}, b_{10}, b_{11} from top to bottom, then we can compactly represent a two-input Boolean function by the 4-bit4\textrm{-bit} string b00b01b10b11.b_{00} b_{01} b_{10} b_{11}. So the example function above is represented by 10001000.

Your challenge is to answer two kinds of questions about "Pascal-Boole" triangles:

  1. For a given Boolean function, f,f, what is the bit in row rr, position ii?

  2. For a given Boolean function, f,f, how many 1’s1\textrm{'s} are there in the first rr rows?

:::align{center} :::

Input Format

The first line of input contains an integer, nn (1≤n≤250),(1 \leq n \leq 250), the number of test cases. This is followed by nn lines, each of which has one of two forms:

  1. ff B rr ii

  2. ff N rr

In both cases, ff is a 4-bit4\textrm{-bit} binary string representing a two-input Boolean function, and rr is an integer (1≤r≤106).(1 \leq r \leq 10^6). In the first case, ii is an integer (1≤i≤r).(1 \leq i \leq r).

Output Format

For a test case of the form f B r i,f~\texttt{B}~r~i, output a line containing the bit in row r,\textrm{row}~r, position i\textrm{position}~i of the Pascal-Boole triangle generated using f.\textrm{using}~f. For a test case of the form f N r,f~\texttt{N}~r, output a line containing the number of 1’s1\textrm{'s} in the first r rowsr~\textrm{rows} of the Pascal-Boole triangle generated using f.\textrm{using}~f.

3
1000 B 5 3
1111 N 7
0100 B 6 4
1
28
0