-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
count-the-number-of-infection-sequences.cpp
74 lines (68 loc) · 2.21 KB
/
count-the-number-of-infection-sequences.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Time: precompute: O(max_n)
// runtime: O(s + logn)
// Space: O(max_n)
// combinatorics
vector<int> FACT = {1, 1};
vector<int> INV = {1, 1};
vector<int> INV_FACT = {1, 1};
class Solution {
public:
int numberOfSequence(int n, vector<int>& sick) {
int result = 1, total = 0, cnt = 0;
for (int i = 0; i <= size(sick); ++i) {
const int l = (i < size(sick) ? sick[i] : n) - (i - 1 >= 0 ? sick[i - 1] : -1) - 1;
if (i != 0 && i != size(sick)) {
cnt += max(l - 1, 0);
}
total += l;
result = mulmod(result, nCr(total, l));
}
result = mulmod(result, powmod(2, cnt));
return result;
}
private:
int nCr(int n, int k) {
while (size(INV) <= n) { // lazy initialization
FACT.emplace_back(mulmod(FACT.back(), size(INV)));
INV.emplace_back(mulmod(INV[MOD % size(INV)], MOD - MOD / size(INV))); // https://cp-algorithms.com/algebra/module-inverse.html
INV_FACT.emplace_back(mulmod(INV_FACT.back(), INV.back()));
}
return mulmod(mulmod(FACT[n], INV_FACT[n - k]), INV_FACT[k]);
}
uint32_t addmod(uint32_t a, uint32_t b) { // avoid overflow
a %= MOD, b %= MOD;
if (MOD - a <= b) {
b -= MOD; // relied on unsigned integer overflow in order to give the expected results
}
return a + b;
}
// reference: https://stackoverflow.com/questions/12168348/ways-to-do-modulo-multiplication-with-primitive-types
uint32_t mulmod(uint32_t a, uint32_t b) { // avoid overflow
a %= MOD, b %= MOD;
uint32_t result = 0;
if (a < b) {
swap(a, b);
}
while (b) {
if (b & 1) {
result = addmod(result, a);
}
a = addmod(a, a);
b >>= 1;
}
return result;
}
uint32_t powmod(uint32_t a, uint32_t b) {
a %= MOD;
uint32_t result = 1;
while (b) {
if (b & 1) {
result = mulmod(result, a);
}
a = mulmod(a, a);
b >>= 1;
}
return result;
}
static const uint32_t MOD = 1e9 + 7;
};