-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfancySequence.py
More file actions
43 lines (32 loc) · 1.09 KB
/
fancySequence.py
File metadata and controls
43 lines (32 loc) · 1.09 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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/fancy-sequence/
# Author: Miao Zhang
# Date: 2021-05-24
class Fancy:
def __init__(self):
self.mod = 10 ** 9 + 7
self.vals = []
self.a = 1
self.b = 0
def fastModularExpo(self, x: int, y: int) -> int:
return pow(x, y, self.mod)
def multiInverse(self, x: int) -> int:
return self.fastModularExpo(x, self.mod - 2)
def append(self, val: int) -> None:
self.vals.append((val - self.b) * self.multiInverse(self.a) % self.mod)
def addAll(self, inc: int) -> None:
self.b = (self.b + inc) % self.mod
def multAll(self, m: int) -> None:
self.a = (self.a * m) % self.mod
self.b = (self.b * m) % self.mod
def getIndex(self, idx: int) -> int:
if idx >= len(self.vals):
return -1
return (self.a * self.vals[idx] + self.b) % self.mod
# Your Fancy object will be instantiated and called as such:
# obj = Fancy()
# obj.append(val)
# obj.addAll(inc)
# obj.multAll(m)
# param_4 = obj.getIndex(idx)