All problems
0612MediumMathGreedyBit Manipulation

Toggled Register Product

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2939Maximum Xor Product

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

Two meters hold the non-negative readings a and b. A calibration pulse is labelled by one non-negative integer x smaller than 2^n. Firing the pulse leaves each meter showing its own reading combined with x under bitwise exclusive-or. A pulse can therefore flip any chosen set of the n lowest bit positions, and it flips the very same positions in both meters.

Fire the pulse once, with x chosen freely in the range 0 <= x < 2^n. Among all allowed choices, take the largest product of the two resulting readings, reduce that product modulo 1000000007, and return the reduced value. The comparison that picks the largest uses the exact products, not the reduced ones.

Examples

Example 1

Input
a = 9, b = 6, n = 3
Output
56

The pulse may flip the three lowest bits. With `x = 1` the readings become `9 XOR 1 = 8` and `6 XOR 1 = 7`, and 8 * 7 = 56, which is smaller than the modulus and so is returned as it stands.

Example 2

Input
a = 12, b = 4, n = 2
Output
105

Only the two lowest bits may flip, so `x` is one of 0, 1, 2 and 3. With `x = 3` the readings become 15 and 7, whose product is 105.

Example 3

Input
a = 3, b = 3, n = 2
Output
9

Both readings are 3, so any pulse changes them identically. Leaving them alone with `x = 0` keeps the product at 9.

Constraints

  • 0 <= a, b < 2^50
  • 0 <= n <= 50

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def maximum_xor_product(a: int, b: int, n: int) -> int:
Java
public int maximumXorProduct(long a, long b, int n)
September 7
Apply