Binary Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1034 Accepted Submission(s): 609
Special Judge
Problem Description
The Old Frog King lives onthe root of an infinite tree. According tothe law, each node should connect to exactly two nodes onthe next level, forming a full binary tree.
Since the king is professional in math, he sets a numberto each node. Specifically, the root ofthe tree, wherethe King lives, is1. Say froot=1.
And for each node u, labels as fu, the left child is fu×2and right child is fu×2+1. The king looks at his tree kingdom, and feels satisfied.
Time flies, andthe frog king gets sick. According tothe old dark magic, there is a way forthe king to live for another N years, only if he could collect exactly N soul gems.
Initially the king has zero soul gems, and he is now atthe root. He will walk down, choosing left or right child tocontinue. Each timeat node x, thenumberatthe node is fx (remember froot=1), he can choose to increase his numberof soul gem by fx, or decrease itby fx.
He will walk fromthe root, visit exactly K nodes (including the root), and do the increasement or decreasement as told. If atlastthenumberis N, then he will succeed.
Noting asthe soul gem issome kind of magic, thenumberof soul gems the king has could be negative.
Given N, K, help the King find a way to collect exactly N soul gems by visiting exactly K nodes.
Input
First line contains an integer T, which indicates thenumberof test cases.
Every test case contains two integers N and K, which indicates soul gems the frog king want to collect andnumberof nodes he can visit.
⋅ 1≤T≤100.
⋅ 1≤N≤109.
⋅ N≤2K≤260.
Output
For every test case, you should output "Case #x:"first, where x indicates the case numberand counts from1.
Then K lines follows, each line is formated as 'a b', where a is node label ofthe node the frog visited, and b is either '+' or '-' which means he increases / decreases his numberby a.
It's guaranteed that there are at least one solution andif there are more than one solutions, you can output any of them.
Sample Input
253104
Sample Output
Case #1:1 +
3 -
7 +
Case #2:1 +
3 +
6 -
12 +
Source
2015ACM/ICPC亚洲区上海站-重现赛(感谢华东理工)
Recommend
wange2014