Parity
Time limit10sMemory limit512 MB
Given n binary strings and target bits, find the smallest column subset of size at most k whose XOR over each string matches its bit.
- Level
Medium7 of 10
- Topics
- Bit manipulation, Greedy, Brute force, Math
- Solved
- No attempts yet
Problem
You are given binary strings , all of the same length . Each string comes with a bit . The strings are -indexed.
For a subset of column indices, define the value it produces on string as the XOR of the bits of at the positions in . (The XOR of a set of bits is if an odd number of them are , and otherwise; the XOR of the empty set is .)
You are given a nonnegative integer . You want to choose a subset of size at most so that for every , the value produced on equals .
For example, if and , then the value on is (the bit at index ) XOR (the bit at index ), which is .
Among all valid subsets of size at most , output the minimum possible size. If no such subset exists, report that instead.
Input
The first line contains two space-separated integers and (, ).
Each of the next lines contains a string , a single space, and its bit . All strings have the same length (), and .
Output
Output a single integer: the minimum possible size of a subset with such that the XOR of the bits of over the indices in equals for every . If no such subset exists, output .