U
U
Umid2017-05-16 04:40:47
JavaScript
Umid, 2017-05-16 04:40:47

Bit problem?

Greetings.
There is a problem from the Olympics.
Perhaps the translation is a little crooked, which is why I can’t solve it for me.
I came to you for help.
1982fa6afb0b4d57aa153cabfc514049.jpg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2017-05-16
@jcmvbkbc

a (+) x > x only if the most significant non-zero bit of a corresponds to the zero bit of x.
You need to go through the bits of x and for each position i, in which there is a 0, count how many different numbers there are from the highest 1 in this position. There are obviously 2^i.
For the test case:
2 in binary is 10, 0 in bit zero only, number of matching a's: 2^0 == 1
10 in binary is 1010, 0 in bit zero gives 2^0 == 1, 0 in second bit gives 2^2 == 4, total matching a: 1 + 4 == 5.
Answer is 1 + 5 == 6.

B
Boris Korobkov, 2017-05-16
@BorisKorobkov

(+) is XOR. In order for the number to increase after XOR, it is necessary:
​​- in any bit zero of "x" (except for the very top ones, otherwise "a" will be greater than "x") make a bit one for "a" (so that XOR increases)
- while in all subsequent (right) bits can be any value (2^NumberBitsRemaining)
My solution: sandbox.onlinephpfunctions.com/code/e6c9bb63f63c26... (run on PHP7)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question