Binary Polynomials

Time limit1sMemory limit128 MB

Summary
Given a Boolean function's polynomial coefficients over n variables, count vectors with exactly k ones that make the function evaluate to 1.
Level

Medium6 of 10

Topics
Bit manipulation, Combinatorics, Brute force, Math
Solved
No attempts yet

Problem

Every mapping ff from the set {0,1}n\{0,1\}^n of nn-dimensional binary vectors to {0,1}\{0,1\} is called a Boolean function of nn variables and is written f(xn,xn−1,…,x1)f(x_n, x_{n-1}, \ldots, x_1). Some properties of Boolean functions are of interest in cryptography. Let B(n,k)B(n,k) denote the set of nn-dimensional binary vectors that have exactly kk ones. For a given Boolean function ff, the task is to find the number of vectors (bn,bn−1,…,b1)(b_n, b_{n-1}, \ldots, b_1) from B(n,k)B(n,k) such that f(bn,bn−1,…,b1)=1f(b_n, b_{n-1}, \ldots, b_1) = 1.

The Boolean function is given by its (unique) polynomial modulo 22. In these polynomials, addition and multiplication modulo 22 are used, as defined in the tables of Fig. 1. In the polynomial of a function, any product of mm variables xi1xi2⋯ximx_{i_1} x_{i_2} \cdots x_{i_m} may or may not appear. So the general form of the polynomial for nn variables is:

a0+a1x1+a2x2+a3x2x1+a4x3+a5x3x1+a6x3x2+a7x3x2x1+⋯+aNxnxn−1⋯x1a_0 + a_1 x_1 + a_2 x_2 + a_3 x_2 x_1 + a_4 x_3 + a_5 x_3 x_1 + a_6 x_3 x_2 + a_7 x_3 x_2 x_1 + \cdots + a_N x_n x_{n-1} \cdots x_1

where every coefficient aja_j (for j=0,1,…,N=2n−1j = 0, 1, \ldots, N = 2^n - 1) is 00 or 11. If a coefficient equals 00 we omit the corresponding product, and if it equals 11 we omit the coefficient itself. For example, the polynomial of the Boolean function "disjunction of 2 variables" shown in Fig. 2 is 0+1⋅x1+1⋅x2+1⋅x2x1=x1+x2+x2x10 + 1 \cdot x_1 + 1 \cdot x_2 + 1 \cdot x_2 x_1 = x_1 + x_2 + x_2 x_1.

Fig. 1

Fig. 2

Input

Your program must handle more than one test case. The first line of input contains the number TT of test cases. Each of the following TT lines describes one function: first the numbers nn and kk separated by a single space (1≤n≤181 \le n \le 18, 0≤k≤n0 \le k \le n), and then, separated by one more space, a string of 2n2^n zeros and ones giving the coefficients of the corresponding polynomial, ordered as in the general form above.

Output

Output TT lines, each containing a single number: the number of vectors found for the corresponding function.

Examples1

  1. Example 1

    Input
    3
    2 1 0111
    4 2 1000000000000000
    5 3 00000000000000000000000000000001
    
    Expected output
    2
    6
    0