#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); ++i)
using ll = long long;
using std::vector;
using std::array;
using std::pair;
using std::tuple;
template <class T> constexpr T infty = std::numeric_limits<T>::max() / 2;
ll dp[10][331][11][11][11];
bool match(const char c, const char d) {
return c == '?' or c == d;
}
ll solve(const int N, const std::string& S, const std::vector<int>& P) {
rep(i, 10) rep(j, 331) rep(k, 11) rep(l, 11) rep(m, 11) dp[i][j][k][l][m] = 0;
rep(k, 11) rep(l, 11) rep(m, 11) dp[0][0][k][l][m] = 1;
rep(round, N - 1) rep(point, 331) rep(k, 11) rep(l, 11) rep(m, 11) {
const ll val = dp[round][point][k][l][m];
if (val == 0) continue;
const auto C = S.begin() + (2 * round);
if (k == 10) {
if (match(C[0], 'x') and match(C[1], '-')) {
const int n_point = point + 10 + l + m;
if (P[round] == -1 or P[round] == n_point) {
rep(n, 11) dp[round + 1][n_point][l][m][n] += val;
}
}
} else {
if (k + l <= 10 and match(C[0], char('0' + k))) {
int n_point = point;
if (k + l == 10) {
if (!match(C[1], '/')) continue;
n_point += 10 + m;
} else {
if (!match(C[1], char('0' + l))) continue;
n_point += k + l;
}
if (P[round] == -1 or P[round] == n_point) {
rep(n, 11) rep(o, 11) dp[round + 1][n_point][m][n][o] += val;
}
}
}
}
const auto C = S.begin() + (2 * N - 2);
ll ret = 0;
rep(point, 331) {
const auto check = [&](const int add) {
return P[N - 1] == -1 or P[N - 1] == point + add;
};
if (match(C[0], 'x')) {
if (match(C[1], 'x')) {
if (match(C[2], 'x')) {
if (check(10 + 10 + 10)) {
ret += dp[N - 1][point][10][10][10];
}
}
rep(m, 10) {
if (match(C[2], char('0' + m)) and check(10 + 10 + m)) {
ret += dp[N - 1][point][10][10][m];
}
}
}
rep(l, 10) {
rep(m, 11) {
if (l + m == 10 and match(C[1], char('0' + l)) and match(C[2], '/') and check(10 + l + m)) {
ret += dp[N - 1][point][10][l][m];
}
if (l + m < 10 and match(C[1], char('0' + l)) and match(C[2], char('0' + m)) and check(10 + l + m)) {
ret += dp[N - 1][point][10][l][m];
}
}
}
}
rep(k, 10) {
rep(l, 11) {
if (k + l == 10 and match(C[0], char('0' + k)) and match(C[1], '/')) {
if (match(C[2], 'x') and check(10 + 10)) {
ret += dp[N - 1][point][k][l][10];
}
rep(m, 10) {
if (match(C[2], char('0' + m)) and check(10 + m)) {
ret += dp[N - 1][point][k][l][m];
}
}
}
if (k + l < 10 and match(C[0], char('0' + k)) and match(C[1], char('0' + l)) and match(C[2], '-') and check(k + l)) {
ret += dp[N - 1][point][k][l][0];
}
}
}
}
return ret;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int Q;
std::cin >> Q;
while (Q--) {
int N;
std::cin >> N;
std::string S;
std::cin >> S;
vector<int> P(N);
for (auto& x : P) {
std::cin >> x;
}
std::cout << solve(N, S, P) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
163 ms |
34788 KB |
Output is correct |
2 |
Correct |
193 ms |
34784 KB |
Output is correct |
3 |
Correct |
168 ms |
34700 KB |
Output is correct |
4 |
Correct |
174 ms |
34784 KB |
Output is correct |
5 |
Correct |
178 ms |
34816 KB |
Output is correct |
6 |
Correct |
183 ms |
34772 KB |
Output is correct |
7 |
Correct |
200 ms |
34772 KB |
Output is correct |
8 |
Correct |
201 ms |
34784 KB |
Output is correct |
9 |
Correct |
178 ms |
34784 KB |
Output is correct |
10 |
Correct |
192 ms |
34784 KB |
Output is correct |
11 |
Correct |
233 ms |
34788 KB |
Output is correct |
12 |
Correct |
153 ms |
34748 KB |
Output is correct |
13 |
Correct |
37 ms |
34772 KB |
Output is correct |
14 |
Correct |
137 ms |
34788 KB |
Output is correct |
15 |
Correct |
46 ms |
34788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
163 ms |
34788 KB |
Output is correct |
2 |
Correct |
197 ms |
34764 KB |
Output is correct |
3 |
Correct |
197 ms |
34788 KB |
Output is correct |
4 |
Correct |
192 ms |
34788 KB |
Output is correct |
5 |
Correct |
193 ms |
34784 KB |
Output is correct |
6 |
Correct |
228 ms |
34772 KB |
Output is correct |
7 |
Correct |
244 ms |
34788 KB |
Output is correct |
8 |
Correct |
248 ms |
34784 KB |
Output is correct |
9 |
Correct |
227 ms |
34784 KB |
Output is correct |
10 |
Correct |
328 ms |
34772 KB |
Output is correct |
11 |
Correct |
311 ms |
34788 KB |
Output is correct |
12 |
Correct |
307 ms |
34892 KB |
Output is correct |
13 |
Correct |
311 ms |
34900 KB |
Output is correct |
14 |
Correct |
236 ms |
34788 KB |
Output is correct |
15 |
Correct |
245 ms |
34796 KB |
Output is correct |
16 |
Correct |
237 ms |
34772 KB |
Output is correct |
17 |
Correct |
243 ms |
34788 KB |
Output is correct |
18 |
Correct |
399 ms |
34796 KB |
Output is correct |
19 |
Correct |
391 ms |
34784 KB |
Output is correct |
20 |
Correct |
425 ms |
34784 KB |
Output is correct |
21 |
Correct |
405 ms |
34804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
34772 KB |
Output is correct |
2 |
Correct |
199 ms |
34788 KB |
Output is correct |
3 |
Correct |
196 ms |
34792 KB |
Output is correct |
4 |
Correct |
187 ms |
34784 KB |
Output is correct |
5 |
Correct |
194 ms |
34772 KB |
Output is correct |
6 |
Correct |
221 ms |
34772 KB |
Output is correct |
7 |
Correct |
223 ms |
34788 KB |
Output is correct |
8 |
Correct |
221 ms |
34772 KB |
Output is correct |
9 |
Correct |
232 ms |
34780 KB |
Output is correct |
10 |
Correct |
240 ms |
34788 KB |
Output is correct |
11 |
Correct |
230 ms |
34788 KB |
Output is correct |
12 |
Correct |
233 ms |
34784 KB |
Output is correct |
13 |
Correct |
237 ms |
34788 KB |
Output is correct |
14 |
Correct |
246 ms |
34796 KB |
Output is correct |
15 |
Correct |
254 ms |
34804 KB |
Output is correct |
16 |
Correct |
232 ms |
34788 KB |
Output is correct |
17 |
Correct |
237 ms |
34792 KB |
Output is correct |
18 |
Correct |
233 ms |
34792 KB |
Output is correct |
19 |
Correct |
227 ms |
34792 KB |
Output is correct |
20 |
Correct |
240 ms |
34728 KB |
Output is correct |
21 |
Correct |
231 ms |
34892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
242 ms |
34784 KB |
Output is correct |
2 |
Correct |
194 ms |
34788 KB |
Output is correct |
3 |
Correct |
223 ms |
34800 KB |
Output is correct |
4 |
Correct |
198 ms |
34784 KB |
Output is correct |
5 |
Correct |
205 ms |
34764 KB |
Output is correct |
6 |
Correct |
190 ms |
34800 KB |
Output is correct |
7 |
Correct |
201 ms |
34716 KB |
Output is correct |
8 |
Correct |
186 ms |
34772 KB |
Output is correct |
9 |
Correct |
186 ms |
34764 KB |
Output is correct |
10 |
Correct |
417 ms |
34792 KB |
Output is correct |
11 |
Correct |
415 ms |
34800 KB |
Output is correct |
12 |
Correct |
385 ms |
34800 KB |
Output is correct |
13 |
Correct |
419 ms |
34784 KB |
Output is correct |
14 |
Correct |
246 ms |
34788 KB |
Output is correct |
15 |
Correct |
253 ms |
34788 KB |
Output is correct |
16 |
Correct |
263 ms |
34784 KB |
Output is correct |
17 |
Correct |
253 ms |
34784 KB |
Output is correct |
18 |
Correct |
222 ms |
34772 KB |
Output is correct |
19 |
Correct |
240 ms |
34796 KB |
Output is correct |
20 |
Correct |
220 ms |
34784 KB |
Output is correct |
21 |
Correct |
218 ms |
34728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
163 ms |
34788 KB |
Output is correct |
2 |
Correct |
193 ms |
34784 KB |
Output is correct |
3 |
Correct |
168 ms |
34700 KB |
Output is correct |
4 |
Correct |
174 ms |
34784 KB |
Output is correct |
5 |
Correct |
178 ms |
34816 KB |
Output is correct |
6 |
Correct |
183 ms |
34772 KB |
Output is correct |
7 |
Correct |
200 ms |
34772 KB |
Output is correct |
8 |
Correct |
201 ms |
34784 KB |
Output is correct |
9 |
Correct |
178 ms |
34784 KB |
Output is correct |
10 |
Correct |
192 ms |
34784 KB |
Output is correct |
11 |
Correct |
233 ms |
34788 KB |
Output is correct |
12 |
Correct |
153 ms |
34748 KB |
Output is correct |
13 |
Correct |
37 ms |
34772 KB |
Output is correct |
14 |
Correct |
137 ms |
34788 KB |
Output is correct |
15 |
Correct |
46 ms |
34788 KB |
Output is correct |
16 |
Correct |
163 ms |
34788 KB |
Output is correct |
17 |
Correct |
197 ms |
34764 KB |
Output is correct |
18 |
Correct |
197 ms |
34788 KB |
Output is correct |
19 |
Correct |
192 ms |
34788 KB |
Output is correct |
20 |
Correct |
193 ms |
34784 KB |
Output is correct |
21 |
Correct |
228 ms |
34772 KB |
Output is correct |
22 |
Correct |
244 ms |
34788 KB |
Output is correct |
23 |
Correct |
248 ms |
34784 KB |
Output is correct |
24 |
Correct |
227 ms |
34784 KB |
Output is correct |
25 |
Correct |
328 ms |
34772 KB |
Output is correct |
26 |
Correct |
311 ms |
34788 KB |
Output is correct |
27 |
Correct |
307 ms |
34892 KB |
Output is correct |
28 |
Correct |
311 ms |
34900 KB |
Output is correct |
29 |
Correct |
236 ms |
34788 KB |
Output is correct |
30 |
Correct |
245 ms |
34796 KB |
Output is correct |
31 |
Correct |
237 ms |
34772 KB |
Output is correct |
32 |
Correct |
243 ms |
34788 KB |
Output is correct |
33 |
Correct |
399 ms |
34796 KB |
Output is correct |
34 |
Correct |
391 ms |
34784 KB |
Output is correct |
35 |
Correct |
425 ms |
34784 KB |
Output is correct |
36 |
Correct |
405 ms |
34804 KB |
Output is correct |
37 |
Correct |
156 ms |
34772 KB |
Output is correct |
38 |
Correct |
199 ms |
34788 KB |
Output is correct |
39 |
Correct |
196 ms |
34792 KB |
Output is correct |
40 |
Correct |
187 ms |
34784 KB |
Output is correct |
41 |
Correct |
194 ms |
34772 KB |
Output is correct |
42 |
Correct |
221 ms |
34772 KB |
Output is correct |
43 |
Correct |
223 ms |
34788 KB |
Output is correct |
44 |
Correct |
221 ms |
34772 KB |
Output is correct |
45 |
Correct |
232 ms |
34780 KB |
Output is correct |
46 |
Correct |
240 ms |
34788 KB |
Output is correct |
47 |
Correct |
230 ms |
34788 KB |
Output is correct |
48 |
Correct |
233 ms |
34784 KB |
Output is correct |
49 |
Correct |
237 ms |
34788 KB |
Output is correct |
50 |
Correct |
246 ms |
34796 KB |
Output is correct |
51 |
Correct |
254 ms |
34804 KB |
Output is correct |
52 |
Correct |
232 ms |
34788 KB |
Output is correct |
53 |
Correct |
237 ms |
34792 KB |
Output is correct |
54 |
Correct |
233 ms |
34792 KB |
Output is correct |
55 |
Correct |
227 ms |
34792 KB |
Output is correct |
56 |
Correct |
240 ms |
34728 KB |
Output is correct |
57 |
Correct |
231 ms |
34892 KB |
Output is correct |
58 |
Correct |
242 ms |
34784 KB |
Output is correct |
59 |
Correct |
194 ms |
34788 KB |
Output is correct |
60 |
Correct |
223 ms |
34800 KB |
Output is correct |
61 |
Correct |
198 ms |
34784 KB |
Output is correct |
62 |
Correct |
205 ms |
34764 KB |
Output is correct |
63 |
Correct |
190 ms |
34800 KB |
Output is correct |
64 |
Correct |
201 ms |
34716 KB |
Output is correct |
65 |
Correct |
186 ms |
34772 KB |
Output is correct |
66 |
Correct |
186 ms |
34764 KB |
Output is correct |
67 |
Correct |
417 ms |
34792 KB |
Output is correct |
68 |
Correct |
415 ms |
34800 KB |
Output is correct |
69 |
Correct |
385 ms |
34800 KB |
Output is correct |
70 |
Correct |
419 ms |
34784 KB |
Output is correct |
71 |
Correct |
246 ms |
34788 KB |
Output is correct |
72 |
Correct |
253 ms |
34788 KB |
Output is correct |
73 |
Correct |
263 ms |
34784 KB |
Output is correct |
74 |
Correct |
253 ms |
34784 KB |
Output is correct |
75 |
Correct |
222 ms |
34772 KB |
Output is correct |
76 |
Correct |
240 ms |
34796 KB |
Output is correct |
77 |
Correct |
220 ms |
34784 KB |
Output is correct |
78 |
Correct |
218 ms |
34728 KB |
Output is correct |
79 |
Correct |
301 ms |
34784 KB |
Output is correct |
80 |
Correct |
220 ms |
34772 KB |
Output is correct |
81 |
Correct |
239 ms |
34800 KB |
Output is correct |
82 |
Correct |
203 ms |
34804 KB |
Output is correct |
83 |
Correct |
192 ms |
34764 KB |
Output is correct |
84 |
Correct |
227 ms |
34772 KB |
Output is correct |
85 |
Correct |
248 ms |
34884 KB |
Output is correct |
86 |
Correct |
219 ms |
34784 KB |
Output is correct |
87 |
Correct |
224 ms |
34772 KB |
Output is correct |
88 |
Correct |
257 ms |
34784 KB |
Output is correct |
89 |
Correct |
315 ms |
34784 KB |
Output is correct |
90 |
Correct |
316 ms |
34800 KB |
Output is correct |
91 |
Correct |
298 ms |
34700 KB |
Output is correct |
92 |
Correct |
230 ms |
34772 KB |
Output is correct |
93 |
Execution timed out |
1085 ms |
34772 KB |
Time limit exceeded |
94 |
Halted |
0 ms |
0 KB |
- |