-
Notifications
You must be signed in to change notification settings - Fork 0
/
gigcombinatorics.cpp
58 lines (48 loc) · 1.03 KB
/
gigcombinatorics.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX 1000001
int N;
char nums[MAX];
int memo[MAX];
const ll MOD = 1e9 + 7;
inline void mult(ll& a, ll b) { a = (a * b) % MOD; }
inline void add(ll& a, ll b) { a = (a + b) % MOD; }
int dp(int n) {
if (n == N) return 0;
if (~memo[n]) return memo[n];
ll total = (ll)dp(n + 1);
switch (nums[n]) {
case '2':
mult(total, 2);
break;
case '3':
total++;
break;
}
return memo[n] = (int)total;
}
int main() {
while (scanf("%d", &N) == 1) {
memset(memo, -1, sizeof(int) * N);
for (int i = 0; i < N; i++) {
char c = '\0';
// Read '1', '2', '3' while skipping spaces.
while (!(c & 16)) c = getchar_unlocked();
nums[i] = c;
}
ll total = 0;
ll ones = 0;
for (int i = 0; i < N; i++) {
switch (nums[i]) {
case '1':
ones++;
break;
case '2':
add(total, ones * dp(i + 1));
break;
}
}
printf("%lld\n", total % MOD);
}
}