-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathd_constructed_combining_classes.cpp
109 lines (95 loc) · 3.05 KB
/
d_constructed_combining_classes.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <string>
#include <memory>
static size_t precision = 7;
std::pair<std::string, std::string> coma_split(const std::string& str) {
size_t balance = 0;
std::string::size_type pos;
for (size_t i = 0; i < str.size(); ++i) {
switch (str[i]) {
case '(':
++balance;
break;
case ')':
--balance;
break;
case ',':
if (balance == 1) {
pos = i;
}
}
}
return {str.substr(2, pos - 2), str.substr(pos + 1, str.size() - pos - 2)};
}
long long fact(long long from, long long amount) {
long long result = 1;
for (long long i = from + 1; i <= amount; ++i) {
result *= i;
}
return result;
}
std::unique_ptr<long long[]> compute(const std::string& expr) {
auto res = std::make_unique<long long[]>(precision);
switch (expr[0]) {
case 'B':
std::fill(res.get(), res.get() + precision, 0);
res[1] = 1;
return std::move(res);
case 'P':
{
auto parts = coma_split(expr);
auto under = compute(parts.first);
auto under_right = compute(parts.second);
for (size_t i = 0; i < precision; ++i) {
res[i] = 0;
for (size_t j = 0; j <= i; ++j) {
res[i] += under[j] * under_right[i - j];
}
}
}
break;
case 'S':
{
auto under = compute(expr.substr(2, expr.size() - 3));
res[0] = 1;
long long matrix[precision][precision];
for (size_t i = 0; i < precision; ++i) {
std::fill(matrix[i], matrix[i] + precision, 0);
matrix[i][0] = 0;
matrix[0][i] = 1;
}
for (size_t i = 1; i < precision; ++i) {
for (size_t j = 1; j < precision; ++j) {
for (size_t k = 0; k <= i / j; ++k) {
long long m = under[j] + k - 1;
m = std::max(m, 0ll);
matrix[i][j] += fact(m - k, m) / fact(1, k) * matrix[i - j * k][j - 1];
}
}
res[i] = matrix[i][i];
}
}
break;
case 'L':
{
auto under = compute(expr.substr(2, expr.size() - 3));
res[0] = 1;
for (size_t i = 1; i < precision; ++i) {
res[i] = 0;
for (size_t j = 1; j <= i; ++j) {
res[i] += under[j] * res[i - j];
}
}
}
}
return res;
}
int main() {
std::string expression;
std::cin >> expression;
auto result = compute(expression);
for (size_t i = 0; i < precision; ++i) {
std::cout << result[i] << ' ';
}
return 0;
}