File tree Expand file tree Collapse file tree 1 file changed +69
-0
lines changed
Design/1622.Fancy-Sequence Expand file tree Collapse file tree 1 file changed +69
-0
lines changed Original file line number Diff line number Diff line change 1+ #define LL long long
2+ LL M = 1e9 +7 ;
3+
4+ class Fancy {
5+ LL add, mul;
6+ vector<LL>nums;
7+
8+ LL quickPow (int x, int y)
9+ {
10+ int ret = 1 ;
11+ int cur = x;
12+ while (y)
13+ {
14+ if (y & 1 )
15+ {
16+ ret = (long long )ret * cur % M;
17+ }
18+ cur = (long long )cur * cur % M;
19+ y >>= 1 ;
20+ }
21+ return ret;
22+ }
23+
24+ LL inv (int x)
25+ {
26+ return quickPow (x, M - 2 );
27+ }
28+
29+ public:
30+ Fancy () {
31+ mul = 1 ;
32+ add = 0 ;
33+ }
34+
35+ void append (int val)
36+ {
37+ val = (val-add+M) % M;
38+ val = (val * inv (mul)) % M;
39+ nums.push_back (val);
40+ }
41+
42+ void addAll (int inc)
43+ {
44+ add = (add + inc) % M;
45+ }
46+
47+ void multAll (int m)
48+ {
49+ mul = (mul*m) % M;
50+ add = (add*m) % M;
51+
52+ }
53+
54+ int getIndex (int idx)
55+ {
56+ if (idx >= nums.size ())
57+ return -1 ;
58+ return (nums[idx]*mul % M + add) % M;
59+ }
60+ };
61+
62+ /* *
63+ * Your Fancy object will be instantiated and called as such:
64+ * Fancy* obj = new Fancy();
65+ * obj->append(val);
66+ * obj->addAll(inc);
67+ * obj->multAll(m);
68+ * int param_4 = obj->getIndex(idx);
69+ */
You can’t perform that action at this time.
0 commit comments