#include "bits/stdc++.h"
using namespace std;
const int N = 300;
long long dp[2][N][12][12];
void solve() {
int n;
string s;
cin >> n >> s;
vector<int> res(n);
for (int i = 0; i < n; i++) {
cin >> res[i];
}
// cin >> n;
// s = string(2 * n + 1, '?');
// vector<int> res(n, -1);
int cur = 0;
for (int x = 0; x < N; x++) {
for (int y = 0; y < 12; y++) {
for (int z = 0; z < 12; z++) {
dp[cur ^ 1][x][y][z] = dp[cur][x][y][z] = 0;
}
}
}
dp[cur][0][11][11] = 1;
long long ans = 0;
for (int i = 0; i < n; i++) {
// cout << i << endl;
// cout << dp.size() << endl;
// cout << (*dp.begin()).first[0] << " " << (*dp.rbegin()).first[0] << endl;
vector<pair<array<int, 3>, long long>> dp2;
for (int x = 0; x < N; x++) {
for (int y = 0; y < 12; y++) {
for (int z = 0; z < 12; z++) {
dp[cur ^ 1][x][y][z] = 0;
if (dp[cur][x][y][z]) {
dp2.push_back({{x, y == 11 ? -1 : y, z == 11 ? -1 : z}, dp[cur][x][y][z]});
}
}
}
}
cur ^= 1;
// dp2.clear();
// if (i == 1) exit(0);
if (i + 1 < n) {
for (int x = 0; x <= 10; x++) {
for (int y = 0; x + y <= 10; y++) {
string t = (string) "" + char('0' + x) + char('0' + y);
if (x == 10) {
t = "x-";
} else if (x + y == 10) {
t = (string) "" + char('0' + x) + '/';
}
if (s[i * 2] != '?' && s[i * 2] != t[0]) {
continue;
}
if (s[i * 2 + 1] != '?' && s[i * 2 + 1] != t[1]) {
continue;
}
if (t == "x-") {
for (auto& [_, cnt] : dp2) {
auto [sum, x1, x2] = _;
if (x1 != -1 && x1 != 10) continue;
for (int nsum = 0; nsum <= 30; nsum++) {
if (res[i] != -1 && res[i] != sum + nsum) continue;
for (int x3 = 0; x3 <= 10; x3++) {
int x4 = nsum - 10 - x3;
if (x4 >= 0 && x4 <= 10) {
if (!(x2 != -1 && x2 != x3)) {
dp[cur][sum + nsum][x3][x4] += cnt;
}
}
}
}
}
} else if (t[1] == '/') {
for (auto& [_, cnt] : dp2) {
auto [sum, x1, x2] = _;
if (x1 != -1 && x1 != x) continue;
if (x2 != -1 && x2 != y) continue;
for (int nsum = 10; nsum <= 20; nsum++) {
if (res[i] != -1 && res[i] != sum + nsum) continue;
dp[cur][sum + nsum][nsum - 10][11] += cnt;
}
}
} else {
for (auto& [_, cnt] : dp2) {
auto [sum, x1, x2] = _;
if (x1 != -1 && x1 != x) continue;
if (x2 != -1 && x2 != y) continue;
if (res[i] != -1 && res[i] != sum + x + y) continue;
dp[cur][sum + x + y][11][11] += cnt;
}
}
}
}
} else {
while ((int) s.size() > 3) {
s.erase(s.begin());
}
for (int x = 0; x <= 10; x++) {
for (int y = 0; y <= 10; y++) {
for (int z = 0; z <= 10; z++) {
if (x == 10 && y == 10 && z == 10) {
if (s[0] != '?' && s[0] != 'x') continue;
if (s[1] != '?' && s[1] != 'x') continue;
if (s[2] != '?' && s[2] != 'x') continue;
for (auto& [_, cnt] : dp2) {
auto [sum, x1, x2] = _;
if (x1 != -1 && x1 != x) continue;
if (x2 != -1 && x2 != y) continue;
if (res[i] != -1 && res[i] != sum + 30) continue;
ans += cnt;
}
} else if (x == 10 && y == 10) {
if (s[0] != '?' && s[0] != 'x') continue;
if (s[1] != '?' && s[1] != 'x') continue;
if (s[2] != '?' && s[2] != '0' + z) continue;
for (auto& [_, cnt] : dp2) {
auto [sum, x1, x2] = _;
if (x1 != -1 && x1 != x) continue;
if (x2 != -1 && x2 != y) continue;
if (res[i] != -1 && res[i] != sum + x + y + z) continue;
ans += cnt;
}
} else if (x == 10 && y + z == 10) {
if (s[0] != '?' && s[0] != 'x') continue;
if (s[1] != '?' && s[1] != '0' + y) continue;
if (s[2] != '?' && s[2] != '/') continue;
for (auto& [_, cnt] : dp2) {
auto [sum, x1, x2] = _;
if (x1 != -1 && x1 != x) continue;
if (x2 != -1 && x2 != y) continue;
if (res[i] != -1 && res[i] != sum + x + y + z) continue;
ans += cnt;
}
} else if (x == 10 && y + z < 10) {
if (s[0] != '?' && s[0] != 'x') continue;
if (s[1] != '?' && s[1] != '0' + y) continue;
if (s[2] != '?' && s[2] != '0' + z) continue;
for (auto& [_, cnt] : dp2) {
auto [sum, x1, x2] = _;
if (x1 != -1 && x1 != x) continue;
if (x2 != -1 && x2 != y) continue;
if (res[i] != -1 && res[i] != sum + x + y + z) continue;
ans += cnt;
}
} else if (x + y == 10 && z == 10) {
if (s[0] != '?' && s[0] != '0' + x) continue;
if (s[1] != '?' && s[1] != '/') continue;
if (s[2] != '?' && s[2] != 'x') continue;
for (auto& [_, cnt] : dp2) {
auto [sum, x1, x2] = _;
if (x1 != -1 && x1 != x) continue;
if (x2 != -1 && x2 != y) continue;
if (res[i] != -1 && res[i] != sum + x + y + z) continue;
ans += cnt;
}
} else if (x + y == 10 && z < 10) {
if (s[0] != '?' && s[0] != '0' + x) continue;
if (s[1] != '?' && s[1] != '/') continue;
if (s[2] != '?' && s[2] != '0' + z) continue;
for (auto& [_, cnt] : dp2) {
auto [sum, x1, x2] = _;
if (x1 != -1 && x1 != x) continue;
if (x2 != -1 && x2 != y) continue;
if (res[i] != -1 && res[i] != sum + x + y + z) continue;
ans += cnt;
}
} else if (x + y < 10 && z == 0) {
if (s[0] != '?' && s[0] != '0' + x) continue;
if (s[1] != '?' && s[1] != '0' + y) continue;
if (s[2] != '?' && s[2] != '-') continue;
for (auto& [_, cnt] : dp2) {
auto [sum, x1, x2] = _;
if (x1 != -1 && x1 != x) continue;
if (x2 != -1 && x2 != y) continue;
if (res[i] != -1 && res[i] != sum + x + y + z) continue;
ans += cnt;
}
}
}
}
}
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1360 KB |
Output is correct |
2 |
Correct |
7 ms |
1276 KB |
Output is correct |
3 |
Correct |
7 ms |
1140 KB |
Output is correct |
4 |
Correct |
7 ms |
1104 KB |
Output is correct |
5 |
Correct |
7 ms |
1272 KB |
Output is correct |
6 |
Correct |
8 ms |
1104 KB |
Output is correct |
7 |
Correct |
12 ms |
1388 KB |
Output is correct |
8 |
Correct |
10 ms |
1300 KB |
Output is correct |
9 |
Correct |
8 ms |
1272 KB |
Output is correct |
10 |
Correct |
9 ms |
1428 KB |
Output is correct |
11 |
Correct |
14 ms |
1104 KB |
Output is correct |
12 |
Correct |
5 ms |
976 KB |
Output is correct |
13 |
Correct |
2 ms |
1104 KB |
Output is correct |
14 |
Correct |
5 ms |
1104 KB |
Output is correct |
15 |
Correct |
2 ms |
1204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
1688 KB |
Output is correct |
2 |
Correct |
14 ms |
1672 KB |
Output is correct |
3 |
Correct |
18 ms |
1872 KB |
Output is correct |
4 |
Correct |
15 ms |
1340 KB |
Output is correct |
5 |
Correct |
10 ms |
1400 KB |
Output is correct |
6 |
Correct |
14 ms |
1576 KB |
Output is correct |
7 |
Correct |
16 ms |
1996 KB |
Output is correct |
8 |
Correct |
17 ms |
1424 KB |
Output is correct |
9 |
Correct |
12 ms |
1272 KB |
Output is correct |
10 |
Correct |
39 ms |
1608 KB |
Output is correct |
11 |
Correct |
36 ms |
2328 KB |
Output is correct |
12 |
Correct |
27 ms |
1728 KB |
Output is correct |
13 |
Correct |
33 ms |
2048 KB |
Output is correct |
14 |
Correct |
14 ms |
1360 KB |
Output is correct |
15 |
Correct |
14 ms |
1240 KB |
Output is correct |
16 |
Correct |
20 ms |
1360 KB |
Output is correct |
17 |
Correct |
15 ms |
1496 KB |
Output is correct |
18 |
Correct |
50 ms |
2480 KB |
Output is correct |
19 |
Correct |
47 ms |
2336 KB |
Output is correct |
20 |
Correct |
52 ms |
2272 KB |
Output is correct |
21 |
Correct |
55 ms |
2436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
2532 KB |
Output is correct |
2 |
Correct |
8 ms |
1280 KB |
Output is correct |
3 |
Correct |
7 ms |
1152 KB |
Output is correct |
4 |
Correct |
7 ms |
1104 KB |
Output is correct |
5 |
Correct |
7 ms |
1104 KB |
Output is correct |
6 |
Correct |
10 ms |
1152 KB |
Output is correct |
7 |
Correct |
10 ms |
1136 KB |
Output is correct |
8 |
Correct |
10 ms |
1104 KB |
Output is correct |
9 |
Correct |
12 ms |
1104 KB |
Output is correct |
10 |
Correct |
11 ms |
1156 KB |
Output is correct |
11 |
Correct |
12 ms |
1104 KB |
Output is correct |
12 |
Correct |
12 ms |
1152 KB |
Output is correct |
13 |
Correct |
11 ms |
1104 KB |
Output is correct |
14 |
Correct |
13 ms |
1104 KB |
Output is correct |
15 |
Correct |
12 ms |
1104 KB |
Output is correct |
16 |
Correct |
11 ms |
1104 KB |
Output is correct |
17 |
Correct |
12 ms |
1148 KB |
Output is correct |
18 |
Correct |
14 ms |
1104 KB |
Output is correct |
19 |
Correct |
12 ms |
1104 KB |
Output is correct |
20 |
Correct |
11 ms |
1104 KB |
Output is correct |
21 |
Correct |
11 ms |
1104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
2632 KB |
Output is correct |
2 |
Correct |
11 ms |
1600 KB |
Output is correct |
3 |
Correct |
14 ms |
1936 KB |
Output is correct |
4 |
Correct |
16 ms |
1528 KB |
Output is correct |
5 |
Correct |
12 ms |
1416 KB |
Output is correct |
6 |
Correct |
8 ms |
1104 KB |
Output is correct |
7 |
Correct |
8 ms |
1424 KB |
Output is correct |
8 |
Correct |
7 ms |
1360 KB |
Output is correct |
9 |
Correct |
8 ms |
1360 KB |
Output is correct |
10 |
Correct |
51 ms |
2316 KB |
Output is correct |
11 |
Correct |
66 ms |
2424 KB |
Output is correct |
12 |
Correct |
50 ms |
2440 KB |
Output is correct |
13 |
Correct |
55 ms |
2440 KB |
Output is correct |
14 |
Correct |
17 ms |
1812 KB |
Output is correct |
15 |
Correct |
16 ms |
1800 KB |
Output is correct |
16 |
Correct |
18 ms |
1820 KB |
Output is correct |
17 |
Correct |
15 ms |
1552 KB |
Output is correct |
18 |
Correct |
12 ms |
1104 KB |
Output is correct |
19 |
Correct |
15 ms |
1268 KB |
Output is correct |
20 |
Correct |
12 ms |
1304 KB |
Output is correct |
21 |
Correct |
13 ms |
1192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1360 KB |
Output is correct |
2 |
Correct |
7 ms |
1276 KB |
Output is correct |
3 |
Correct |
7 ms |
1140 KB |
Output is correct |
4 |
Correct |
7 ms |
1104 KB |
Output is correct |
5 |
Correct |
7 ms |
1272 KB |
Output is correct |
6 |
Correct |
8 ms |
1104 KB |
Output is correct |
7 |
Correct |
12 ms |
1388 KB |
Output is correct |
8 |
Correct |
10 ms |
1300 KB |
Output is correct |
9 |
Correct |
8 ms |
1272 KB |
Output is correct |
10 |
Correct |
9 ms |
1428 KB |
Output is correct |
11 |
Correct |
14 ms |
1104 KB |
Output is correct |
12 |
Correct |
5 ms |
976 KB |
Output is correct |
13 |
Correct |
2 ms |
1104 KB |
Output is correct |
14 |
Correct |
5 ms |
1104 KB |
Output is correct |
15 |
Correct |
2 ms |
1204 KB |
Output is correct |
16 |
Correct |
16 ms |
1688 KB |
Output is correct |
17 |
Correct |
14 ms |
1672 KB |
Output is correct |
18 |
Correct |
18 ms |
1872 KB |
Output is correct |
19 |
Correct |
15 ms |
1340 KB |
Output is correct |
20 |
Correct |
10 ms |
1400 KB |
Output is correct |
21 |
Correct |
14 ms |
1576 KB |
Output is correct |
22 |
Correct |
16 ms |
1996 KB |
Output is correct |
23 |
Correct |
17 ms |
1424 KB |
Output is correct |
24 |
Correct |
12 ms |
1272 KB |
Output is correct |
25 |
Correct |
39 ms |
1608 KB |
Output is correct |
26 |
Correct |
36 ms |
2328 KB |
Output is correct |
27 |
Correct |
27 ms |
1728 KB |
Output is correct |
28 |
Correct |
33 ms |
2048 KB |
Output is correct |
29 |
Correct |
14 ms |
1360 KB |
Output is correct |
30 |
Correct |
14 ms |
1240 KB |
Output is correct |
31 |
Correct |
20 ms |
1360 KB |
Output is correct |
32 |
Correct |
15 ms |
1496 KB |
Output is correct |
33 |
Correct |
50 ms |
2480 KB |
Output is correct |
34 |
Correct |
47 ms |
2336 KB |
Output is correct |
35 |
Correct |
52 ms |
2272 KB |
Output is correct |
36 |
Correct |
55 ms |
2436 KB |
Output is correct |
37 |
Correct |
33 ms |
2532 KB |
Output is correct |
38 |
Correct |
8 ms |
1280 KB |
Output is correct |
39 |
Correct |
7 ms |
1152 KB |
Output is correct |
40 |
Correct |
7 ms |
1104 KB |
Output is correct |
41 |
Correct |
7 ms |
1104 KB |
Output is correct |
42 |
Correct |
10 ms |
1152 KB |
Output is correct |
43 |
Correct |
10 ms |
1136 KB |
Output is correct |
44 |
Correct |
10 ms |
1104 KB |
Output is correct |
45 |
Correct |
12 ms |
1104 KB |
Output is correct |
46 |
Correct |
11 ms |
1156 KB |
Output is correct |
47 |
Correct |
12 ms |
1104 KB |
Output is correct |
48 |
Correct |
12 ms |
1152 KB |
Output is correct |
49 |
Correct |
11 ms |
1104 KB |
Output is correct |
50 |
Correct |
13 ms |
1104 KB |
Output is correct |
51 |
Correct |
12 ms |
1104 KB |
Output is correct |
52 |
Correct |
11 ms |
1104 KB |
Output is correct |
53 |
Correct |
12 ms |
1148 KB |
Output is correct |
54 |
Correct |
14 ms |
1104 KB |
Output is correct |
55 |
Correct |
12 ms |
1104 KB |
Output is correct |
56 |
Correct |
11 ms |
1104 KB |
Output is correct |
57 |
Correct |
11 ms |
1104 KB |
Output is correct |
58 |
Correct |
31 ms |
2632 KB |
Output is correct |
59 |
Correct |
11 ms |
1600 KB |
Output is correct |
60 |
Correct |
14 ms |
1936 KB |
Output is correct |
61 |
Correct |
16 ms |
1528 KB |
Output is correct |
62 |
Correct |
12 ms |
1416 KB |
Output is correct |
63 |
Correct |
8 ms |
1104 KB |
Output is correct |
64 |
Correct |
8 ms |
1424 KB |
Output is correct |
65 |
Correct |
7 ms |
1360 KB |
Output is correct |
66 |
Correct |
8 ms |
1360 KB |
Output is correct |
67 |
Correct |
51 ms |
2316 KB |
Output is correct |
68 |
Correct |
66 ms |
2424 KB |
Output is correct |
69 |
Correct |
50 ms |
2440 KB |
Output is correct |
70 |
Correct |
55 ms |
2440 KB |
Output is correct |
71 |
Correct |
17 ms |
1812 KB |
Output is correct |
72 |
Correct |
16 ms |
1800 KB |
Output is correct |
73 |
Correct |
18 ms |
1820 KB |
Output is correct |
74 |
Correct |
15 ms |
1552 KB |
Output is correct |
75 |
Correct |
12 ms |
1104 KB |
Output is correct |
76 |
Correct |
15 ms |
1268 KB |
Output is correct |
77 |
Correct |
12 ms |
1304 KB |
Output is correct |
78 |
Correct |
13 ms |
1192 KB |
Output is correct |
79 |
Correct |
73 ms |
2612 KB |
Output is correct |
80 |
Correct |
19 ms |
1552 KB |
Output is correct |
81 |
Correct |
25 ms |
2252 KB |
Output is correct |
82 |
Correct |
13 ms |
1716 KB |
Output is correct |
83 |
Correct |
14 ms |
1416 KB |
Output is correct |
84 |
Correct |
11 ms |
1148 KB |
Output is correct |
85 |
Correct |
12 ms |
1168 KB |
Output is correct |
86 |
Correct |
11 ms |
1104 KB |
Output is correct |
87 |
Correct |
14 ms |
1136 KB |
Output is correct |
88 |
Correct |
21 ms |
1408 KB |
Output is correct |
89 |
Correct |
34 ms |
1784 KB |
Output is correct |
90 |
Correct |
33 ms |
2248 KB |
Output is correct |
91 |
Correct |
32 ms |
1808 KB |
Output is correct |
92 |
Correct |
16 ms |
1444 KB |
Output is correct |
93 |
Correct |
562 ms |
2600 KB |
Output is correct |
94 |
Correct |
491 ms |
2592 KB |
Output is correct |
95 |
Correct |
491 ms |
2632 KB |
Output is correct |
96 |
Correct |
480 ms |
2636 KB |
Output is correct |
97 |
Correct |
464 ms |
2624 KB |
Output is correct |
98 |
Correct |
485 ms |
2608 KB |
Output is correct |
99 |
Correct |
467 ms |
2596 KB |
Output is correct |
100 |
Correct |
470 ms |
2620 KB |
Output is correct |