hey, can someone please help me with this problem? https://icpcarchive.ecs.baylor.edu/external/74/7401.pdf https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=691&page=show_problem&problem=5423
You are given an infinite binary indexed tree, where each node's value is its index. You're also given two integers, N and K. you're supposed to print a walk on this tree, where each node you visit, you either add or subtract it to your sum. The length of the walk must be K and the final sum must be N.
thanks.
If N is not even you can always go to the left son. These nodes are exponents of 2. Consider N in base 2 and a bit i, which is not the MSB. If the bit
Unable to parse markup [type=CF_TEX]
is set then consider i as an addition to the sum. Otherwise, we still want to add 2i to the sum, but we also want to cancel all the bits i + 1, i + 2, .. j - 1, considering j as the smallest index that is greater than i and it is of a set bit. To do so, we notice thatUnable to parse markup [type=CF_TEX]
. Obviously, we now can write down the signs for the (i + 1, j — 1) substring. Every position which shares the same parity withUnable to parse markup [type=CF_TEX]
will be added the sum and the others will be subtracted. Doing this, we have used the nodesUnable to parse markup [type=CF_TEX]
and we have the sumUnable to parse markup [type=CF_TEX]
, from which we subtract 2i, obtaining 2i just what we wanted in the first place. We are allowed to follow this path because it is known thatUnable to parse markup [type=CF_TEX]
.If N is even, find the solution for N - 1. At the end, instead of going from the node
Unable to parse markup [type=CF_TEX]
to the last node on the left, choose the right node instead, which isUnable to parse markup [type=CF_TEX]
.For example, 9 in base 2 is
Unable to parse markup [type=CF_TEX]
, so we take - 1 +Unable to parse markup [type=CF_TEX]
+ 4 + 8. For 10 we will simply modify the former solution, takingUnable to parse markup [type=CF_TEX]
instead ofUnable to parse markup [type=CF_TEX]
.thank you so much for your awsome explanation! lots of love to Bucharest :)