-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfancySequence.cpp
More file actions
62 lines (52 loc) · 1.32 KB
/
fancySequence.cpp
File metadata and controls
62 lines (52 loc) · 1.32 KB
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
// Source: https://leetcode.com/problems/fancy-sequence/
// Author: Miao Zhang
// Date: 2021-05-24
class Fancy {
public:
Fancy(): a(1), b(0) {
}
int fastModularExpo(int a, int n) {
if (n == 0)
return 1;
else if (n & 1) {
return fastModularExpo(a, n - 1) * a;
} else {
int tmp = fastModularExpo(a, n / 2);
return (long long)tmp * tmp;
}
}
int multiInverse(int x) {
return fastModularExpo(x, kMod - 2);
}
void append(int val) {
vals.push_back((long long) ((val - b + kMod) % kMod) * multiInverse(a) % kMod);
}
void addAll(int inc) {
b = (b + inc) % kMod;
}
void multAll(int m) {
a = (long long) a * m % kMod;
b = (long long) b * m % kMod;
}
int getIndex(int idx) {
if (idx >= vals.size()) {
return -1;
}
cout << "getIndx" << endl;
for (auto v: vals) cout << v << endl;
int res = ((long long) a * vals[idx] % kMod + b) % kMod;
return res;
}
private:
int kMod = 1e9 + 7;
vector<int> vals;
int a, b;
};
/**
* Your Fancy object will be instantiated and called as such:
* Fancy* obj = new Fancy();
* obj->append(val);
* obj->addAll(inc);
* obj->multAll(m);
* int param_4 = obj->getIndex(idx);
*/