#include <stdio.h>
#include <string.h>
#define N 10
#define S (N * 30)
int ss[N];
int ok(int i, int s) {
return i < 0 || ss[i] == -1 || ss[i] == s;
}
int eq(char a, char b) {
return a == '?' || a == b;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
static char cc[N * 2 + 2];
static long long dp[N][S + 1][4];
int n, i, s, a, b;
long long ans;
scanf("%d%s", &n, cc);
for (i = 0; i < n; i++)
scanf("%d", &ss[i]);
for (i = 0; i < n; i++)
memset(dp[i], 0, sizeof dp[i]);
dp[0][0][0] = 1;
for (i = 0; i < n - 1; i++)
for (s = 0; s <= S; s++) {
long long x;
if ((x = dp[i][s][0]) > 0 && ok(i - 1, s)) {
for (a = 0; a < 10; a++)
if (eq(cc[i * 2], a + '0'))
for (b = 0; a + b < 10; b++)
if (eq(cc[i * 2 + 1], b + '0'))
dp[i + 1][s + a + b][0] += x;
if (eq(cc[i * 2 + 1], '/'))
for (a = 0; a < 10; a++)
if (eq(cc[i * 2], a + '0'))
dp[i + 1][s + 10][1] += x;
if (eq(cc[i * 2], 'x') && eq(cc[i * 2 + 1], '-'))
dp[i + 1][s + 10][2] += x;
}
if ((x = dp[i][s][1]) > 0) {
for (a = 0; a < 10; a++)
if (eq(cc[i * 2], a + '0') && ok(i - 1, s + a))
for (b = 0; a + b < 10; b++)
if (eq(cc[i * 2 + 1], b + '0'))
dp[i + 1][s + a + a + b][0] += x;
if (eq(cc[i * 2 + 1], '/'))
for (a = 0; a < 10; a++)
if (eq(cc[i * 2], a + '0') && ok(i - 1, s + a))
dp[i + 1][s + a + 10][1] += x;
if (eq(cc[i * 2], 'x') && eq(cc[i * 2 + 1], '-') && ok(i - 1, s + 10))
dp[i + 1][s + 10 + 10][2] += x;
}
if ((x = dp[i][s][2]) > 0) {
for (a = 0; a < 10; a++)
if (eq(cc[i * 2], a + '0'))
for (b = 0; a + b < 10; b++)
if (eq(cc[i * 2 + 1], b + '0') && ok(i - 1, s + a + b))
dp[i + 1][s + a + b + a + b][0] += x;
if (eq(cc[i * 2 + 1], '/') && ok(i - 1, s + 10))
for (a = 0; a < 10; a++)
if (eq(cc[i * 2], a + '0'))
dp[i + 1][s + 10 + 10][1] += x;
if (eq(cc[i * 2], 'x') && eq(cc[i * 2 + 1], '-'))
dp[i + 1][s + 10][3] += x;
}
if ((x = dp[i][s][3]) > 0) {
for (a = 0; a < 10; a++)
if (eq(cc[i * 2], a + '0') && ok(i - 2, s + a))
for (b = 0; a + b < 10; b++)
if (eq(cc[i * 2 + 1], b + '0') && ok(i - 1, s + a + 10 + a + b))
dp[i + 1][s + a + 10 + a + b + a + b][0] += x;
if (eq(cc[i * 2 + 1], '/'))
for (a = 0; a < 10; a++)
if (eq(cc[i * 2], a + '0') && ok(i - 2, s + a) && ok(i - 1, s + a + 10 + 10))
dp[i + 1][s + a + 10 + 10 + 10][1] += x;
if (eq(cc[i * 2], 'x') && eq(cc[i * 2 + 1], '-') && ok(i - 2, s + 10))
dp[i + 1][s + 10 + 10 + 10][3] += x;
}
}
ans = 0;
i = n - 1;
for (s = 0; s <= S; s++) {
long long x;
if ((x = dp[i][s][0]) > 0 && ok(i - 1, s)) {
for (a = 0; a < 10; a++)
if (eq(cc[i * 2], a + '0'))
for (b = 0; a + b < 10; b++)
if (eq(cc[i * 2 + 1], b + '0') && eq(cc[i * 2 + 2], '-') && ok(i, s + a + b))
ans += x;
if (eq(cc[i * 2 + 1], '/'))
for (a = 0; a < 10; a++)
if (eq(cc[i * 2], a + '0')) {
for (b = 0; b < 10; b++)
if (eq(cc[i * 2 + 2], b + '0') && ok(i, s + 10 + b))
ans += x;
if (eq(cc[i * 2 + 2], 'x') && ok(i, s + 10 + 10))
ans += x;
}
if (eq(cc[i * 2], 'x')) {
for (a = 0; a < 10; a++)
if (eq(cc[i * 2 + 1], a + '0'))
for (b = 0; a + b < 10; b++)
if (eq(cc[i * 2 + 2], b + '0') && ok(i, s + 10 + a + b))
ans += x;
for (a = 0; a < 10; a++)
if (eq(cc[i * 2 + 1], a + '0') && eq(cc[i * 2 + 2], '/') && ok(i, s + 10 + 10))
ans += x;
if (eq(cc[i * 2 + 1], 'x')) {
for (a = 0; a < 10; a++)
if (eq(cc[i * 2 + 2], a + '0') && ok(i, s + 10 + 10 + a))
ans += x;
if (eq(cc[i * 2 + 2], 'x') && ok(i, s + 10 + 10 + 10))
ans += x;
}
}
}
if ((x = dp[i][s][1]) > 0) {
for (a = 0; a < 10; a++)
if (eq(cc[i * 2], a + '0') && ok(i - 1, s + a))
for (b = 0; a + b < 10; b++)
if (eq(cc[i * 2 + 1], b + '0') && eq(cc[i * 2 + 2], '-') && ok(i, s + a + a + b))
ans += x;
if (eq(cc[i * 2 + 1], '/'))
for (a = 0; a < 10; a++)
if (eq(cc[i * 2], a + '0') && ok(i - 1, s + a)) {
for (b = 0; b < 10; b++)
if (eq(cc[i * 2 + 2], b + '0') && ok(i, s + a + 10 + b))
ans += x;
if (eq(cc[i * 2 + 2], 'x') && ok(i, s + a + 10 + 10))
ans += x;
}
if (eq(cc[i * 2], 'x') && ok(i - 1, s + 10)) {
for (a = 0; a < 10; a++)
if (eq(cc[i * 2 + 1], a + '0'))
for (b = 0; a + b < 10; b++)
if (eq(cc[i * 2 + 2], b + '0') && ok(i, s + 10 + 10 + a + b))
ans += x;
for (a = 0; a < 10; a++)
if (eq(cc[i * 2 + 1], a + '0') && eq(cc[i * 2 + 2], '/') && ok(i, s + 10 + 10 + 10))
ans += x;
if (eq(cc[i * 2 + 1], 'x')) {
for (a = 0; a < 10; a++)
if (eq(cc[i * 2 + 2], a + '0') && ok(i, s + 10 + 10 + 10 + a))
ans += x;
if (eq(cc[i * 2 + 2], 'x') && ok(i, s + 10 + 10 + 10 + 10))
ans += x;
}
}
}
if ((x = dp[i][s][2]) > 0) {
for (a = 0; a < 10; a++)
if (eq(cc[i * 2], a + '0'))
for (b = 0; a + b < 10; b++)
if (eq(cc[i * 2 + 1], b + '0') && eq(cc[i * 2 + 2], '-') && ok(i - 1, s + a + b) && ok(i, s + a + b + a + b))
ans += x;
if (eq(cc[i * 2 + 1], '/') && ok(i - 1, s + 10))
for (a = 0; a < 10; a++)
if (eq(cc[i * 2], a + '0')) {
for (b = 0; b < 10; b++)
if (eq(cc[i * 2 + 2], b + '0') && ok(i, s + 10 + 10 + b))
ans += x;
if (eq(cc[i * 2 + 2], 'x') && ok(i, s + 10 + 10 + 10))
ans += x;
}
if (eq(cc[i * 2], 'x')) {
for (a = 0; a < 10; a++)
if (eq(cc[i * 2 + 1], a + '0') && ok(i - 1, s + 10 + a))
for (b = 0; a + b < 10; b++)
if (eq(cc[i * 2 + 2], b + '0') && ok(i, s + 10 + a + 10 + a + b))
ans += x;
for (a = 0; a < 10; a++)
if (eq(cc[i * 2 + 1], a + '0') && eq(cc[i * 2 + 2], '/') && ok(i - 1, s + 10 + a) && ok(i, s + 10 + a + 10 + 10))
ans += x;
if (eq(cc[i * 2 + 1], 'x') && ok(i - 1, s + 10 + 10)) {
for (a = 0; a < 10; a++)
if (eq(cc[i * 2 + 2], a + '0') && ok(i, s + 10 + 10 + 10 + 10 + a))
ans += x;
if (eq(cc[i * 2 + 2], 'x') && ok(i, s + 10 + 10 + 10 + 10 + 10))
ans += x;
}
}
}
if ((x = dp[i][s][3]) > 0) {
for (a = 0; a < 10; a++)
if (eq(cc[i * 2], a + '0') && ok(i - 2, s + a))
for (b = 0; a + b < 10; b++)
if (eq(cc[i * 2 + 1], b + '0') && eq(cc[i * 2 + 2], '-') && ok(i - 1, s + a + 10 + a + b) && ok(i, s + a + 10 + a + b + a + b))
ans += x;
if (eq(cc[i * 2 + 1], '/'))
for (a = 0; a < 10; a++)
if (eq(cc[i * 2], a + '0') && ok(i - 2, s + a) && ok(i - 1, s + a + 10 + 10)) {
for (b = 0; b < 10; b++)
if (eq(cc[i * 2 + 2], b + '0') && ok(i, s + a + 10 + 10 + 10 + b))
ans += x;
if (eq(cc[i * 2 + 2], 'x') && ok(i, s + a + 10 + 10 + 10 + 10))
ans += x;
}
if (eq(cc[i * 2], 'x') && ok(i - 2, s + 10)) {
for (a = 0; a < 10; a++)
if (eq(cc[i * 2 + 1], a + '0') && ok(i - 1, s + 10 + 10 + 10 + a))
for (b = 0; a + b < 10; b++)
if (eq(cc[i * 2 + 2], b + '0') && ok(i, s + 10 + 10 + 10 + a + 10 + a + b))
ans += x;
for (a = 0; a < 10; a++)
if (eq(cc[i * 2 + 1], a + '0') && eq(cc[i * 2 + 2], '/') && ok(i - 1, s + 10 + 10 + 10 + a) && ok(i, s + 10 + 10 + 10 + a + 10 + 10))
ans += x;
if (eq(cc[i * 2 + 1], 'x') && ok(i - 1, s + 10 + 10 + 10 + 10)) {
for (a = 0; a < 10; a++)
if (eq(cc[i * 2 + 2], a + '0') && ok(i, s + 10 + 10 + 10 + 10 + 10 + 10 + a))
ans += x;
if (eq(cc[i * 2 + 2], 'x') && ok(i, s + 10 + 10 + 10 + 10 + 10 + 10 + 10))
ans += x;
}
}
}
}
printf("%lld\n", ans);
}
return 0;
}
Compilation message
bow.c: In function 'main':
bow.c:20:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | scanf("%d", &t);
| ^~~~~~~~~~~~~~~
bow.c:27:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | scanf("%d%s", &n, cc);
| ^~~~~~~~~~~~~~~~~~~~~
bow.c:29:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | scanf("%d", &ss[i]);
| ^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
296 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
4 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
4 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
340 KB |
Output is correct |
20 |
Correct |
4 ms |
340 KB |
Output is correct |
21 |
Correct |
5 ms |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
280 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
296 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
296 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
292 KB |
Output is correct |
24 |
Correct |
1 ms |
292 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
3 ms |
296 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
4 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
4 ms |
340 KB |
Output is correct |
34 |
Correct |
3 ms |
340 KB |
Output is correct |
35 |
Correct |
4 ms |
340 KB |
Output is correct |
36 |
Correct |
5 ms |
296 KB |
Output is correct |
37 |
Correct |
2 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
292 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
2 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
280 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
292 KB |
Output is correct |
58 |
Correct |
2 ms |
296 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
296 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
296 KB |
Output is correct |
64 |
Correct |
1 ms |
340 KB |
Output is correct |
65 |
Correct |
1 ms |
340 KB |
Output is correct |
66 |
Correct |
1 ms |
296 KB |
Output is correct |
67 |
Correct |
2 ms |
340 KB |
Output is correct |
68 |
Correct |
2 ms |
340 KB |
Output is correct |
69 |
Correct |
3 ms |
340 KB |
Output is correct |
70 |
Correct |
2 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
340 KB |
Output is correct |
72 |
Correct |
2 ms |
340 KB |
Output is correct |
73 |
Correct |
1 ms |
340 KB |
Output is correct |
74 |
Correct |
1 ms |
340 KB |
Output is correct |
75 |
Correct |
1 ms |
296 KB |
Output is correct |
76 |
Correct |
1 ms |
340 KB |
Output is correct |
77 |
Correct |
1 ms |
340 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
4 ms |
340 KB |
Output is correct |
80 |
Correct |
2 ms |
340 KB |
Output is correct |
81 |
Correct |
2 ms |
296 KB |
Output is correct |
82 |
Correct |
2 ms |
340 KB |
Output is correct |
83 |
Correct |
1 ms |
340 KB |
Output is correct |
84 |
Correct |
1 ms |
296 KB |
Output is correct |
85 |
Correct |
1 ms |
340 KB |
Output is correct |
86 |
Correct |
1 ms |
300 KB |
Output is correct |
87 |
Correct |
1 ms |
340 KB |
Output is correct |
88 |
Correct |
2 ms |
340 KB |
Output is correct |
89 |
Correct |
3 ms |
340 KB |
Output is correct |
90 |
Correct |
3 ms |
292 KB |
Output is correct |
91 |
Correct |
3 ms |
340 KB |
Output is correct |
92 |
Correct |
1 ms |
340 KB |
Output is correct |
93 |
Correct |
24 ms |
340 KB |
Output is correct |
94 |
Correct |
24 ms |
340 KB |
Output is correct |
95 |
Correct |
23 ms |
340 KB |
Output is correct |
96 |
Correct |
25 ms |
388 KB |
Output is correct |
97 |
Correct |
33 ms |
340 KB |
Output is correct |
98 |
Correct |
24 ms |
340 KB |
Output is correct |
99 |
Correct |
23 ms |
340 KB |
Output is correct |
100 |
Correct |
23 ms |
384 KB |
Output is correct |