forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-non-zero-product-of-the-array-elements.cpp
67 lines (59 loc) · 2.19 KB
/
minimum-non-zero-product-of-the-array-elements.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Time: O(min(p, logM))
// Space: O(1)
class Solution {
public:
int minNonZeroProduct(int p) {
static const int MOD = 1e9 + 7;
// max_num = 2^p-1
uint32_t max_num_mod = submod(powmod(2, p, MOD), 1, MOD); // max_num % MOD
// pair_num = max_num-1
uint32_t pair_product_mod = submod(max_num_mod, 1, MOD); // (max_num_mod-1) % MOD
// since pair_product^MOD % MOD = pair_product_mod^MOD % MOD = pair_product_mod
// => pair_product_mod^(MOD-1) % MOD = 1
// => pair_product_mod^(pair_cnt) % MOD = pair_product_mod^(pair_cnt%(MOD-1)) %MOD
uint32_t pair_cnt_mod_m_1 = submod(powmod(2, p - 1, MOD - 1), 1, MOD - 1); // pair_cnt%(MOD-1)
// the ans is:
// max_num * pair_num^pair_cnt % MOD
// = max_num_mod * pair_product_mod^(pair_cnt_mod_m_1) % MOD
return mulmod(max_num_mod, powmod(pair_product_mod, pair_cnt_mod_m_1, MOD), MOD);
}
private:
uint32_t addmod(uint32_t a, uint32_t b, uint32_t mod) { // avoid overflow
// a %= mod, b %= mod; // assumed a, b have been mod
if (mod - a <= b) {
b -= mod; // relied on unsigned integer overflow in order to give the expected results
}
return a + b;
}
uint32_t submod(uint32_t a, uint32_t b, uint32_t mod) {
// a %= mod, b %= mod; // assumed a, b have been mod
return addmod(a, mod - b, mod);
}
// reference: https://stackoverflow.com/questions/12168348/ways-to-do-modulo-multiplication-with-primitive-types
uint32_t mulmod(uint32_t a, uint32_t b, uint32_t mod) { // avoid overflow
uint32_t result = 0;
if (a < b) {
swap(a, b);
}
while (b > 0) {
if (b % 2 == 1) {
result = addmod(result, a, mod);
}
a = addmod(a, a, mod);
b /= 2;
}
return result;
}
uint32_t powmod(uint32_t a, uint32_t b, uint32_t mod) {
a %= mod;
uint32_t result = 1;
while (b) {
if (b & 1) {
result = mulmod(result, a, mod);
}
a = mulmod(a, a, mod);
b >>= 1;
}
return result;
}
};