Sharif Super Computer
Time limit1sMemory limit128 MB
Choose distinct positive slave heights between 0 and a top master height H so that all red cable lengths match exactly and every slave pair distance is an allowed blue length, minimizing the output sequence lexicographically.
- Level
Hard8 of 10
- Topics
- Brute force, Backtracking, Math, Implementation
- Solved
- No attempts yet
Problem
SSC is a super computer designed at Sharif University. It has 2 "master" processors and "slave" processors, and it can run software in parallel: one master loads the software onto the slave processors so that memory and CPU usage are balanced among them, while the other master monitors the system.
Because different parts of the software depend on each other, many messages must be exchanged between processors. A very fast network is needed to minimize the message-passing overhead. To optimize the network, a clique structure is built in which there is a direct communication cable between every pair of processors.
There are two kinds of cable: blue cables, which transmit up to 100 Megabits per second, and red cables, which transmit up to 1 Gigabit per second. Every pair of slave processors is connected by one blue cable. Because the master processors carry more traffic, the two masters are connected by one red cable, and each master is also connected to each slave by one red cable. Hence there are exactly red cables: one between the two masters and between masters and slaves.
SSC is therefore made of motherboards, each containing exactly one processor, the needed memory, and identical network sockets arranged horizontally. The motherboards are placed one per horizontal shelf in a vertical rack box, so each motherboard is uniquely identified by its height in the rack.
For cooling reasons, the two master motherboards must occupy the lowest and highest shelves of the rack. Let the bottom master have height ; every other motherboard has a distinct positive integer height. As the engineer, you receive the empty rack and the ready motherboards and must assemble SSC. You want the cabling to be tidy and tight, so you place the motherboards so that the length of the cable between any two boards equals the difference of their heights.
In short, you must assign heights as follows.
- The bottom master has height ; the top master sits at the highest position (height ).
- The slaves get distinct positive integer heights strictly between and .
The assignment must satisfy both of the following.
- The multiset of the lengths of all pairs that involve a master (that is, the red-cable lengths) matches the given red-cable lengths exactly.
- The height difference of every pair of slaves equals one of the given blue-cable lengths. (Each blue length is available in unlimited quantity.)
Input
The input contains several test cases.
The first line of each test case contains two integers and (, ). The second line contains the lengths of the red (Gigabit) cables. The third line contains the available lengths of the blue (Megabit) cables.
The last line of the input contains two zeros, marking the end of the input.
Output
For each test case, print integers on one line. The first number is the height of the top master processor, and the remaining numbers are the heights of the slaves in increasing order.
If several valid arrangements exist, print the one whose output sequence (the top master's height followed by the slave heights in increasing order) is lexicographically smallest when the numbers are compared as integers.
If no valid arrangement exists, print Impossible.