-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.java
More file actions
43 lines (35 loc) · 957 Bytes
/
solution.java
File metadata and controls
43 lines (35 loc) · 957 Bytes
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
class Fancy {
static final long MOD = 1000000007;
ArrayList<Long> seq = new ArrayList<>();
long mul = 1;
long add = 0;
private long modPow(long a, long b) {
long res = 1;
while (b > 0) {
if ((b & 1) == 1)
res = (res * a) % MOD;
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
public Fancy() {
}
public void append(int val) {
long inv = modPow(mul, MOD - 2);
long stored = ((val - add + MOD) % MOD * inv) % MOD;
seq.add(stored);
}
public void addAll(int inc) {
add = (add + inc) % MOD;
}
public void multAll(int m) {
mul = (mul * m) % MOD;
add = (add * m) % MOD;
}
public int getIndex(int idx) {
if (idx >= seq.size())
return -1;
return (int) ((seq.get(idx) * mul % MOD + add) % MOD);
}
}