Polynomial Remainder Operation
Time limit1sMemory limit128 MB
Multiply two binary polynomials over GF(2) and print the remainder modulo a third polynomial, all given as bit strings.
- Level
Easy3 of 10
- Topics
- Math, Bit manipulation, Implementation
- Solved
- No attempts yet
Problem
Consider a special set of polynomials whose coefficients are all either 0 or 1. Addition, subtraction, and multiplication on this set are all defined by reducing each resulting coefficient modulo 2.
Addition. Add the two polynomials in the usual way, then reduce every coefficient modulo 2. Since , , , and , each coefficient of the result is the XOR of the corresponding coefficients of the two operands.
Subtraction. Likewise , , , and , so on this set subtraction is identical to addition (coefficient-wise XOR).
Multiplication. Multiply the two polynomials in the usual way, then reduce every coefficient modulo 2.
Remainder. The remainder of the product of and divided by is obtained by first forming as above and then taking its remainder upon division by .
Notation. Let be the degree of a polynomial's highest term. Because every coefficient is 0 or 1, the polynomial is uniquely described by the integer together with a bit string of length . The bit string lists the coefficients from the highest-degree term down to the constant term. Every polynomial has degree less than 1000.
For example, is written as:
8 1 1 0 0 0 0 0 1
Here 8 is the total number of bits, equal to the highest degree 7 plus 1. The following 8 bits 1 1 0 0 0 0 0 1 are the coefficients of respectively.
Given three polynomials , , and in this format, compute the remainder of divided by .
Input
The input consists of several test cases.
The first line contains the number of test cases . Each test case then spans three lines giving , , and , in that order, in the format described above.
Output
For each test case, print on one line the remainder of divided by , in the format described above. If the remainder is the zero polynomial, print it as 1 0.