Пожалуйста, подпишитесь на официальный канал Codeforces в Telegram по ссылке https://t.me/codeforces_official. ×

Блог пользователя prathams

Автор prathams, история, 7 лет назад, По-английски

Given x and y, we need to find smallest a and b such that
a + b = x
a xor b = y

How to solve this?

Теги xor
  • Проголосовать: нравится
  • -9
  • Проголосовать: не нравится

»
7 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Is it 1 ≤ x, y ≤ 107? If so, you can brute-force the value of a. If you decide the value of a, the value of b is determined to be x - a, and you can easily check a xor b.

But, I have two questions:
1. What is the actual constraints?
2. Smallest a and b? Which value should be minimized?

»
7 лет назад, # |
Rev. 3   Проголосовать: нравится +13 Проголосовать: не нравится
Hint

By the way, I solved this question few months back and this is the code: https://ideone.com/WmTLgr

»
7 лет назад, # |
  Проголосовать: нравится +10 Проголосовать: не нравится

Another way to solve this problem to simulate the process using digit DP, it works in O(logN).