Chemical Analysis
InterviewTime limit1sMemory limit128 MB
Given up to 12 element bitmasks and a target bitmask, find the fewest elements whose bitwise OR equals the target, or report that none does.
- Level
Medium6 of 10
- Topics
- Bit manipulation, Brute force, Dynamic programming, Implementation
- Solved
- No attempts yet
Problem
The Curiosity rover carries a lot of high-tech hardware so it can analyze the Martian surface on the spot. (Sending rocks back from Mars is hard to imagine.) One way scientists infer which chemical elements are present in a sample is to heat or burn it and observe which frequencies of light are emitted. Each element has a characteristic spectrum, so from the observed spectrum one can work out which combination of elements could have produced it.
We model this as follows. There are possible frequencies () and candidate elements (). Each candidate element is described by an -bit vector of zeros and ones telling at which frequencies it emits light. The sample is described by the same kind of -bit vector.
We assume that if an element occurs in the sample in any quantity, then every frequency it emits is observed. When several elements emit the same frequency, that frequency is still observed just once — there is no cancellation. Therefore a set of elements explains the sample exactly when the union of their emitted frequencies equals the sample's spectrum: every frequency an element emits must appear in the sample, and every observed frequency must be emitted by at least one chosen element.
For each sample, determine the smallest number of elements whose combined spectrum equals the observed spectrum, or report that no combination does.
Input
The first line contains the number of data sets. Then follow data sets, each of this form:
The first line holds two integers and : the number of frequencies and the number of elements.
The next lines each contain a string of ones and zeros describing one element (line describes element ). The following line contains a string of ones and zeros describing the sample.
Output
For each data set, first print Data Set x: on its own line, where is the data set's number (starting from 1). On the next line print the minimum number of elements whose combined spectrum equals the observed spectrum. If no combination of elements produces exactly the observed spectrum, print Impossible instead. Separate consecutive data sets with a blank line.