Min Flips To Match OR
Python
Hard
3 views
Problem Description
Read three non-negative integers a b c. In one flip you can change one bit of a or b. Compute minimum flips needed so that (a | b) == c.
Input Format
One line: a b c.
Output Format
One integer flips.
Official Solution
import sys
p=sys.stdin.read().strip().split()
if len(p)<3: sys.exit(0)
a=int(p[0]); b=int(p[1]); c=int(p[2])
ans=0
for i in range(61):
abit=(a>>i)&1
bbit=(b>>i)&1
cbit=(c>>i)&1
if cbit==0:
ans += abit + bbit
else:
if (abit|bbit)==0:
ans += 1
sys.stdout.write(str(ans))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!