forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFancy Sequence.cpp
45 lines (41 loc) · 1.04 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
int mod97 = 1000000007;
/**
Calculates multiplicative inverse
*/
unsigned long modPow(unsigned long x, int y) {
unsigned long tot = 1, p = x;
for (; y; y >>= 1) {
if (y & 1)
tot = (tot * p) % mod97;
p = (p * p) % mod97;
}
return tot;
}
class Fancy {
public:
unsigned long seq[100001];
unsigned int length = 0;
unsigned long increment = 0;
unsigned long mult = 1;
Fancy() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
void append(int val) {
seq[length++] = (((mod97 + val - increment)%mod97) * modPow(mult, mod97-2))%mod97;
}
void addAll(int inc) {
increment = (increment+ inc%mod97)%mod97;
}
void multAll(int m) {
mult = (mult* m%mod97)%mod97;
increment = (increment* m%mod97)%mod97;
}
int getIndex(int idx) {
if (idx >= length){
return -1;
}else{
return ((seq[idx] * mult)%mod97+increment)%mod97;
}
}
};