#include <stdio.h>
#define N 20
#define M 12
#define L 8
#define Q 1000000
#define N_ 531441
int min(int a, int b) { return a < b ? a : b; }
int pp3[M + 1];
void init() {
int m;
pp3[0] = 1;
for (m = 1; m <= M; m++)
pp3[m] = pp3[m - 1] * 3;
}
void solve(char *cc, int *dp, int m) {
int p, i;
if (m == 0) {
dp[0] = cc[0] - '0';
return;
}
p = pp3[m - 1];
solve(cc, dp, m - 1);
solve(cc + (1 << m - 1), dp + p, m - 1);
for (i = 0; i < p; i++)
dp[p * 2 + i] = dp[i] + dp[p + i];
}
int main() {
static char cc[(1 << N) + 1];
static int bb_[Q], lower[Q], upper[Q], ans[Q], dp[N_];
int n, m, l, q, h, i, b;
init();
scanf("%d%d%s", &n, &q, cc), m = min(n, M), l = n - m;
for (h = 0; h < q; h++) {
static char s[N + 1];
scanf("%s", s);
for (i = m - 1; i >= 0; i--)
bb_[h] = bb_[h] * 3 + (s[n - 1 - i] == '?' ? 2 : (s[n - 1 - i] == '0' ? 0 : 1));
for (i = 0; i < l; i++)
if (s[n - 1 - m - i] == '1')
lower[h] |= 1 << i, upper[h] |= 1 << i;
else if (s[n - 1 - m - i] == '?')
upper[h] |= 1 << i;
}
for (b = 0; b < 1 << l; b++) {
solve(cc + (b << m), dp, m);
for (h = 0; h < q; h++)
if ((lower[h] & b) == lower[h] && (upper[h] | b) == upper[h])
ans[h] += dp[bb_[h]];
}
for (h = 0; h < q; h++)
printf("%d\n", ans[h]);
return 0;
}
Compilation message
snake_escaping.c: In function 'solve':
snake_escaping.c:30:21: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
30 | solve(cc + (1 << m - 1), dp + p, m - 1);
| ~~^~~
snake_escaping.c: In function 'main':
snake_escaping.c:41:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | scanf("%d%d%s", &n, &q, cc), m = min(n, M), l = n - m;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.c:45:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf("%s", s);
| ^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
560 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
556 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
560 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
556 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
225 ms |
15772 KB |
Output is correct |
12 |
Correct |
225 ms |
15420 KB |
Output is correct |
13 |
Correct |
198 ms |
14700 KB |
Output is correct |
14 |
Correct |
204 ms |
14860 KB |
Output is correct |
15 |
Correct |
208 ms |
15840 KB |
Output is correct |
16 |
Correct |
209 ms |
14876 KB |
Output is correct |
17 |
Correct |
208 ms |
14864 KB |
Output is correct |
18 |
Correct |
165 ms |
16804 KB |
Output is correct |
19 |
Correct |
164 ms |
13772 KB |
Output is correct |
20 |
Correct |
223 ms |
15444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
560 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
556 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
225 ms |
15772 KB |
Output is correct |
12 |
Correct |
225 ms |
15420 KB |
Output is correct |
13 |
Correct |
198 ms |
14700 KB |
Output is correct |
14 |
Correct |
204 ms |
14860 KB |
Output is correct |
15 |
Correct |
208 ms |
15840 KB |
Output is correct |
16 |
Correct |
209 ms |
14876 KB |
Output is correct |
17 |
Correct |
208 ms |
14864 KB |
Output is correct |
18 |
Correct |
165 ms |
16804 KB |
Output is correct |
19 |
Correct |
164 ms |
13772 KB |
Output is correct |
20 |
Correct |
223 ms |
15444 KB |
Output is correct |
21 |
Correct |
239 ms |
25380 KB |
Output is correct |
22 |
Correct |
272 ms |
21704 KB |
Output is correct |
23 |
Correct |
232 ms |
24644 KB |
Output is correct |
24 |
Correct |
225 ms |
24456 KB |
Output is correct |
25 |
Correct |
236 ms |
26452 KB |
Output is correct |
26 |
Correct |
246 ms |
26612 KB |
Output is correct |
27 |
Correct |
247 ms |
24884 KB |
Output is correct |
28 |
Correct |
191 ms |
24140 KB |
Output is correct |
29 |
Correct |
196 ms |
23112 KB |
Output is correct |
30 |
Correct |
275 ms |
27448 KB |
Output is correct |
31 |
Correct |
242 ms |
28092 KB |
Output is correct |
32 |
Correct |
242 ms |
28372 KB |
Output is correct |
33 |
Correct |
219 ms |
25680 KB |
Output is correct |
34 |
Correct |
247 ms |
24352 KB |
Output is correct |
35 |
Correct |
251 ms |
26408 KB |
Output is correct |
36 |
Correct |
187 ms |
26228 KB |
Output is correct |
37 |
Correct |
234 ms |
24568 KB |
Output is correct |
38 |
Correct |
201 ms |
26520 KB |
Output is correct |
39 |
Correct |
231 ms |
25580 KB |
Output is correct |
40 |
Correct |
229 ms |
27276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
560 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
556 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
117 ms |
6488 KB |
Output is correct |
12 |
Correct |
127 ms |
6284 KB |
Output is correct |
13 |
Correct |
151 ms |
6360 KB |
Output is correct |
14 |
Correct |
133 ms |
6420 KB |
Output is correct |
15 |
Correct |
169 ms |
6492 KB |
Output is correct |
16 |
Correct |
132 ms |
6352 KB |
Output is correct |
17 |
Correct |
140 ms |
6380 KB |
Output is correct |
18 |
Correct |
103 ms |
6412 KB |
Output is correct |
19 |
Correct |
112 ms |
6220 KB |
Output is correct |
20 |
Correct |
115 ms |
6484 KB |
Output is correct |
21 |
Correct |
154 ms |
6476 KB |
Output is correct |
22 |
Correct |
127 ms |
6316 KB |
Output is correct |
23 |
Correct |
110 ms |
6356 KB |
Output is correct |
24 |
Correct |
136 ms |
6312 KB |
Output is correct |
25 |
Correct |
132 ms |
6376 KB |
Output is correct |
26 |
Correct |
98 ms |
6276 KB |
Output is correct |
27 |
Correct |
117 ms |
6232 KB |
Output is correct |
28 |
Correct |
116 ms |
6236 KB |
Output is correct |
29 |
Correct |
112 ms |
6364 KB |
Output is correct |
30 |
Correct |
125 ms |
6376 KB |
Output is correct |
31 |
Correct |
150 ms |
6360 KB |
Output is correct |
32 |
Correct |
144 ms |
6348 KB |
Output is correct |
33 |
Correct |
137 ms |
6376 KB |
Output is correct |
34 |
Correct |
105 ms |
5904 KB |
Output is correct |
35 |
Correct |
162 ms |
6388 KB |
Output is correct |
36 |
Correct |
153 ms |
6356 KB |
Output is correct |
37 |
Correct |
155 ms |
6476 KB |
Output is correct |
38 |
Correct |
152 ms |
6356 KB |
Output is correct |
39 |
Correct |
159 ms |
6356 KB |
Output is correct |
40 |
Correct |
154 ms |
6392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
560 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
556 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
225 ms |
15772 KB |
Output is correct |
12 |
Correct |
225 ms |
15420 KB |
Output is correct |
13 |
Correct |
198 ms |
14700 KB |
Output is correct |
14 |
Correct |
204 ms |
14860 KB |
Output is correct |
15 |
Correct |
208 ms |
15840 KB |
Output is correct |
16 |
Correct |
209 ms |
14876 KB |
Output is correct |
17 |
Correct |
208 ms |
14864 KB |
Output is correct |
18 |
Correct |
165 ms |
16804 KB |
Output is correct |
19 |
Correct |
164 ms |
13772 KB |
Output is correct |
20 |
Correct |
223 ms |
15444 KB |
Output is correct |
21 |
Correct |
239 ms |
25380 KB |
Output is correct |
22 |
Correct |
272 ms |
21704 KB |
Output is correct |
23 |
Correct |
232 ms |
24644 KB |
Output is correct |
24 |
Correct |
225 ms |
24456 KB |
Output is correct |
25 |
Correct |
236 ms |
26452 KB |
Output is correct |
26 |
Correct |
246 ms |
26612 KB |
Output is correct |
27 |
Correct |
247 ms |
24884 KB |
Output is correct |
28 |
Correct |
191 ms |
24140 KB |
Output is correct |
29 |
Correct |
196 ms |
23112 KB |
Output is correct |
30 |
Correct |
275 ms |
27448 KB |
Output is correct |
31 |
Correct |
242 ms |
28092 KB |
Output is correct |
32 |
Correct |
242 ms |
28372 KB |
Output is correct |
33 |
Correct |
219 ms |
25680 KB |
Output is correct |
34 |
Correct |
247 ms |
24352 KB |
Output is correct |
35 |
Correct |
251 ms |
26408 KB |
Output is correct |
36 |
Correct |
187 ms |
26228 KB |
Output is correct |
37 |
Correct |
234 ms |
24568 KB |
Output is correct |
38 |
Correct |
201 ms |
26520 KB |
Output is correct |
39 |
Correct |
231 ms |
25580 KB |
Output is correct |
40 |
Correct |
229 ms |
27276 KB |
Output is correct |
41 |
Correct |
117 ms |
6488 KB |
Output is correct |
42 |
Correct |
127 ms |
6284 KB |
Output is correct |
43 |
Correct |
151 ms |
6360 KB |
Output is correct |
44 |
Correct |
133 ms |
6420 KB |
Output is correct |
45 |
Correct |
169 ms |
6492 KB |
Output is correct |
46 |
Correct |
132 ms |
6352 KB |
Output is correct |
47 |
Correct |
140 ms |
6380 KB |
Output is correct |
48 |
Correct |
103 ms |
6412 KB |
Output is correct |
49 |
Correct |
112 ms |
6220 KB |
Output is correct |
50 |
Correct |
115 ms |
6484 KB |
Output is correct |
51 |
Correct |
154 ms |
6476 KB |
Output is correct |
52 |
Correct |
127 ms |
6316 KB |
Output is correct |
53 |
Correct |
110 ms |
6356 KB |
Output is correct |
54 |
Correct |
136 ms |
6312 KB |
Output is correct |
55 |
Correct |
132 ms |
6376 KB |
Output is correct |
56 |
Correct |
98 ms |
6276 KB |
Output is correct |
57 |
Correct |
117 ms |
6232 KB |
Output is correct |
58 |
Correct |
116 ms |
6236 KB |
Output is correct |
59 |
Correct |
112 ms |
6364 KB |
Output is correct |
60 |
Correct |
125 ms |
6376 KB |
Output is correct |
61 |
Correct |
150 ms |
6360 KB |
Output is correct |
62 |
Correct |
144 ms |
6348 KB |
Output is correct |
63 |
Correct |
137 ms |
6376 KB |
Output is correct |
64 |
Correct |
105 ms |
5904 KB |
Output is correct |
65 |
Correct |
162 ms |
6388 KB |
Output is correct |
66 |
Correct |
153 ms |
6356 KB |
Output is correct |
67 |
Correct |
155 ms |
6476 KB |
Output is correct |
68 |
Correct |
152 ms |
6356 KB |
Output is correct |
69 |
Correct |
159 ms |
6356 KB |
Output is correct |
70 |
Correct |
154 ms |
6392 KB |
Output is correct |
71 |
Correct |
848 ms |
25728 KB |
Output is correct |
72 |
Correct |
911 ms |
20684 KB |
Output is correct |
73 |
Correct |
1399 ms |
23088 KB |
Output is correct |
74 |
Correct |
1269 ms |
23160 KB |
Output is correct |
75 |
Correct |
1912 ms |
25520 KB |
Output is correct |
76 |
Correct |
1200 ms |
24068 KB |
Output is correct |
77 |
Correct |
1276 ms |
25392 KB |
Output is correct |
78 |
Correct |
585 ms |
26188 KB |
Output is correct |
79 |
Correct |
798 ms |
21380 KB |
Output is correct |
80 |
Correct |
873 ms |
25932 KB |
Output is correct |
81 |
Correct |
1575 ms |
26884 KB |
Output is correct |
82 |
Correct |
1189 ms |
23516 KB |
Output is correct |
83 |
Correct |
712 ms |
24084 KB |
Output is correct |
84 |
Correct |
1202 ms |
24272 KB |
Output is correct |
85 |
Correct |
1274 ms |
25748 KB |
Output is correct |
86 |
Correct |
448 ms |
25540 KB |
Output is correct |
87 |
Correct |
882 ms |
22620 KB |
Output is correct |
88 |
Correct |
821 ms |
25332 KB |
Output is correct |
89 |
Correct |
924 ms |
26824 KB |
Output is correct |
90 |
Correct |
1135 ms |
23644 KB |
Output is correct |
91 |
Correct |
1501 ms |
22448 KB |
Output is correct |
92 |
Correct |
1291 ms |
25448 KB |
Output is correct |
93 |
Correct |
1272 ms |
26596 KB |
Output is correct |
94 |
Correct |
564 ms |
14788 KB |
Output is correct |
95 |
Correct |
1604 ms |
24796 KB |
Output is correct |
96 |
Correct |
1610 ms |
24776 KB |
Output is correct |
97 |
Correct |
1598 ms |
25940 KB |
Output is correct |
98 |
Correct |
1596 ms |
25724 KB |
Output is correct |
99 |
Correct |
1607 ms |
25700 KB |
Output is correct |
100 |
Correct |
1596 ms |
24552 KB |
Output is correct |