Pascal Meets Boole
문제
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 st and th elements in row are set equal to , and for and , element in row 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 J.1(a) depicts the first rows of Pascal's Triangle.
Figure J.1: (a) Pascal’s Triangle, (b) Pascal-Boole triangle for function
But what if we consider a rule other than standard addition for combining values? Since the edge elements are bits '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 J.1(b) (where we also show the first rows). In this truth table, and correspond to elements and respectively, in row , and is the resulting element in row .
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 -bit 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 's are there in the first rows?
입력
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, f is a -bit binary string representing a two-input Boolean function, and is an integer In the first case, is an integer
출력
For a test case of the form B , output a line containing the bit in row , position of the Pascal-Boole triangle generated using . For a test case of the form N , output a line containing the number of 's in the first rows of the Pascal-Boole triangle generated using .
예제
예제 1
3 1000 B 5 3 1111 N 7 0100 B 6 4
1 28 0