forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
range-product-queries-of-powers.cpp
60 lines (55 loc) · 1.67 KB
/
range-product-queries-of-powers.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
// Time: O(logn + qlogr), r = MOD
// Space: O(logn)
// prefix sum
class Solution {
public:
vector<int> productQueries(int n, vector<vector<int>>& queries) {
static const int MOD = 1e9 + 7;
vector<int> prefix(1);
for (int i = 0; (1 << i) <= n; ++i) {
if ((1 << i) & n) {
prefix.push_back(prefix.back() + i);
}
}
vector<int> result;
for (const auto& q : queries) {
result.emplace_back(powmod(2, prefix[q[1] + 1] - prefix[q[0]], MOD));
}
return result;
}
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;
}
// 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;
}
};