#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define all(v) begin(v), end(v)
using ll = long long;
void solve();
int main() {
int q; cin >> q;
rep (_, q) solve();
}
void solve() {
int n; cin >> n;
string s; cin >> s;
vector<int> a(n);
rep (i, n) cin >> a[i];
vector dp(n, vector(301, vector(11, vector(11, 0ll))));
rep (i, 11) rep (j, 11) dp[0][0][i][j] = 1;
rep (i, n - 1) {
string t = s.substr(i * 2, 2);
rep (j, 301) rep (k, 11) rep (l, 11) {
if (dp[i][j][k][l] == 0) continue;
if (k == 10) {
if ((t[0] == 'x' || t[0] == '?') && (t[1] == '-' || t[1] == '?')) {
rep (m, 11) {
if (a[i] == -1 || a[i] == j + 10 + l + m) dp[i + 1][j + 10 + l + m][l][m] += dp[i][j][k][l];
}
}
}
else if (k + l <= 10) {
if (t[0] == 'x' || t[1] == '-') continue;
if (k + l == 10) {
if (t[1] == '/' || t[1] == '?') {
if (t[0] != '?' && t[0] - '0' != k) continue;
rep (m, 11) {
if (a[i] == -1 || a[i] == j + 10 + m) {
rep (n, 11) dp[i + 1][j + 10 + m][m][n] += dp[i][j][k][l];
}
}
}
}
else {
if (t[1] == '/') continue;
if (t[0] != '?' && t[0] - '0' != k) continue;
if (t[1] != '?' && t[1] - '0' != l) continue;
if (a[i] == -1 || a[i] == j + k + l) {
rep (m, 11) rep (n, 11) dp[i + 1][j + k + l][m][n] += dp[i][j][k][l];
}
}
}
}
}
ll ans = 0;
string t = s.substr((n - 1) * 2);
rep (s, 301) {
do {
if (t[0] != '?' && t[0] != 'x') continue;
if (t[1] != '?' && t[1] != 'x') continue;
if (t[2] != '?' && t[2] != 'x') continue;
if (a[n - 1] == -1 || a[n - 1] == s + 30) ans += dp[n - 1][s][10][10];
} while (0);
rep (i, 10) {
if (t[0] != '?' && t[0] != 'x') continue;
if (t[1] != '?' && t[1] != 'x') continue;
if (t[2] != '?' && t[2] - '0' != i) continue;
if (a[n - 1] == -1 || a[n - 1] == s + 20 + i) ans += dp[n - 1][s][10][10];
}
rep (i, 10) {
if (t[0] != '?' && t[0] != 'x') continue;
if (t[1] != '?' && t[1] - '0' != i) continue;
if (t[2] != '?' && t[2] != '/') continue;
if (a[n - 1] == -1 || a[n - 1] == s + 20) ans += dp[n - 1][s][10][i];
}
rep (i, 10) rep (j, 10) {
if (i + j >= 10) break;
if (t[0] != '?' && t[0] != 'x') continue;
if (t[1] != '?' && t[1] - '0' != i) continue;
if (t[2] != '?' && t[2] - '0' != j) continue;
if (a[n - 1] == -1 || a[n - 1] == s + 10 + i + j) ans += dp[n - 1][s][10][i];
}
rep (i, 10) {
if (t[0] != '?' && t[0] - '0' != i) continue;
if (t[1] != '?' && t[1] != '/') continue;
if (t[2] != '?' && t[2] != 'x') continue;
if (a[n - 1] == -1 || a[n - 1] == s + 20) ans += dp[n - 1][s][i][10 - i];
}
rep (i, 10) rep (j, 10) {
if (t[0] != '?' && t[0] - '0' != i) continue;
if (t[1] != '?' && t[1] != '/') continue;
if (t[2] != '?' && t[2] - '0' != j) continue;
if (a[n - 1] == -1 || a[n - 1] == s + 10 + j) ans += dp[n - 1][s][i][10 - i];
}
rep (i, 10) rep (j, 10) {
if (i + j >= 10) break;
if (t[0] != '?' && t[0] - '0' != i) continue;
if (t[1] != '?' && t[1] - '0' != j) continue;
if (t[2] != '?' && t[2] != '-') continue;
if (a[n - 1] == -1 || a[n - 1] == s + i + j) ans += dp[n - 1][s][i][j];
}
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
4696 KB |
Output is correct |
2 |
Correct |
26 ms |
4188 KB |
Output is correct |
3 |
Correct |
27 ms |
4508 KB |
Output is correct |
4 |
Correct |
29 ms |
4692 KB |
Output is correct |
5 |
Correct |
29 ms |
4700 KB |
Output is correct |
6 |
Correct |
31 ms |
4632 KB |
Output is correct |
7 |
Correct |
41 ms |
4700 KB |
Output is correct |
8 |
Correct |
33 ms |
4700 KB |
Output is correct |
9 |
Correct |
29 ms |
4700 KB |
Output is correct |
10 |
Correct |
32 ms |
4696 KB |
Output is correct |
11 |
Correct |
46 ms |
4696 KB |
Output is correct |
12 |
Correct |
14 ms |
1628 KB |
Output is correct |
13 |
Correct |
3 ms |
1628 KB |
Output is correct |
14 |
Correct |
14 ms |
1456 KB |
Output is correct |
15 |
Correct |
4 ms |
1628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
4700 KB |
Output is correct |
2 |
Correct |
31 ms |
4700 KB |
Output is correct |
3 |
Correct |
31 ms |
4700 KB |
Output is correct |
4 |
Correct |
33 ms |
4700 KB |
Output is correct |
5 |
Correct |
33 ms |
4920 KB |
Output is correct |
6 |
Correct |
45 ms |
4696 KB |
Output is correct |
7 |
Correct |
46 ms |
4700 KB |
Output is correct |
8 |
Correct |
46 ms |
4900 KB |
Output is correct |
9 |
Correct |
44 ms |
4700 KB |
Output is correct |
10 |
Correct |
51 ms |
4700 KB |
Output is correct |
11 |
Correct |
53 ms |
4696 KB |
Output is correct |
12 |
Correct |
49 ms |
4700 KB |
Output is correct |
13 |
Correct |
52 ms |
4700 KB |
Output is correct |
14 |
Correct |
56 ms |
4700 KB |
Output is correct |
15 |
Correct |
48 ms |
4700 KB |
Output is correct |
16 |
Correct |
47 ms |
4864 KB |
Output is correct |
17 |
Correct |
52 ms |
4700 KB |
Output is correct |
18 |
Correct |
60 ms |
4696 KB |
Output is correct |
19 |
Correct |
60 ms |
4700 KB |
Output is correct |
20 |
Correct |
68 ms |
4700 KB |
Output is correct |
21 |
Correct |
60 ms |
4696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
4700 KB |
Output is correct |
2 |
Correct |
33 ms |
4696 KB |
Output is correct |
3 |
Correct |
31 ms |
4700 KB |
Output is correct |
4 |
Correct |
30 ms |
4700 KB |
Output is correct |
5 |
Correct |
28 ms |
4700 KB |
Output is correct |
6 |
Correct |
56 ms |
4696 KB |
Output is correct |
7 |
Correct |
43 ms |
4696 KB |
Output is correct |
8 |
Correct |
44 ms |
4700 KB |
Output is correct |
9 |
Correct |
44 ms |
4696 KB |
Output is correct |
10 |
Correct |
46 ms |
4700 KB |
Output is correct |
11 |
Correct |
48 ms |
4696 KB |
Output is correct |
12 |
Correct |
51 ms |
4696 KB |
Output is correct |
13 |
Correct |
46 ms |
4700 KB |
Output is correct |
14 |
Correct |
47 ms |
4700 KB |
Output is correct |
15 |
Correct |
48 ms |
4696 KB |
Output is correct |
16 |
Correct |
48 ms |
4700 KB |
Output is correct |
17 |
Correct |
46 ms |
4696 KB |
Output is correct |
18 |
Correct |
50 ms |
4956 KB |
Output is correct |
19 |
Correct |
45 ms |
4828 KB |
Output is correct |
20 |
Correct |
46 ms |
4696 KB |
Output is correct |
21 |
Correct |
46 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
4700 KB |
Output is correct |
2 |
Correct |
28 ms |
4696 KB |
Output is correct |
3 |
Correct |
39 ms |
4956 KB |
Output is correct |
4 |
Correct |
29 ms |
4700 KB |
Output is correct |
5 |
Correct |
28 ms |
4428 KB |
Output is correct |
6 |
Correct |
31 ms |
4700 KB |
Output is correct |
7 |
Correct |
28 ms |
4700 KB |
Output is correct |
8 |
Correct |
28 ms |
4700 KB |
Output is correct |
9 |
Correct |
29 ms |
4700 KB |
Output is correct |
10 |
Correct |
49 ms |
4696 KB |
Output is correct |
11 |
Correct |
50 ms |
4700 KB |
Output is correct |
12 |
Correct |
46 ms |
4696 KB |
Output is correct |
13 |
Correct |
47 ms |
4700 KB |
Output is correct |
14 |
Correct |
45 ms |
4700 KB |
Output is correct |
15 |
Correct |
46 ms |
4700 KB |
Output is correct |
16 |
Correct |
45 ms |
4700 KB |
Output is correct |
17 |
Correct |
45 ms |
4900 KB |
Output is correct |
18 |
Correct |
43 ms |
4700 KB |
Output is correct |
19 |
Correct |
47 ms |
4956 KB |
Output is correct |
20 |
Correct |
45 ms |
4700 KB |
Output is correct |
21 |
Correct |
43 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
4696 KB |
Output is correct |
2 |
Correct |
26 ms |
4188 KB |
Output is correct |
3 |
Correct |
27 ms |
4508 KB |
Output is correct |
4 |
Correct |
29 ms |
4692 KB |
Output is correct |
5 |
Correct |
29 ms |
4700 KB |
Output is correct |
6 |
Correct |
31 ms |
4632 KB |
Output is correct |
7 |
Correct |
41 ms |
4700 KB |
Output is correct |
8 |
Correct |
33 ms |
4700 KB |
Output is correct |
9 |
Correct |
29 ms |
4700 KB |
Output is correct |
10 |
Correct |
32 ms |
4696 KB |
Output is correct |
11 |
Correct |
46 ms |
4696 KB |
Output is correct |
12 |
Correct |
14 ms |
1628 KB |
Output is correct |
13 |
Correct |
3 ms |
1628 KB |
Output is correct |
14 |
Correct |
14 ms |
1456 KB |
Output is correct |
15 |
Correct |
4 ms |
1628 KB |
Output is correct |
16 |
Correct |
24 ms |
4700 KB |
Output is correct |
17 |
Correct |
31 ms |
4700 KB |
Output is correct |
18 |
Correct |
31 ms |
4700 KB |
Output is correct |
19 |
Correct |
33 ms |
4700 KB |
Output is correct |
20 |
Correct |
33 ms |
4920 KB |
Output is correct |
21 |
Correct |
45 ms |
4696 KB |
Output is correct |
22 |
Correct |
46 ms |
4700 KB |
Output is correct |
23 |
Correct |
46 ms |
4900 KB |
Output is correct |
24 |
Correct |
44 ms |
4700 KB |
Output is correct |
25 |
Correct |
51 ms |
4700 KB |
Output is correct |
26 |
Correct |
53 ms |
4696 KB |
Output is correct |
27 |
Correct |
49 ms |
4700 KB |
Output is correct |
28 |
Correct |
52 ms |
4700 KB |
Output is correct |
29 |
Correct |
56 ms |
4700 KB |
Output is correct |
30 |
Correct |
48 ms |
4700 KB |
Output is correct |
31 |
Correct |
47 ms |
4864 KB |
Output is correct |
32 |
Correct |
52 ms |
4700 KB |
Output is correct |
33 |
Correct |
60 ms |
4696 KB |
Output is correct |
34 |
Correct |
60 ms |
4700 KB |
Output is correct |
35 |
Correct |
68 ms |
4700 KB |
Output is correct |
36 |
Correct |
60 ms |
4696 KB |
Output is correct |
37 |
Correct |
20 ms |
4700 KB |
Output is correct |
38 |
Correct |
33 ms |
4696 KB |
Output is correct |
39 |
Correct |
31 ms |
4700 KB |
Output is correct |
40 |
Correct |
30 ms |
4700 KB |
Output is correct |
41 |
Correct |
28 ms |
4700 KB |
Output is correct |
42 |
Correct |
56 ms |
4696 KB |
Output is correct |
43 |
Correct |
43 ms |
4696 KB |
Output is correct |
44 |
Correct |
44 ms |
4700 KB |
Output is correct |
45 |
Correct |
44 ms |
4696 KB |
Output is correct |
46 |
Correct |
46 ms |
4700 KB |
Output is correct |
47 |
Correct |
48 ms |
4696 KB |
Output is correct |
48 |
Correct |
51 ms |
4696 KB |
Output is correct |
49 |
Correct |
46 ms |
4700 KB |
Output is correct |
50 |
Correct |
47 ms |
4700 KB |
Output is correct |
51 |
Correct |
48 ms |
4696 KB |
Output is correct |
52 |
Correct |
48 ms |
4700 KB |
Output is correct |
53 |
Correct |
46 ms |
4696 KB |
Output is correct |
54 |
Correct |
50 ms |
4956 KB |
Output is correct |
55 |
Correct |
45 ms |
4828 KB |
Output is correct |
56 |
Correct |
46 ms |
4696 KB |
Output is correct |
57 |
Correct |
46 ms |
4700 KB |
Output is correct |
58 |
Correct |
33 ms |
4700 KB |
Output is correct |
59 |
Correct |
28 ms |
4696 KB |
Output is correct |
60 |
Correct |
39 ms |
4956 KB |
Output is correct |
61 |
Correct |
29 ms |
4700 KB |
Output is correct |
62 |
Correct |
28 ms |
4428 KB |
Output is correct |
63 |
Correct |
31 ms |
4700 KB |
Output is correct |
64 |
Correct |
28 ms |
4700 KB |
Output is correct |
65 |
Correct |
28 ms |
4700 KB |
Output is correct |
66 |
Correct |
29 ms |
4700 KB |
Output is correct |
67 |
Correct |
49 ms |
4696 KB |
Output is correct |
68 |
Correct |
50 ms |
4700 KB |
Output is correct |
69 |
Correct |
46 ms |
4696 KB |
Output is correct |
70 |
Correct |
47 ms |
4700 KB |
Output is correct |
71 |
Correct |
45 ms |
4700 KB |
Output is correct |
72 |
Correct |
46 ms |
4700 KB |
Output is correct |
73 |
Correct |
45 ms |
4700 KB |
Output is correct |
74 |
Correct |
45 ms |
4900 KB |
Output is correct |
75 |
Correct |
43 ms |
4700 KB |
Output is correct |
76 |
Correct |
47 ms |
4956 KB |
Output is correct |
77 |
Correct |
45 ms |
4700 KB |
Output is correct |
78 |
Correct |
43 ms |
4700 KB |
Output is correct |
79 |
Correct |
33 ms |
4700 KB |
Output is correct |
80 |
Correct |
35 ms |
4700 KB |
Output is correct |
81 |
Correct |
34 ms |
4700 KB |
Output is correct |
82 |
Correct |
34 ms |
4700 KB |
Output is correct |
83 |
Correct |
31 ms |
4700 KB |
Output is correct |
84 |
Correct |
49 ms |
4700 KB |
Output is correct |
85 |
Correct |
44 ms |
4700 KB |
Output is correct |
86 |
Correct |
44 ms |
4696 KB |
Output is correct |
87 |
Correct |
44 ms |
4700 KB |
Output is correct |
88 |
Correct |
48 ms |
4816 KB |
Output is correct |
89 |
Correct |
51 ms |
4696 KB |
Output is correct |
90 |
Correct |
50 ms |
4700 KB |
Output is correct |
91 |
Correct |
51 ms |
4696 KB |
Output is correct |
92 |
Correct |
44 ms |
4696 KB |
Output is correct |
93 |
Correct |
150 ms |
4696 KB |
Output is correct |
94 |
Correct |
153 ms |
4700 KB |
Output is correct |
95 |
Correct |
147 ms |
4700 KB |
Output is correct |
96 |
Correct |
147 ms |
4696 KB |
Output is correct |
97 |
Correct |
146 ms |
4800 KB |
Output is correct |
98 |
Correct |
151 ms |
4700 KB |
Output is correct |
99 |
Correct |
148 ms |
4812 KB |
Output is correct |
100 |
Correct |
146 ms |
4700 KB |
Output is correct |