Fancy Sequence LeetCode solution in C++

1
int mod97 = 1000000007;
2
/**
3
Calculates multiplicative inverse
4
*/
5
unsigned long modPow(unsigned long x, int y) {
6
unsigned long tot = 1, p = x;
7
for (; y; y >>= 1) {
8
if (y & 1) tot = (tot * p) % mod97;
9
p = (p * p) % mod97;
10
}
11
return tot;
12
}
13
class Fancy {
14
public:
15
unsigned long seq[100001];
16
unsigned int length = 0;
17
unsigned long increment = 0;
18
unsigned long mult = 1;
19
Fancy() {
20
ios_base::sync_with_stdio(false);
21
cin.tie(NULL);
22
}
23

24
void append(int val) {
25
seq[length++] = (((mod97 + val - increment) % mod97) * modPow(mult, mod97 - 2)) % mod97;
26
}
27
void addAll(int inc) {
28
increment = (increment + inc % mod97) % mod97;
29
}
30

31
void multAll(int m) {
32
mult = (mult * m % mod97) % mod97;
33
increment = (increment * m % mod97) % mod97;
34
}
35

36
int getIndex(int idx) {
37
if (idx >= length) {
38
return -1;
39
} else {
40
return ((seq[idx] * mult) % mod97 + increment) % mod97;
41
}
42
}
43
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0