#include <bits/stdc++.h>
#define ll long long
#define pii pair<ll, ll>
#define st first
#define nd second
#define file "test"
#define rep(i, begin, end) for (ll i = (begin); i <= (end); i ++)
#define rrep(i, begin, end) for (ll i = (begin); i >= (end); i --)
using namespace std;
const long long INF = 1e18;
const long long N = 2e6 + 5;
int a[N], L, q, n;
ll dp[2][N];
// dp[bit][mask] = sum of mask such bit on = bit
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
//freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout);
#endif
cin >> L >> q;
n = 1 << L;
rep(i, 0, n - 1) {
char ch; cin >> ch;
a[i] = ch - '0';
}
rep(bit, 0, 1) {
rep(mask, 0, n - 1) dp[bit][mask] = a[bit == 0 ? (n - 1 - mask) : mask];
rep(i, 0, L - 1) rep(mask, 0, n - 1)
if (mask >> i & 1)
dp[bit][mask] += dp[bit][mask ^ (1 << i)];
// rep(mask, 0, n - 1)
//cout << bit << ' ' << mask << ": " << dp[bit][mask] << '\n';
}
//return 0;
string s;
while (q --) {
int na = 0, nb = 0, nc = 0, ma = 0, mb = 0, mc = 0;
cin >> s;
reverse(s.begin(), s.end());
rep(i, 0, L - 1) {
if (s[i] == '1') {
na ++; ma ^= 1 << i;
}
if (s[i] == '0') {
nb ++; mb ^= 1 << i;
}
if (s[i] == '?') {
nc ++; mc ^= 1 << i;
}
}
if (nc <= na && nc <= nb) {
ll ans = 0;
for (int i = mc; ; i = (i - 1) & mc) {
ans += a[i ^ ma];
if (i == 0) break;
}
cout << ans << '\n';
} else {
int bit = 1, mask = ma, other = mb;
// if (nb < na) {
// bit = 0;
// mask = mb;
// other = ma;
// }
// if (bit == 0) mc ^= (n - 1) ^ mb;
ll ans = 0;
int bb = __builtin_popcount(mask) & 1;
for (int i = mask; ; i = (i - 1) & mask) {
if ((__builtin_popcount(i) & 1) == bb) {
ans += dp[bit][i ^ mc];
// cout << "-" << (i ^ mc) << ' ' << dp[bit][i^mc] << '\n';
}
else {
ans -= dp[bit][i ^ mc];
// cout << "+" << (i ^ mc) << ' ' << dp[bit][i ^ mc] << '\n';
}
if (i == 0) break;
}
cout << ans << '\n';
}
}
return 0;
}
/** /\_/\
* (= ._.)
* / >🍵 \>🍮
3 case
cnt 0 min
cnt 1 min
cnt ? min
-> handle each case in 2^cnt, cnt <= L / 3 -> O(q * 2 ^ (L / 3));
**/
Compilation message
snake_escaping.cpp:104:9: warning: "/*" within comment [-Wcomment]
104 | /** /\_/\
|
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:74:37: warning: unused variable 'other' [-Wunused-variable]
74 | int bit = 1, mask = ma, other = mb;
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
323 ms |
4364 KB |
Output is correct |
12 |
Correct |
199 ms |
4008 KB |
Output is correct |
13 |
Correct |
210 ms |
3284 KB |
Output is correct |
14 |
Correct |
204 ms |
3280 KB |
Output is correct |
15 |
Correct |
216 ms |
4388 KB |
Output is correct |
16 |
Correct |
222 ms |
3468 KB |
Output is correct |
17 |
Correct |
271 ms |
3500 KB |
Output is correct |
18 |
Correct |
158 ms |
5292 KB |
Output is correct |
19 |
Correct |
182 ms |
2364 KB |
Output is correct |
20 |
Correct |
462 ms |
3972 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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
323 ms |
4364 KB |
Output is correct |
12 |
Correct |
199 ms |
4008 KB |
Output is correct |
13 |
Correct |
210 ms |
3284 KB |
Output is correct |
14 |
Correct |
204 ms |
3280 KB |
Output is correct |
15 |
Correct |
216 ms |
4388 KB |
Output is correct |
16 |
Correct |
222 ms |
3468 KB |
Output is correct |
17 |
Correct |
271 ms |
3500 KB |
Output is correct |
18 |
Correct |
158 ms |
5292 KB |
Output is correct |
19 |
Correct |
182 ms |
2364 KB |
Output is correct |
20 |
Correct |
462 ms |
3972 KB |
Output is correct |
21 |
Correct |
520 ms |
4436 KB |
Output is correct |
22 |
Correct |
231 ms |
4696 KB |
Output is correct |
23 |
Correct |
252 ms |
3660 KB |
Output is correct |
24 |
Correct |
247 ms |
3424 KB |
Output is correct |
25 |
Correct |
243 ms |
5488 KB |
Output is correct |
26 |
Correct |
276 ms |
3912 KB |
Output is correct |
27 |
Correct |
355 ms |
3864 KB |
Output is correct |
28 |
Correct |
170 ms |
6732 KB |
Output is correct |
29 |
Correct |
222 ms |
2968 KB |
Output is correct |
30 |
Correct |
1163 ms |
5148 KB |
Output is correct |
31 |
Correct |
328 ms |
5072 KB |
Output is correct |
32 |
Correct |
315 ms |
4928 KB |
Output is correct |
33 |
Correct |
853 ms |
3924 KB |
Output is correct |
34 |
Correct |
392 ms |
4068 KB |
Output is correct |
35 |
Correct |
375 ms |
4456 KB |
Output is correct |
36 |
Correct |
143 ms |
2892 KB |
Output is correct |
37 |
Correct |
239 ms |
4988 KB |
Output is correct |
38 |
Correct |
230 ms |
2936 KB |
Output is correct |
39 |
Correct |
439 ms |
4332 KB |
Output is correct |
40 |
Correct |
277 ms |
4520 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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
379 ms |
21592 KB |
Output is correct |
12 |
Correct |
76 ms |
21372 KB |
Output is correct |
13 |
Correct |
95 ms |
21476 KB |
Output is correct |
14 |
Correct |
121 ms |
21408 KB |
Output is correct |
15 |
Correct |
106 ms |
21464 KB |
Output is correct |
16 |
Correct |
108 ms |
21352 KB |
Output is correct |
17 |
Correct |
181 ms |
21392 KB |
Output is correct |
18 |
Correct |
79 ms |
22360 KB |
Output is correct |
19 |
Correct |
83 ms |
22872 KB |
Output is correct |
20 |
Correct |
1018 ms |
23140 KB |
Output is correct |
21 |
Correct |
117 ms |
23152 KB |
Output is correct |
22 |
Correct |
163 ms |
23072 KB |
Output is correct |
23 |
Correct |
1466 ms |
23076 KB |
Output is correct |
24 |
Correct |
170 ms |
22984 KB |
Output is correct |
25 |
Correct |
153 ms |
23040 KB |
Output is correct |
26 |
Correct |
74 ms |
22976 KB |
Output is correct |
27 |
Correct |
77 ms |
23128 KB |
Output is correct |
28 |
Correct |
81 ms |
22956 KB |
Output is correct |
29 |
Correct |
264 ms |
23224 KB |
Output is correct |
30 |
Correct |
90 ms |
23028 KB |
Output is correct |
31 |
Correct |
81 ms |
22988 KB |
Output is correct |
32 |
Correct |
112 ms |
23000 KB |
Output is correct |
33 |
Correct |
148 ms |
23000 KB |
Output is correct |
34 |
Correct |
71 ms |
22992 KB |
Output is correct |
35 |
Correct |
211 ms |
22996 KB |
Output is correct |
36 |
Correct |
212 ms |
22992 KB |
Output is correct |
37 |
Correct |
220 ms |
23056 KB |
Output is correct |
38 |
Correct |
231 ms |
23076 KB |
Output is correct |
39 |
Correct |
208 ms |
22980 KB |
Output is correct |
40 |
Correct |
220 ms |
23040 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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
323 ms |
4364 KB |
Output is correct |
12 |
Correct |
199 ms |
4008 KB |
Output is correct |
13 |
Correct |
210 ms |
3284 KB |
Output is correct |
14 |
Correct |
204 ms |
3280 KB |
Output is correct |
15 |
Correct |
216 ms |
4388 KB |
Output is correct |
16 |
Correct |
222 ms |
3468 KB |
Output is correct |
17 |
Correct |
271 ms |
3500 KB |
Output is correct |
18 |
Correct |
158 ms |
5292 KB |
Output is correct |
19 |
Correct |
182 ms |
2364 KB |
Output is correct |
20 |
Correct |
462 ms |
3972 KB |
Output is correct |
21 |
Correct |
520 ms |
4436 KB |
Output is correct |
22 |
Correct |
231 ms |
4696 KB |
Output is correct |
23 |
Correct |
252 ms |
3660 KB |
Output is correct |
24 |
Correct |
247 ms |
3424 KB |
Output is correct |
25 |
Correct |
243 ms |
5488 KB |
Output is correct |
26 |
Correct |
276 ms |
3912 KB |
Output is correct |
27 |
Correct |
355 ms |
3864 KB |
Output is correct |
28 |
Correct |
170 ms |
6732 KB |
Output is correct |
29 |
Correct |
222 ms |
2968 KB |
Output is correct |
30 |
Correct |
1163 ms |
5148 KB |
Output is correct |
31 |
Correct |
328 ms |
5072 KB |
Output is correct |
32 |
Correct |
315 ms |
4928 KB |
Output is correct |
33 |
Correct |
853 ms |
3924 KB |
Output is correct |
34 |
Correct |
392 ms |
4068 KB |
Output is correct |
35 |
Correct |
375 ms |
4456 KB |
Output is correct |
36 |
Correct |
143 ms |
2892 KB |
Output is correct |
37 |
Correct |
239 ms |
4988 KB |
Output is correct |
38 |
Correct |
230 ms |
2936 KB |
Output is correct |
39 |
Correct |
439 ms |
4332 KB |
Output is correct |
40 |
Correct |
277 ms |
4520 KB |
Output is correct |
41 |
Correct |
379 ms |
21592 KB |
Output is correct |
42 |
Correct |
76 ms |
21372 KB |
Output is correct |
43 |
Correct |
95 ms |
21476 KB |
Output is correct |
44 |
Correct |
121 ms |
21408 KB |
Output is correct |
45 |
Correct |
106 ms |
21464 KB |
Output is correct |
46 |
Correct |
108 ms |
21352 KB |
Output is correct |
47 |
Correct |
181 ms |
21392 KB |
Output is correct |
48 |
Correct |
79 ms |
22360 KB |
Output is correct |
49 |
Correct |
83 ms |
22872 KB |
Output is correct |
50 |
Correct |
1018 ms |
23140 KB |
Output is correct |
51 |
Correct |
117 ms |
23152 KB |
Output is correct |
52 |
Correct |
163 ms |
23072 KB |
Output is correct |
53 |
Correct |
1466 ms |
23076 KB |
Output is correct |
54 |
Correct |
170 ms |
22984 KB |
Output is correct |
55 |
Correct |
153 ms |
23040 KB |
Output is correct |
56 |
Correct |
74 ms |
22976 KB |
Output is correct |
57 |
Correct |
77 ms |
23128 KB |
Output is correct |
58 |
Correct |
81 ms |
22956 KB |
Output is correct |
59 |
Correct |
264 ms |
23224 KB |
Output is correct |
60 |
Correct |
90 ms |
23028 KB |
Output is correct |
61 |
Correct |
81 ms |
22988 KB |
Output is correct |
62 |
Correct |
112 ms |
23000 KB |
Output is correct |
63 |
Correct |
148 ms |
23000 KB |
Output is correct |
64 |
Correct |
71 ms |
22992 KB |
Output is correct |
65 |
Correct |
211 ms |
22996 KB |
Output is correct |
66 |
Correct |
212 ms |
22992 KB |
Output is correct |
67 |
Correct |
220 ms |
23056 KB |
Output is correct |
68 |
Correct |
231 ms |
23076 KB |
Output is correct |
69 |
Correct |
208 ms |
22980 KB |
Output is correct |
70 |
Correct |
220 ms |
23040 KB |
Output is correct |
71 |
Execution timed out |
2078 ms |
30660 KB |
Time limit exceeded |
72 |
Halted |
0 ms |
0 KB |
- |