#P9496. Minimum Cost Bitwise Transformation
Minimum Cost Bitwise Transformation
Minimum Cost Bitwise Transformation
You are given two positive integers n and m. In one operation, you can choose one of the following two actions on n at a cost of 1:
- ACCEPT: Replace n with n | k, where k is any positive integer. Here, | denotes the bitwise OR operation.
- BOTH: Replace n with n & k, where k is any positive integer. Here, & denotes the bitwise AND operation.
Your task is to determine the minimum cost required to transform n into m using a sequence of these operations (operations may be performed zero or more times).
Note: The operations work on the binary representations of the numbers. In particular:
- The OR operation, n = n | k, can only set bits to 1 (it cannot clear bits already set in n).
- The AND operation, n = n & k, can only clear bits to 0 (it cannot set 0 bits to 1).
Thus, if n already has extra bits (1's) that m does not have, you can clear them using the BOTH operation. Conversely, if n lacks some bits that m has, you can set them using the ACCEPT operation.
Hint (in LaTeX):
\( \text{If } n = m, \text{ the cost is } 0. \)
\( \text{If } n \subseteq m \text{ (i.e. } n \text{ has no bits that are 1 while } m \text{ has 0), then one ACCEPT operation is enough.} \)
\( \text{If } m \subseteq n \text{ (i.e. } m \text{ has no bits that are 1 while } n \text{ has 0), then one BOTH operation is enough.} \)
\( \text{Otherwise, the answer is } 2. \)
inputFormat
The input consists of a single line with two space-separated positive integers n and m.
\(1 \leq n, m \leq 10^9\)
outputFormat
Output a single integer, the minimum cost required to transform n into m using the allowed operations.
sample
3 3
0