Binary Polynomials
Time limit1sMemory limit128 MB
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 from the set of -dimensional binary vectors to is called a Boolean function of variables and is written . Some properties of Boolean functions are of interest in cryptography. Let denote the set of -dimensional binary vectors that have exactly ones. For a given Boolean function , the task is to find the number of vectors from such that .
The Boolean function is given by its (unique) polynomial modulo . In these polynomials, addition and multiplication modulo are used, as defined in the tables of Fig. 1. In the polynomial of a function, any product of variables may or may not appear. So the general form of the polynomial for variables is:
where every coefficient (for ) is or . If a coefficient equals we omit the corresponding product, and if it equals we omit the coefficient itself. For example, the polynomial of the Boolean function "disjunction of 2 variables" shown in Fig. 2 is .

Fig. 1

Fig. 2
Input
Your program must handle more than one test case. The first line of input contains the number of test cases. Each of the following lines describes one function: first the numbers and separated by a single space (, ), and then, separated by one more space, a string of zeros and ones giving the coefficients of the corresponding polynomial, ordered as in the general form above.
Output
Output lines, each containing a single number: the number of vectors found for the corresponding function.