// opt
#include<bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define ii pair<int, int>
#define iii pair<ii, int>
#define ll long long
#define pb push_back
#define pli pair<ll, int>
#define all(v) (v).begin(), (v).end()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define cntbit(mask) __builtin_popcount(mask)
#define getbit(x, i) ((x >> i) & 1)
#define MASK(i) (1 << i)
#define pli pair<ll, int>
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return 1;} return 0;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return 1;} return 0;}
const int N = 1e5 + 5;
const int V = 1e6 + 5;
const int MOD = 1e9 + 7;
int n, q, cost[MASK(20) + 5], f[MASK(20) + 5];
string s;
void solve() {
cin >> n >> q;
cin >> s;
for(int i = 0 ; i < (1 << n) ; i++) f[i] = cost[i] = (s[i] - '0');
for(int i = 0 ; i < n ; i++)
for(int mask = 0 ; mask < MASK(n) ; mask++)
if (getbit(mask, i)) cost[mask] += cost[mask ^ MASK(i)];
for(int i = 0 ; i < n ; i++)
for(int mask = 0 ; mask < MASK(n) ; mask++)
if (!getbit(mask, i)) f[mask] += f[mask | MASK(i)];
for(int i = 0 ; i < q ; i++) {
string t; cin >> t;
int mask1, mask2, mask3, sz = (int) t.size() - 1;
ll ans;
mask1 = mask2 = mask3 = ans = 0;
for(int i = sz ; i >= 0 ; i--)
if (t[i] == '1') mask1 |= MASK(sz - i);
else if (t[i] == '?') mask2 |= MASK(sz - i);
else mask3 |= MASK(sz - i);
if (cntbit(mask1) <= 6) {
int c = cntbit(mask1) & 1;
for(int submask = mask1 ; submask >= 0 ; submask = (submask - 1) & mask1) {
if ((cntbit(submask) & 1) == c) ans += cost[submask + mask2];
else ans -= cost[submask + mask2];
if (submask == 0) break;
}
} else if (cntbit(mask2) <= 6) {
for(int submask = mask2 ; submask >= 0 ; submask = (submask - 1) & mask2) {
ans += (s[submask + mask1] - '0');
if (submask == 0) break;
}
} else {
for(int submask = mask3 ; submask >= 0 ; submask = (submask - 1) & mask3) {
if (cntbit(submask) & 1) ans -= f[submask + mask1];
else ans += f[submask + mask1];
if (submask == 0) break;
}
}
cout << ans << '\n';
}
}
int main(void) {
//freopen("task.inp", "r", stdin);
//freopen("task.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1; while(t--) solve();
return 0;
}
Compilation message
snake_escaping.cpp: In function 'void solve()':
snake_escaping.cpp:42:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
42 | for(int i = 0 ; i < n ; i++)
| ^~~
snake_escaping.cpp:46:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
46 | for(int i = 0 ; i < q ; i++) {
| ^~~
snake_escaping.cpp:53:38: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
53 | if (t[i] == '1') mask1 |= MASK(sz - i);
| ~~~^~~
snake_escaping.cpp:15:23: note: in definition of macro 'MASK'
15 | #define MASK(i) (1 << i)
| ^
snake_escaping.cpp:54:43: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
54 | else if (t[i] == '?') mask2 |= MASK(sz - i);
| ~~~^~~
snake_escaping.cpp:15:23: note: in definition of macro 'MASK'
15 | #define MASK(i) (1 << i)
| ^
snake_escaping.cpp:55:35: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
55 | else mask3 |= MASK(sz - i);
| ~~~^~~
snake_escaping.cpp:15:23: note: in definition of macro 'MASK'
15 | #define MASK(i) (1 << i)
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
231 ms |
15132 KB |
Output is correct |
12 |
Correct |
159 ms |
14888 KB |
Output is correct |
13 |
Correct |
178 ms |
14156 KB |
Output is correct |
14 |
Correct |
173 ms |
14164 KB |
Output is correct |
15 |
Correct |
157 ms |
14988 KB |
Output is correct |
16 |
Correct |
205 ms |
14156 KB |
Output is correct |
17 |
Correct |
208 ms |
14164 KB |
Output is correct |
18 |
Correct |
117 ms |
16208 KB |
Output is correct |
19 |
Correct |
207 ms |
13000 KB |
Output is correct |
20 |
Correct |
207 ms |
14652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
231 ms |
15132 KB |
Output is correct |
12 |
Correct |
159 ms |
14888 KB |
Output is correct |
13 |
Correct |
178 ms |
14156 KB |
Output is correct |
14 |
Correct |
173 ms |
14164 KB |
Output is correct |
15 |
Correct |
157 ms |
14988 KB |
Output is correct |
16 |
Correct |
205 ms |
14156 KB |
Output is correct |
17 |
Correct |
208 ms |
14164 KB |
Output is correct |
18 |
Correct |
117 ms |
16208 KB |
Output is correct |
19 |
Correct |
207 ms |
13000 KB |
Output is correct |
20 |
Correct |
207 ms |
14652 KB |
Output is correct |
21 |
Correct |
324 ms |
18148 KB |
Output is correct |
22 |
Correct |
164 ms |
18340 KB |
Output is correct |
23 |
Correct |
220 ms |
17364 KB |
Output is correct |
24 |
Correct |
207 ms |
17236 KB |
Output is correct |
25 |
Correct |
176 ms |
19028 KB |
Output is correct |
26 |
Correct |
245 ms |
17492 KB |
Output is correct |
27 |
Correct |
242 ms |
17648 KB |
Output is correct |
28 |
Correct |
123 ms |
20052 KB |
Output is correct |
29 |
Correct |
142 ms |
16040 KB |
Output is correct |
30 |
Correct |
266 ms |
18304 KB |
Output is correct |
31 |
Correct |
218 ms |
18192 KB |
Output is correct |
32 |
Correct |
232 ms |
18004 KB |
Output is correct |
33 |
Correct |
187 ms |
16976 KB |
Output is correct |
34 |
Correct |
251 ms |
17208 KB |
Output is correct |
35 |
Correct |
259 ms |
17488 KB |
Output is correct |
36 |
Correct |
121 ms |
16208 KB |
Output is correct |
37 |
Correct |
157 ms |
18004 KB |
Output is correct |
38 |
Correct |
229 ms |
16212 KB |
Output is correct |
39 |
Correct |
345 ms |
17236 KB |
Output is correct |
40 |
Correct |
255 ms |
17176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
45 ms |
12020 KB |
Output is correct |
12 |
Correct |
45 ms |
12108 KB |
Output is correct |
13 |
Correct |
50 ms |
11844 KB |
Output is correct |
14 |
Correct |
59 ms |
11848 KB |
Output is correct |
15 |
Correct |
47 ms |
12016 KB |
Output is correct |
16 |
Correct |
56 ms |
12016 KB |
Output is correct |
17 |
Correct |
55 ms |
12000 KB |
Output is correct |
18 |
Correct |
41 ms |
12020 KB |
Output is correct |
19 |
Correct |
44 ms |
11768 KB |
Output is correct |
20 |
Correct |
46 ms |
12104 KB |
Output is correct |
21 |
Correct |
56 ms |
12016 KB |
Output is correct |
22 |
Correct |
61 ms |
12016 KB |
Output is correct |
23 |
Correct |
46 ms |
11896 KB |
Output is correct |
24 |
Correct |
53 ms |
11968 KB |
Output is correct |
25 |
Correct |
54 ms |
12020 KB |
Output is correct |
26 |
Correct |
53 ms |
11824 KB |
Output is correct |
27 |
Correct |
55 ms |
12112 KB |
Output is correct |
28 |
Correct |
48 ms |
11764 KB |
Output is correct |
29 |
Correct |
52 ms |
11856 KB |
Output is correct |
30 |
Correct |
54 ms |
12016 KB |
Output is correct |
31 |
Correct |
50 ms |
11764 KB |
Output is correct |
32 |
Correct |
70 ms |
12016 KB |
Output is correct |
33 |
Correct |
55 ms |
12024 KB |
Output is correct |
34 |
Correct |
44 ms |
11764 KB |
Output is correct |
35 |
Correct |
52 ms |
11852 KB |
Output is correct |
36 |
Correct |
61 ms |
11816 KB |
Output is correct |
37 |
Correct |
55 ms |
12016 KB |
Output is correct |
38 |
Correct |
55 ms |
11848 KB |
Output is correct |
39 |
Correct |
54 ms |
11828 KB |
Output is correct |
40 |
Correct |
54 ms |
12016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
231 ms |
15132 KB |
Output is correct |
12 |
Correct |
159 ms |
14888 KB |
Output is correct |
13 |
Correct |
178 ms |
14156 KB |
Output is correct |
14 |
Correct |
173 ms |
14164 KB |
Output is correct |
15 |
Correct |
157 ms |
14988 KB |
Output is correct |
16 |
Correct |
205 ms |
14156 KB |
Output is correct |
17 |
Correct |
208 ms |
14164 KB |
Output is correct |
18 |
Correct |
117 ms |
16208 KB |
Output is correct |
19 |
Correct |
207 ms |
13000 KB |
Output is correct |
20 |
Correct |
207 ms |
14652 KB |
Output is correct |
21 |
Correct |
324 ms |
18148 KB |
Output is correct |
22 |
Correct |
164 ms |
18340 KB |
Output is correct |
23 |
Correct |
220 ms |
17364 KB |
Output is correct |
24 |
Correct |
207 ms |
17236 KB |
Output is correct |
25 |
Correct |
176 ms |
19028 KB |
Output is correct |
26 |
Correct |
245 ms |
17492 KB |
Output is correct |
27 |
Correct |
242 ms |
17648 KB |
Output is correct |
28 |
Correct |
123 ms |
20052 KB |
Output is correct |
29 |
Correct |
142 ms |
16040 KB |
Output is correct |
30 |
Correct |
266 ms |
18304 KB |
Output is correct |
31 |
Correct |
218 ms |
18192 KB |
Output is correct |
32 |
Correct |
232 ms |
18004 KB |
Output is correct |
33 |
Correct |
187 ms |
16976 KB |
Output is correct |
34 |
Correct |
251 ms |
17208 KB |
Output is correct |
35 |
Correct |
259 ms |
17488 KB |
Output is correct |
36 |
Correct |
121 ms |
16208 KB |
Output is correct |
37 |
Correct |
157 ms |
18004 KB |
Output is correct |
38 |
Correct |
229 ms |
16212 KB |
Output is correct |
39 |
Correct |
345 ms |
17236 KB |
Output is correct |
40 |
Correct |
255 ms |
17176 KB |
Output is correct |
41 |
Correct |
45 ms |
12020 KB |
Output is correct |
42 |
Correct |
45 ms |
12108 KB |
Output is correct |
43 |
Correct |
50 ms |
11844 KB |
Output is correct |
44 |
Correct |
59 ms |
11848 KB |
Output is correct |
45 |
Correct |
47 ms |
12016 KB |
Output is correct |
46 |
Correct |
56 ms |
12016 KB |
Output is correct |
47 |
Correct |
55 ms |
12000 KB |
Output is correct |
48 |
Correct |
41 ms |
12020 KB |
Output is correct |
49 |
Correct |
44 ms |
11768 KB |
Output is correct |
50 |
Correct |
46 ms |
12104 KB |
Output is correct |
51 |
Correct |
56 ms |
12016 KB |
Output is correct |
52 |
Correct |
61 ms |
12016 KB |
Output is correct |
53 |
Correct |
46 ms |
11896 KB |
Output is correct |
54 |
Correct |
53 ms |
11968 KB |
Output is correct |
55 |
Correct |
54 ms |
12020 KB |
Output is correct |
56 |
Correct |
53 ms |
11824 KB |
Output is correct |
57 |
Correct |
55 ms |
12112 KB |
Output is correct |
58 |
Correct |
48 ms |
11764 KB |
Output is correct |
59 |
Correct |
52 ms |
11856 KB |
Output is correct |
60 |
Correct |
54 ms |
12016 KB |
Output is correct |
61 |
Correct |
50 ms |
11764 KB |
Output is correct |
62 |
Correct |
70 ms |
12016 KB |
Output is correct |
63 |
Correct |
55 ms |
12024 KB |
Output is correct |
64 |
Correct |
44 ms |
11764 KB |
Output is correct |
65 |
Correct |
52 ms |
11852 KB |
Output is correct |
66 |
Correct |
61 ms |
11816 KB |
Output is correct |
67 |
Correct |
55 ms |
12016 KB |
Output is correct |
68 |
Correct |
55 ms |
11848 KB |
Output is correct |
69 |
Correct |
54 ms |
11828 KB |
Output is correct |
70 |
Correct |
54 ms |
12016 KB |
Output is correct |
71 |
Correct |
307 ms |
36336 KB |
Output is correct |
72 |
Correct |
292 ms |
36340 KB |
Output is correct |
73 |
Correct |
392 ms |
34800 KB |
Output is correct |
74 |
Correct |
544 ms |
35292 KB |
Output is correct |
75 |
Correct |
297 ms |
37268 KB |
Output is correct |
76 |
Correct |
471 ms |
35668 KB |
Output is correct |
77 |
Correct |
433 ms |
35540 KB |
Output is correct |
78 |
Correct |
198 ms |
39152 KB |
Output is correct |
79 |
Correct |
262 ms |
33272 KB |
Output is correct |
80 |
Correct |
306 ms |
36336 KB |
Output is correct |
81 |
Correct |
412 ms |
36340 KB |
Output is correct |
82 |
Correct |
584 ms |
35152 KB |
Output is correct |
83 |
Correct |
309 ms |
34292 KB |
Output is correct |
84 |
Correct |
440 ms |
35304 KB |
Output is correct |
85 |
Correct |
459 ms |
35676 KB |
Output is correct |
86 |
Correct |
173 ms |
33268 KB |
Output is correct |
87 |
Correct |
267 ms |
36152 KB |
Output is correct |
88 |
Correct |
259 ms |
33264 KB |
Output is correct |
89 |
Correct |
333 ms |
34804 KB |
Output is correct |
90 |
Correct |
392 ms |
35332 KB |
Output is correct |
91 |
Correct |
295 ms |
34384 KB |
Output is correct |
92 |
Correct |
569 ms |
35668 KB |
Output is correct |
93 |
Correct |
446 ms |
35340 KB |
Output is correct |
94 |
Correct |
175 ms |
33364 KB |
Output is correct |
95 |
Correct |
382 ms |
35320 KB |
Output is correct |
96 |
Correct |
396 ms |
35316 KB |
Output is correct |
97 |
Correct |
402 ms |
35312 KB |
Output is correct |
98 |
Correct |
384 ms |
35336 KB |
Output is correct |
99 |
Correct |
386 ms |
35312 KB |
Output is correct |
100 |
Correct |
370 ms |
35356 KB |
Output is correct |