Comment

avatar username

Shorter code for C.

for _ in range(int(input())):
    b, c, d = map(int, input().split())
    a = 0
    f = 0
    for i in range(62):
        ch = 1 << i
        if d & ch:
            if ch ^ (b & ch):
                a |= ch
                f |= ch & c
        elif b & ch:
            if ch & c:
                a |= ch
            else:
                f |= True
    print([a, -1][f!=0])
Explanation

For each of the 62 bits i.e $[2^0, 2^{61}]$

if $d_{i}$ is set;

  • and $b_{i}$ is not set;
    • then $a_{i}$ must be set;
    • raise a flag if $c_{i}$ is set, as it would unset this bit in the final result.
  • else if $b_{i}$ is set; save us the stress and do nothing.

else if $d_{i}$ is not set and $b_{i}$ is set i.e we don't need this bit but $b_{i}$ gives it anyway;

  • then we need $c_{i}$ to be set to take it out, and if we must get $c_{i}$ then we have to set $a_{i}$;
  • else if $c_{i}$ is not set, we raise a flag as we have an unwanted set bit that we can't take out.

Finally if the flag was raised print $-1$ else print $a$.

The actual rating of this user is 1076.

Original comment.

Statistics