PROBLEM LINK:
Author, Tester & Editorialist: Devanshu Garg
DIFFICULTY:
EASY - MEDIUM
PREREQUISITES:
PROBLEM:
Task was to write a program that takes two non - negative integers X and Y as an input and output a single non - negative integer Z, which is equivalent to X' * Y'.
X' and Y' satisfy the following two conditions:
- X = X' + Y',
- Y = X' \oplus Y', where \oplus is bit-wise exclusive or.
Since Z can be large, output Z modulo 1000000007 (10^9 + 7).
Note - X' is the smallest number among all the numbers which satisfy the above two conditions.
EXPLANATION:
Let’s take a look at some bit in X^{'}, which is equal to 1. If the respective bit in Y^{'} is equal to 0, then we can swap these two bits, thus reducing X^{'} and increasing Y^{'} without changing their sum and xor. We can conclude that if some bit in X^{'} is equal to 1 then the respective bit in Y^{'} is also equal to 1. Thus, Y^{'} = X^{'} + Y. Taking into account that X^{'} + Y^{'} = X^{'} + X^{'} + Y = X, we can obtain the following formulas for finding X^{'} and Y^{'}:
- X^{'} = (X - Y) / 2
- Y^{'} = X^{'} + Y
One should also notice that if X < Y or X and Y have different parity, then the answer doesn’t exist and we should output -1. If X^{'} and (X - X^{'}) \neq X^{'} then the answer is also -1.
Now if we have computed X^{'} and Y^{'}, then we just need to multiply them to get the desired value of Z.
SOLUTIONS:
Editorialist's Solution
x, y = map(int, input().split())
if (x + y) & 1:
print(-1)
else:
mod = int(1e9+7)
x_ = (x - y) // 2
y_ = x_ + y
print(((x_ % mod) * y_) % mod)
Well this was my approach, feel free to share your approach here. Suggestions are always welcomed.