#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 starting from the top, then row contains elements, which we will number from left to right. The and elements in are set equal , and for and , in is the sum of elements and in row More informally, each "non-edge" element is the sum of the two elements immediately above it. Figure 1(a) depicts the first of Pascal's Triangle. But what if we consider a rule other than standard addition for combining values? Since the edge elements are 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 In this truth table, and correspond to elements and respectively, in , and is the resulting in .
In general, if we label the bits in the rightmost column of any such truth table from top to bottom, then we can compactly represent a two-input Boolean function by the string So the example function above is represented by .
Your challenge is to answer two kinds of questions about "Pascal-Boole" triangles:
-
For a given Boolean function, what is the bit in row , position ?
-
For a given Boolean function, how many are there in the first rows?
:::align{center}
:::
Input Format
The first line of input contains an integer, the number of test cases. This is followed by lines, each of which has one of two forms:
-
B -
N
In both cases, is a binary string representing a two-input Boolean function, and is an integer In the first case, is an integer
Output Format
For a test case of the form output a line containing the bit in of the Pascal-Boole triangle generated For a test case of the form output a line containing the number of in the first of the Pascal-Boole triangle generated
3
1000 B 5 3
1111 N 7
0100 B 6 4
1
28
0