-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathaerolite.cpp
82 lines (82 loc) · 3.13 KB
/
aerolite.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
75
76
77
78
79
80
81
82
// 2023-12-25
#include <iostream>
#include <string.h>
using namespace std;
int memo[11][11][11][31];
constexpr int MOD = 11380;
int calc(int i, int j, int k, int l) {
int& result = memo[i][j][k][l];
if (result >= 0) return result;
result = 0;
if (l == 0) {
if (i + j + k == 0) {
result = 1;
}
return result;
}
for (int d = 1; d <= l; d++) {
// Let `d` be the depth of the first piece, which shall be a valid
// string enclosed in a pair of (some kind of) brackets. The remainder
// can be empty.
for (int i1 = 0; i1 <= i; i1++) {
for (int j1 = 0; j1 <= j; j1++) {
for (int k1 = 0; k1 <= k; k1++) {
if (i1 + j1 + k1 == 0) continue;
const int i2 = i - i1;
const int j2 = j - j1;
const int k2 = k - k1;
if (i1 > 0) {
// the first piece can be enclosed in {}
result += calc(i1 - 1, j1, k1, d - 1) *
calc(i2, j2, k2, l);
result %= MOD;
// If the first piece uses the full depth, then the
// remainder of the string can have any depth.
if (d == l) {
for (int d2 = 0; d2 < l; d2++) {
result += calc(i1 - 1, j1, k1, d - 1) *
calc(i2, j2, k2, d2);
result %= MOD;
}
}
}
if (j1 > 0 && i1 == 0) {
// the first piece can be enclosed in []
result += calc(i1, j1 - 1, k1, d - 1) *
calc(i2, j2, k2, l);
result %= MOD;
if (d == l) {
for (int d2 = 0; d2 < l; d2++) {
result += calc(i1, j1 - 1, k1, d - 1) *
calc(i2, j2, k2, d2);
result %= MOD;
}
}
}
if (k1 > 0 && i1 == 0 && j1 == 0) {
// the first piece can be enclosed in ()
result += calc(i1, j1, k1 - 1, d - 1) *
calc(i2, j2, k2, l);
result %= MOD;
if (d == l) {
for (int d2 = 0; d2 < l; d2++) {
result += calc(i1, j1, k1 - 1, d - 1) *
calc(i2, j2, k2, d2);
result %= MOD;
}
}
}
}
}
}
}
return result;
}
int main() {
memset(memo, -1, sizeof(memo));
for (int i = 0; i < 10; i++) {
int L1, L2, L3, D;
cin >> L1 >> L2 >> L3 >> D;
cout << calc(L1, L2, L3, D) << '\n';
}
}