You are given a positive integer p. Consider an array nums (1-indexed) that consists of the integers in the inclusive range [1, 2p - 1] in their binary representations. You are allowed to do the following operation
any number of times:
Choose two elements x and y from nums.
Choose a bit in x and swap it with its corresponding bit in y. Corresponding bit refers to the bit that is in the same position in the other integer.
For example, if x = 11 _0_ 1 and y = 00 _1_ 1, after swapping the 2nd
bit from the right, we have x = 11 _1_ 1 and y = 00 _0_ 1.
Find the minimum non-zero product of nums after performing the above operation any number of times. Return this product _modulo _109 + 7.
Note: The answer should be the minimum product before the modulo operation is done.
Input: p =2Output: 6Explanation: nums =[01,10,11].Any swap would either make the product 0 or stay the same.Thus, the array product of 1*2*3=6is already minimized.
Input: p =3Output: 1512Explanation: nums =[001,010,011,100,101,110,111]- In the first operation we can swap the leftmost bit of the second and fifth elements.- The resulting array is[001, _1_ 10,011,100, _0_ 01,110,111].- In the second operation we can swap the middle bit of the third and fourth elements.- The resulting array is[001,110,0 _0_ 1,1 _1_ 0,001,110,111].The array product is1*6*1*6*1*6*7=1512, which is the minimum possible product.
The minimum product is achieved by maximizing the number of elements set to 2^p - 2 and one element set to 2^p - 1. This is because swapping bits can make all but one element as small as possible, and one as large as possible.
classSolution {
public:int minNonZeroProduct(int p) {
longlong MOD =1e9+7;
longlong max_num = (1LL<< p) -1;
longlong second = max_num -1;
longlong cnt = max_num /2;
auto mod_pow = [](longlong a, longlong b, longlong mod) {
longlong res =1;
while (b) {
if (b &1) res = res * a % mod;
a = a * a % mod;
b >>=1;
}
return res;
};
return (mod_pow(second, cnt, MOD) * max_num) % MOD;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
funcminNonZeroProduct(pint) int {
mod:= int(1e9+7)
maxNum:= (1<<p) -1second:=maxNum-1cnt:=maxNum/2modPow:=func(a, b, modint) int {
res:=1forb > 0 {
ifb&1==1 {
res = res*a%mod }
a = a*a%modb>>=1 }
returnres }
returnmodPow(second, cnt, mod) *maxNum%mod}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
publicintminNonZeroProduct(int p) {
long MOD = 1000000007;
long maxNum = (1L << p) - 1;
long second = maxNum - 1;
long cnt = maxNum / 2;
return (int)((modPow(second, cnt, MOD) * maxNum) % MOD);
}
privatelongmodPow(long a, long b, long mod) {
long res = 1;
while (b > 0) {
if ((b & 1) == 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
}
classSolution {
funminNonZeroProduct(p: Int): Int {
val MOD = 1_000_000_007L
val maxNum = (1L shl p) - 1val second = maxNum - 1val cnt = maxNum / 2funmodPow(a: Long, b: Long, mod: Long): Long {
var res = 1Lvar aa = a
var bb = b
while (bb > 0) {
if (bb and 1L==1L) res = res * aa % mod
aa = aa * aa % mod
bb = bb shr 1 }
return res
}
return ((modPow(second, cnt, MOD) * maxNum) % MOD).toInt()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
defminNonZeroProduct(self, p: int) -> int:
MOD: int =10**9+7 max_num: int = (1<< p) -1 second: int = max_num -1 cnt: int = max_num //2defmod_pow(a: int, b: int, mod: int) -> int:
res =1while b:
if b &1:
res = res * a % mod
a = a * a % mod
b >>=1return res
return mod_pow(second, cnt, MOD) * max_num % MOD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
impl Solution {
pubfnmin_non_zero_product(p: i32) -> i32 {
let modv =1_000_000_007;
let max_num = (1u64<< p) -1;
let second = max_num -1;
let cnt = max_num /2;
fnmod_pow(mut a: u64, mut b: u64, modv: u64) -> u64 {
letmut res =1u64;
while b >0 {
if b &1==1 { res = res * a % modv; }
a = a * a % modv;
b >>=1;
}
res
}
((mod_pow(second, cnt, modv) * max_num) % modv) asi32 }
}