#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int SIZE = 20;
const int MAX = 1 << SIZE;
int n, m, q;
int a[MAX];
string s;
int dp0[MAX], dp1[MAX];
void solve() {
cin >> n >> q;
cin >> s;
m = 1 << n;
FOR (i, 0, m - 1) {
a[i] = s[i] - '0';
dp0[(m - 1) ^ i] = a[i];
dp1[i] = a[i];
}
FOR (i, 0, n - 1) FOR (mask, 0, m - 1) if (mask >> i & 1) {
dp0[mask] += dp0[mask ^ (1 << i)];
dp1[mask] += dp1[mask ^ (1 << i)];
}
while (q--) {
cin >> s;
reverse(s.begin(), s.end());
int cnt[3] = {};
for (char c : s) (c == '0' ? cnt[0] : c == '1' ? cnt[1] : cnt[2])++;
int p;
FOR (i, 0, 2) if (cnt[i] == *min_element(cnt, cnt + 3)) {
p = i;
break;
}
int ans = 0;
if (p == 2) {
auto dfs = [&](auto dfs, int i, int mask)->void {
if (i == n) {
ans += a[mask];
return;
}
if (s[i] == '0' || s[i] == '?') dfs(dfs, i + 1, mask);
if (s[i] == '1' || s[i] == '?') dfs(dfs, i + 1, mask | (1 << i));
};
dfs(dfs, 0, 0);
} else if (p == 0) {
auto dfs = [&](auto dfs, int i, int mask, int cnt)->void {
if (i == n) {
ans += (cnt & 1 ? -dp0[mask] : dp0[mask]);
return;
}
if (s[i] == '0') {
dfs(dfs, i + 1, mask | (1 << i), cnt - 1);
dfs(dfs, i + 1, mask, cnt);
}
if (s[i] == '1') dfs(dfs, i + 1, mask, cnt);
if (s[i] == '?') dfs(dfs, i + 1, mask | (1 << i), cnt);
};
dfs(dfs, 0, 0, cnt[0]);
} else {
auto dfs = [&](auto dfs, int i, int mask, int cnt)->void {
if (i == n) {
ans += (cnt & 1 ? -dp1[mask] : dp1[mask]);
return;
}
if (s[i] == '1') {
dfs(dfs, i + 1, mask | (1 << i), cnt - 1);
dfs(dfs, i + 1, mask, cnt);
}
if (s[i] == '0') dfs(dfs, i + 1, mask, cnt);
if (s[i] == '?') dfs(dfs, i + 1, mask | (1 << i), cnt);
};
dfs(dfs, 0, 0, cnt[1]);
}
cout << ans << '\n';
}
}
int main() {
Waimai;
solve();
}
Compilation message
snake_escaping.cpp: In function 'void solve()':
snake_escaping.cpp:64:16: warning: 'p' may be used uninitialized in this function [-Wmaybe-uninitialized]
64 | } else if (p == 0) {
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
344 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 |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 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 |
344 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 |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
238 ms |
15016 KB |
Output is correct |
12 |
Correct |
277 ms |
14788 KB |
Output is correct |
13 |
Correct |
388 ms |
14064 KB |
Output is correct |
14 |
Correct |
520 ms |
14156 KB |
Output is correct |
15 |
Correct |
322 ms |
14996 KB |
Output is correct |
16 |
Correct |
507 ms |
14272 KB |
Output is correct |
17 |
Correct |
489 ms |
14248 KB |
Output is correct |
18 |
Correct |
195 ms |
16180 KB |
Output is correct |
19 |
Correct |
236 ms |
13060 KB |
Output is correct |
20 |
Correct |
288 ms |
14720 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 |
344 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 |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
238 ms |
15016 KB |
Output is correct |
12 |
Correct |
277 ms |
14788 KB |
Output is correct |
13 |
Correct |
388 ms |
14064 KB |
Output is correct |
14 |
Correct |
520 ms |
14156 KB |
Output is correct |
15 |
Correct |
322 ms |
14996 KB |
Output is correct |
16 |
Correct |
507 ms |
14272 KB |
Output is correct |
17 |
Correct |
489 ms |
14248 KB |
Output is correct |
18 |
Correct |
195 ms |
16180 KB |
Output is correct |
19 |
Correct |
236 ms |
13060 KB |
Output is correct |
20 |
Correct |
288 ms |
14720 KB |
Output is correct |
21 |
Correct |
300 ms |
18044 KB |
Output is correct |
22 |
Correct |
319 ms |
18244 KB |
Output is correct |
23 |
Correct |
645 ms |
17304 KB |
Output is correct |
24 |
Correct |
924 ms |
17056 KB |
Output is correct |
25 |
Correct |
425 ms |
19120 KB |
Output is correct |
26 |
Correct |
835 ms |
17652 KB |
Output is correct |
27 |
Correct |
756 ms |
17580 KB |
Output is correct |
28 |
Correct |
229 ms |
20180 KB |
Output is correct |
29 |
Correct |
318 ms |
16204 KB |
Output is correct |
30 |
Correct |
325 ms |
18280 KB |
Output is correct |
31 |
Correct |
602 ms |
18104 KB |
Output is correct |
32 |
Correct |
913 ms |
18184 KB |
Output is correct |
33 |
Correct |
477 ms |
16936 KB |
Output is correct |
34 |
Correct |
809 ms |
17136 KB |
Output is correct |
35 |
Correct |
753 ms |
17616 KB |
Output is correct |
36 |
Correct |
247 ms |
16092 KB |
Output is correct |
37 |
Correct |
283 ms |
18124 KB |
Output is correct |
38 |
Correct |
390 ms |
16052 KB |
Output is correct |
39 |
Correct |
683 ms |
17284 KB |
Output is correct |
40 |
Correct |
922 ms |
17100 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 |
344 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 |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
48 ms |
16076 KB |
Output is correct |
12 |
Correct |
49 ms |
16016 KB |
Output is correct |
13 |
Correct |
105 ms |
15976 KB |
Output is correct |
14 |
Correct |
173 ms |
16004 KB |
Output is correct |
15 |
Correct |
57 ms |
16072 KB |
Output is correct |
16 |
Correct |
154 ms |
15956 KB |
Output is correct |
17 |
Correct |
152 ms |
15984 KB |
Output is correct |
18 |
Correct |
41 ms |
16192 KB |
Output is correct |
19 |
Correct |
46 ms |
15812 KB |
Output is correct |
20 |
Correct |
53 ms |
16192 KB |
Output is correct |
21 |
Correct |
113 ms |
16072 KB |
Output is correct |
22 |
Correct |
182 ms |
15952 KB |
Output is correct |
23 |
Correct |
63 ms |
15920 KB |
Output is correct |
24 |
Correct |
181 ms |
16080 KB |
Output is correct |
25 |
Correct |
138 ms |
15972 KB |
Output is correct |
26 |
Correct |
42 ms |
15820 KB |
Output is correct |
27 |
Correct |
43 ms |
16064 KB |
Output is correct |
28 |
Correct |
55 ms |
15876 KB |
Output is correct |
29 |
Correct |
101 ms |
15912 KB |
Output is correct |
30 |
Correct |
188 ms |
16012 KB |
Output is correct |
31 |
Correct |
70 ms |
15980 KB |
Output is correct |
32 |
Correct |
182 ms |
15936 KB |
Output is correct |
33 |
Correct |
147 ms |
16024 KB |
Output is correct |
34 |
Correct |
42 ms |
15804 KB |
Output is correct |
35 |
Correct |
106 ms |
15968 KB |
Output is correct |
36 |
Correct |
104 ms |
15992 KB |
Output is correct |
37 |
Correct |
110 ms |
15916 KB |
Output is correct |
38 |
Correct |
107 ms |
15936 KB |
Output is correct |
39 |
Correct |
111 ms |
15980 KB |
Output is correct |
40 |
Correct |
110 ms |
16024 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 |
344 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 |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
238 ms |
15016 KB |
Output is correct |
12 |
Correct |
277 ms |
14788 KB |
Output is correct |
13 |
Correct |
388 ms |
14064 KB |
Output is correct |
14 |
Correct |
520 ms |
14156 KB |
Output is correct |
15 |
Correct |
322 ms |
14996 KB |
Output is correct |
16 |
Correct |
507 ms |
14272 KB |
Output is correct |
17 |
Correct |
489 ms |
14248 KB |
Output is correct |
18 |
Correct |
195 ms |
16180 KB |
Output is correct |
19 |
Correct |
236 ms |
13060 KB |
Output is correct |
20 |
Correct |
288 ms |
14720 KB |
Output is correct |
21 |
Correct |
300 ms |
18044 KB |
Output is correct |
22 |
Correct |
319 ms |
18244 KB |
Output is correct |
23 |
Correct |
645 ms |
17304 KB |
Output is correct |
24 |
Correct |
924 ms |
17056 KB |
Output is correct |
25 |
Correct |
425 ms |
19120 KB |
Output is correct |
26 |
Correct |
835 ms |
17652 KB |
Output is correct |
27 |
Correct |
756 ms |
17580 KB |
Output is correct |
28 |
Correct |
229 ms |
20180 KB |
Output is correct |
29 |
Correct |
318 ms |
16204 KB |
Output is correct |
30 |
Correct |
325 ms |
18280 KB |
Output is correct |
31 |
Correct |
602 ms |
18104 KB |
Output is correct |
32 |
Correct |
913 ms |
18184 KB |
Output is correct |
33 |
Correct |
477 ms |
16936 KB |
Output is correct |
34 |
Correct |
809 ms |
17136 KB |
Output is correct |
35 |
Correct |
753 ms |
17616 KB |
Output is correct |
36 |
Correct |
247 ms |
16092 KB |
Output is correct |
37 |
Correct |
283 ms |
18124 KB |
Output is correct |
38 |
Correct |
390 ms |
16052 KB |
Output is correct |
39 |
Correct |
683 ms |
17284 KB |
Output is correct |
40 |
Correct |
922 ms |
17100 KB |
Output is correct |
41 |
Correct |
48 ms |
16076 KB |
Output is correct |
42 |
Correct |
49 ms |
16016 KB |
Output is correct |
43 |
Correct |
105 ms |
15976 KB |
Output is correct |
44 |
Correct |
173 ms |
16004 KB |
Output is correct |
45 |
Correct |
57 ms |
16072 KB |
Output is correct |
46 |
Correct |
154 ms |
15956 KB |
Output is correct |
47 |
Correct |
152 ms |
15984 KB |
Output is correct |
48 |
Correct |
41 ms |
16192 KB |
Output is correct |
49 |
Correct |
46 ms |
15812 KB |
Output is correct |
50 |
Correct |
53 ms |
16192 KB |
Output is correct |
51 |
Correct |
113 ms |
16072 KB |
Output is correct |
52 |
Correct |
182 ms |
15952 KB |
Output is correct |
53 |
Correct |
63 ms |
15920 KB |
Output is correct |
54 |
Correct |
181 ms |
16080 KB |
Output is correct |
55 |
Correct |
138 ms |
15972 KB |
Output is correct |
56 |
Correct |
42 ms |
15820 KB |
Output is correct |
57 |
Correct |
43 ms |
16064 KB |
Output is correct |
58 |
Correct |
55 ms |
15876 KB |
Output is correct |
59 |
Correct |
101 ms |
15912 KB |
Output is correct |
60 |
Correct |
188 ms |
16012 KB |
Output is correct |
61 |
Correct |
70 ms |
15980 KB |
Output is correct |
62 |
Correct |
182 ms |
15936 KB |
Output is correct |
63 |
Correct |
147 ms |
16024 KB |
Output is correct |
64 |
Correct |
42 ms |
15804 KB |
Output is correct |
65 |
Correct |
106 ms |
15968 KB |
Output is correct |
66 |
Correct |
104 ms |
15992 KB |
Output is correct |
67 |
Correct |
110 ms |
15916 KB |
Output is correct |
68 |
Correct |
107 ms |
15936 KB |
Output is correct |
69 |
Correct |
111 ms |
15980 KB |
Output is correct |
70 |
Correct |
110 ms |
16024 KB |
Output is correct |
71 |
Correct |
459 ms |
26144 KB |
Output is correct |
72 |
Correct |
440 ms |
26256 KB |
Output is correct |
73 |
Correct |
1439 ms |
24420 KB |
Output is correct |
74 |
Execution timed out |
2090 ms |
21996 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |