All problems

Problem 674

Minimum Flips to Make a OR b Equal to c

Medium · 20 pts · Bit Manipulation

Problem

Given three positive integers A, B and C, find the minimum number of single-bit flips in A and B needed to make (A OR B) equal to C. A flip changes one bit of A or B from 0 to 1 or from 1 to 0. Look at each bit position separately: if C's bit is 1 and both A and B have 0, one flip is needed; if C's bit is 0, every 1 among A's and B's bits must be flipped.

Input

A single line with A, B and C.

Output

Print the minimum number of flips.

Constraints

1 <= A, B, C <= 10^9

Example input

2 6 5

Example output

3

Explanation

For A = 2 (0010), B = 6 (0110) and C = 5 (0101): bit 0 needs one flip (to 1), bit 1 has 1 in both A and B and needs two flips, and bit 2 already matches. The total is 3.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Number Complement Next: XOR Sum of All Pairs Bitwise AND