forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfancy-sequence.cpp
167 lines (144 loc) · 4.4 KB
/
fancy-sequence.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Time: O(1)
// Space: O(n)
// #define USE_UINT64_T
class Fancy {
public:
Fancy() : ops_{{1, 0}} {
}
void append(int val) {
arr_.emplace_back(val);
ops_.emplace_back(ops_.back());
}
void addAll(int inc) {
ops_.back().second = addmod(ops_.back().second, inc, MOD);
}
void multAll(int m) {
ops_.back().first = mulmod(ops_.back().first, m, MOD);
ops_.back().second = mulmod(ops_.back().second, m, MOD);
}
int getIndex(int idx) {
if (idx >= size(arr_)) {
return -1;
}
const auto& [a1, b1] = ops_[idx];
const auto& [a2, b2] = ops_.back();
int a = mulmod(a2, powmod(a1, MOD - 2, MOD), MOD);
int b = submod(b2, mulmod(a, b1, MOD), MOD);
return addmod(mulmod(arr_[idx], a, MOD), b, 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
#ifdef USE_UINT64_T
return uint64_t(a) * b % mod; // speed up if we can use uint64_t
#else
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;
#endif
}
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;
}
static const int MOD = 1e9 + 7;
vector<int> arr_;
vector<pair<int, int>> ops_;
};
// Time: O(1)
// Space: O(n)
class Fancy2 {
public:
Fancy2() : op_{1, 0} {
}
void append(int val) {
arr_.emplace_back(mulmod(submod(val, op_.second, MOD), powmod(op_.first, MOD - 2, MOD), MOD));
}
void addAll(int inc) {
op_.second = addmod(op_.second, inc, MOD);
}
void multAll(int m) {
op_.first = mulmod(op_.first, m, MOD);
op_.second = mulmod(op_.second, m, MOD);
}
int getIndex(int idx) {
if (idx >= size(arr_)) {
return -1;
}
return addmod(mulmod(arr_[idx], op_.first, MOD), op_.second, 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
#ifdef USE_UINT64_T
return uint64_t(a) * b % mod; // speed up if we can use uint64_t
#else
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;
#endif
}
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;
}
static const int MOD = 1e9 + 7;
vector<int> arr_;
pair<int, int> op_;
};