Try XORing This Too
Time limit2sMemory limit1024 MB
Find the weight of each of n coins, where the weights are exactly 1 to k, by querying the XOR of two disjoint subset sums at most n-1 times.
- Level
Medium7 of 10
- Topics
- Bit manipulation, Math, Implementation
- Solved
- No attempts yet
Statement
Coin collector Seungjae had sorted his coins by weight. Then a cat appeared, mixed the coins up at random, and vanished after saying, "Try XORing this too!" Unable to lose to a mere cat, Seungjae decided to sort the coins again using an XOR scale he built himself.
The XOR scale is a modified balance. It weighs each side separately and returns the XOR of the two weights. However, something went wrong during the modification, and if either side is empty, the scale does not work.
The coins Seungjae collected follow a few rules.
- He collected the coins in order from 1 g upward. So if the maximum weight of a coin is g, then for every integer with , there is a coin weighing g.
- is not of the form (). He does not remember the exact value of .
- The coins are labeled from to .
Use these conditions to help Seungjae sort all the coins.
Input
The first line contains the number of coins . The coins are labeled from to .
The maximum weight is not given. , ,
Output
Print the following to standard output (stdout), one per line, to learn the XOR of the total coin weights.
- : Uses the XOR scale to output . is the weight of coin . The indices are distinct coin labels.
After each query, you must flush the standard output buffer, then read the answer from standard input (stdin). Otherwise you get Time Limit Exceeded or Runtime Error.
If any index in a query is outside the range of coin labels, you get Wrong Answer.
You may make at most queries. Making more queries gives Wrong Answer.
Using at most queries, print the answer exactly once to standard output (stdout):
- : Prints the weights of all coins in order.
Then flush the standard output buffer and end the program. This output does not count toward the query limit.
Hint
Flush the standard output buffer after printing the output. Flush methods by language:
- C: fflush(stdout)
- C++: std::cout << std::flush
- Java: System.out.flush()
- Python: sys.stdout.flush()