-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2939-maximum-xor-product.cpp
More file actions
40 lines (39 loc) · 1.12 KB
/
2939-maximum-xor-product.cpp
File metadata and controls
40 lines (39 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
int maximumXorProduct(long long a, long long b, int n) {
if (a > b) {
swap(a, b);
}
const int MOD = 1e9 + 7;
long long x = 0;
int highest_bit = 0;
for (long long bit = 0; bit < 50; bit++) {
int abit = (a >> bit) & 1;
int bbit = (b >> bit) & 1;
if (bbit == 1 && abit != 1) {
highest_bit = bit;
}
}
for (long long bit = 0; bit < n; bit++) {
int abit = (a >> bit) & 1;
int bbit = (b >> bit) & 1;
if (abit == 1 && bbit == 1) {
continue;
}
if (abit == 0 && bbit == 0) {
x |= (1LL << bit);
continue;
}
}
for (long long bit = 0; bit < min(n, highest_bit); bit++) {
int abit = (a >> bit) & 1;
int bbit = (b >> bit) & 1;
if (abit == 0 && bbit == 1) {
x |= (1LL << bit);
}
}
long long ans = ((x ^ a) % MOD) * ((x ^ b) % MOD);
ans %= MOD;
return ans;
}
};