-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.2939 (#1986)
No.2939.Maximum Xor Product
- Loading branch information
Showing
7 changed files
with
327 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution { | ||
public: | ||
int maximumXorProduct(long long a, long long b, int n) { | ||
const int mod = 1e9 + 7; | ||
long long ax = (a >> n) << n, bx = (b >> n) << n; | ||
for (int i = n - 1; ~i; --i) { | ||
int x = a >> i & 1, y = b >> i & 1; | ||
if (x == y) { | ||
ax |= 1LL << i; | ||
bx |= 1LL << i; | ||
} else if (ax < bx) { | ||
ax |= 1LL << i; | ||
} else { | ||
bx |= 1LL << i; | ||
} | ||
} | ||
ax %= mod; | ||
bx %= mod; | ||
return ax * bx % mod; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
func maximumXorProduct(a int64, b int64, n int) int { | ||
const mod int64 = 1e9 + 7 | ||
ax := (a >> n) << n | ||
bx := (b >> n) << n | ||
for i := n - 1; i >= 0; i-- { | ||
x, y := (a>>i)&1, (b>>i)&1 | ||
if x == y { | ||
ax |= 1 << i | ||
bx |= 1 << i | ||
} else if ax < bx { | ||
ax |= 1 << i | ||
} else { | ||
bx |= 1 << i | ||
} | ||
} | ||
ax %= mod | ||
bx %= mod | ||
return int(ax * bx % mod) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Solution { | ||
public int maximumXorProduct(long a, long b, int n) { | ||
final int mod = (int) 1e9 + 7; | ||
long ax = (a >> n) << n; | ||
long bx = (b >> n) << n; | ||
for (int i = n - 1; i >= 0; --i) { | ||
long x = a >> i & 1; | ||
long y = b >> i & 1; | ||
if (x == y) { | ||
ax |= 1L << i; | ||
bx |= 1L << i; | ||
} else if (ax < bx) { | ||
ax |= 1L << i; | ||
} else { | ||
bx |= 1L << i; | ||
} | ||
} | ||
ax %= mod; | ||
bx %= mod; | ||
return (int) (ax * bx % mod); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution: | ||
def maximumXorProduct(self, a: int, b: int, n: int) -> int: | ||
mod = 10**9 + 7 | ||
ax, bx = (a >> n) << n, (b >> n) << n | ||
for i in range(n - 1, -1, -1): | ||
x = a >> i & 1 | ||
y = b >> i & 1 | ||
if x == y: | ||
ax |= 1 << i | ||
bx |= 1 << i | ||
elif ax > bx: | ||
bx |= 1 << i | ||
else: | ||
ax |= 1 << i | ||
return ax * bx % mod |
Oops, something went wrong.