Charging Chaos (Large)
Time limit5sMemory limit512 MB
Find a bit mask applied to every outlet string that makes the outlet set match the device set with the fewest flipped bits, or report that it is impossible.
- Level
Medium6 of 10
- Topics
- Bit manipulation, Hash map, Brute force
- Solved
- No attempts yet
Problem
Shota the farmer just moved into his newly built farmhouse, and the outlets there are not set up for his devices. He owns a pile of smartphones and laptops, and a tablet for his favorite cow Wagyu to use. In total he owns devices.
The devices have different specifications and come from different companies, so each one needs its own electric flow to charge. Each outlet also puts out one specific electric flow. An electric flow is a string of 0s and 1s of length .
Shota wants to charge all devices at the same time, and his new house has exactly outlets. The flow from the outlets is configured on a master control panel with switches. Flipping switch flips the -th bit of the flow from every outlet in the house.
For example, suppose three outlets put out 10, 01, and 11. Flipping the second switch turns those flows into 11, 00, and 10. If Shota has a smartphone that needs 11, a tablet that needs 10, and a laptop that needs 00, flipping the second switch alone charges all three.
Misaki measured the flow of every outlet in the house and found that they are all different. Decide whether Shota can charge all of his devices at the same time, and if he can, find the smallest number of switches that have to be flipped. The switches are big and heavy, so Misaki does not want to flip more of them than she has to.
Input
The first line contains the number of test cases . test cases follow, each on three lines.
The first line of a test case contains two integers and separated by a space. The second line contains strings of length separated by spaces, the flow that each outlet puts out at the start. The third line contains strings of length separated by spaces, the flow that each of Shota's devices needs.
Limits
- No two outlets put out the same flow at the start.
- No two devices need the same flow.
Output
For each test case, print one line in the form Case #x: y, where is the test case number starting from 1 and is the smallest number of switches that have to be flipped so that Shota can charge all of his devices. If there is no way to charge them all, print NOT POSSIBLE in place of , without the quotes.
Hint
In the first test case of the first example, flipping the second switch once turns the outlet flows into 00, 10, and 11. Shota then charges device 1 from outlet 0, device 2 from outlet 1, and device 0 from outlet 2. Flipping nothing charges nothing, so 1 is the smallest answer.