#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("popcnt")
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T>
bool chmax(T &a, T b) { return a < b ? (a = b, true) : false; }
template<class T>
bool chmin(T &a, T b) { return b < a ? (a = b, true) : false; }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U>
void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T>
void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L) == R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 20;
int n, q;
char w[(1<<MAX_N) + 10], com[MAX_N << 1];
int dp[1<<MAX_N];
void init() {
for (int i = 0;i < 1<<n;++i)
dp[i] = w[i] - '0';
for (int i = 0;i < n;++i)
for (int s = 0;s < 1<<n;++s)
if (s>>i&1)
dp[s] += dp[s^(1<<i)];
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> q >> w;
init();
for (int i = 0;i < q;++i) {
cin >> com;
int l = 0, r = (1<<n) - 1, sub = 0;
reverse(com, com + n);
for (int i = 0;i < n;++i) {
if (com[i] == '0') r ^= 1<<i;
if (com[i] == '1') l ^= 1<<i;
if (com[i] == '?') sub |= 1<<i;
}
int res = 0;
if (__builtin_popcount(sub) <= 10) {
for (int s = sub ; s ;(--s) &= sub) {
res += w[l | s] - '0';
}
res += w[l] - '0';
}
else {
int ls = __builtin_popcount(l);
for (int s = l ; s ; (--s) &= l) {
int x = ((__builtin_popcount(s)^ls)&1?-1:1) * dp[s | sub];
res += x;
}
res += (ls&1?-1:1) * dp[sub];
DE(res);
}
cout << abs(res) << '\n';
}
}
Compilation message
snake_escaping.cpp: In function 'int32_t main()':
snake_escaping.cpp:20:17: warning: statement has no effect [-Wunused-value]
20 | #define DE(...) 0
| ^
snake_escaping.cpp:63:4: note: in expansion of macro 'DE'
63 | DE(res);
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
267 ms |
4332 KB |
Output is correct |
12 |
Correct |
266 ms |
4204 KB |
Output is correct |
13 |
Correct |
228 ms |
3308 KB |
Output is correct |
14 |
Correct |
228 ms |
3436 KB |
Output is correct |
15 |
Correct |
292 ms |
4332 KB |
Output is correct |
16 |
Correct |
267 ms |
3820 KB |
Output is correct |
17 |
Correct |
258 ms |
3564 KB |
Output is correct |
18 |
Correct |
1369 ms |
5396 KB |
Output is correct |
19 |
Correct |
232 ms |
2412 KB |
Output is correct |
20 |
Correct |
312 ms |
4076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
267 ms |
4332 KB |
Output is correct |
12 |
Correct |
266 ms |
4204 KB |
Output is correct |
13 |
Correct |
228 ms |
3308 KB |
Output is correct |
14 |
Correct |
228 ms |
3436 KB |
Output is correct |
15 |
Correct |
292 ms |
4332 KB |
Output is correct |
16 |
Correct |
267 ms |
3820 KB |
Output is correct |
17 |
Correct |
258 ms |
3564 KB |
Output is correct |
18 |
Correct |
1369 ms |
5396 KB |
Output is correct |
19 |
Correct |
232 ms |
2412 KB |
Output is correct |
20 |
Correct |
312 ms |
4076 KB |
Output is correct |
21 |
Correct |
408 ms |
4588 KB |
Output is correct |
22 |
Correct |
409 ms |
4716 KB |
Output is correct |
23 |
Correct |
260 ms |
3564 KB |
Output is correct |
24 |
Correct |
256 ms |
3564 KB |
Output is correct |
25 |
Correct |
522 ms |
5484 KB |
Output is correct |
26 |
Correct |
307 ms |
3948 KB |
Output is correct |
27 |
Correct |
310 ms |
3948 KB |
Output is correct |
28 |
Correct |
216 ms |
17212 KB |
Output is correct |
29 |
Correct |
260 ms |
16108 KB |
Output is correct |
30 |
Correct |
476 ms |
18284 KB |
Output is correct |
31 |
Correct |
326 ms |
18156 KB |
Output is correct |
32 |
Correct |
286 ms |
18220 KB |
Output is correct |
33 |
Correct |
271 ms |
17132 KB |
Output is correct |
34 |
Correct |
276 ms |
17132 KB |
Output is correct |
35 |
Correct |
314 ms |
17644 KB |
Output is correct |
36 |
Correct |
200 ms |
16108 KB |
Output is correct |
37 |
Correct |
286 ms |
18148 KB |
Output is correct |
38 |
Correct |
265 ms |
16108 KB |
Output is correct |
39 |
Correct |
300 ms |
17388 KB |
Output is correct |
40 |
Correct |
269 ms |
17132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
108 ms |
5740 KB |
Output is correct |
12 |
Correct |
60 ms |
5740 KB |
Output is correct |
13 |
Correct |
45 ms |
5740 KB |
Output is correct |
14 |
Correct |
50 ms |
5612 KB |
Output is correct |
15 |
Correct |
43 ms |
5740 KB |
Output is correct |
16 |
Correct |
61 ms |
5740 KB |
Output is correct |
17 |
Correct |
56 ms |
5740 KB |
Output is correct |
18 |
Correct |
38 ms |
6892 KB |
Output is correct |
19 |
Correct |
43 ms |
7660 KB |
Output is correct |
20 |
Correct |
84 ms |
7788 KB |
Output is correct |
21 |
Correct |
114 ms |
7788 KB |
Output is correct |
22 |
Correct |
54 ms |
7788 KB |
Output is correct |
23 |
Correct |
44 ms |
7660 KB |
Output is correct |
24 |
Correct |
49 ms |
7788 KB |
Output is correct |
25 |
Correct |
57 ms |
7788 KB |
Output is correct |
26 |
Correct |
37 ms |
7660 KB |
Output is correct |
27 |
Correct |
105 ms |
7788 KB |
Output is correct |
28 |
Correct |
44 ms |
7660 KB |
Output is correct |
29 |
Correct |
49 ms |
7788 KB |
Output is correct |
30 |
Correct |
48 ms |
7788 KB |
Output is correct |
31 |
Correct |
41 ms |
7660 KB |
Output is correct |
32 |
Correct |
59 ms |
7788 KB |
Output is correct |
33 |
Correct |
59 ms |
7788 KB |
Output is correct |
34 |
Correct |
38 ms |
7660 KB |
Output is correct |
35 |
Correct |
59 ms |
7788 KB |
Output is correct |
36 |
Correct |
60 ms |
7788 KB |
Output is correct |
37 |
Correct |
60 ms |
7700 KB |
Output is correct |
38 |
Correct |
60 ms |
7788 KB |
Output is correct |
39 |
Correct |
61 ms |
7788 KB |
Output is correct |
40 |
Correct |
59 ms |
7788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
267 ms |
4332 KB |
Output is correct |
12 |
Correct |
266 ms |
4204 KB |
Output is correct |
13 |
Correct |
228 ms |
3308 KB |
Output is correct |
14 |
Correct |
228 ms |
3436 KB |
Output is correct |
15 |
Correct |
292 ms |
4332 KB |
Output is correct |
16 |
Correct |
267 ms |
3820 KB |
Output is correct |
17 |
Correct |
258 ms |
3564 KB |
Output is correct |
18 |
Correct |
1369 ms |
5396 KB |
Output is correct |
19 |
Correct |
232 ms |
2412 KB |
Output is correct |
20 |
Correct |
312 ms |
4076 KB |
Output is correct |
21 |
Correct |
408 ms |
4588 KB |
Output is correct |
22 |
Correct |
409 ms |
4716 KB |
Output is correct |
23 |
Correct |
260 ms |
3564 KB |
Output is correct |
24 |
Correct |
256 ms |
3564 KB |
Output is correct |
25 |
Correct |
522 ms |
5484 KB |
Output is correct |
26 |
Correct |
307 ms |
3948 KB |
Output is correct |
27 |
Correct |
310 ms |
3948 KB |
Output is correct |
28 |
Correct |
216 ms |
17212 KB |
Output is correct |
29 |
Correct |
260 ms |
16108 KB |
Output is correct |
30 |
Correct |
476 ms |
18284 KB |
Output is correct |
31 |
Correct |
326 ms |
18156 KB |
Output is correct |
32 |
Correct |
286 ms |
18220 KB |
Output is correct |
33 |
Correct |
271 ms |
17132 KB |
Output is correct |
34 |
Correct |
276 ms |
17132 KB |
Output is correct |
35 |
Correct |
314 ms |
17644 KB |
Output is correct |
36 |
Correct |
200 ms |
16108 KB |
Output is correct |
37 |
Correct |
286 ms |
18148 KB |
Output is correct |
38 |
Correct |
265 ms |
16108 KB |
Output is correct |
39 |
Correct |
300 ms |
17388 KB |
Output is correct |
40 |
Correct |
269 ms |
17132 KB |
Output is correct |
41 |
Correct |
108 ms |
5740 KB |
Output is correct |
42 |
Correct |
60 ms |
5740 KB |
Output is correct |
43 |
Correct |
45 ms |
5740 KB |
Output is correct |
44 |
Correct |
50 ms |
5612 KB |
Output is correct |
45 |
Correct |
43 ms |
5740 KB |
Output is correct |
46 |
Correct |
61 ms |
5740 KB |
Output is correct |
47 |
Correct |
56 ms |
5740 KB |
Output is correct |
48 |
Correct |
38 ms |
6892 KB |
Output is correct |
49 |
Correct |
43 ms |
7660 KB |
Output is correct |
50 |
Correct |
84 ms |
7788 KB |
Output is correct |
51 |
Correct |
114 ms |
7788 KB |
Output is correct |
52 |
Correct |
54 ms |
7788 KB |
Output is correct |
53 |
Correct |
44 ms |
7660 KB |
Output is correct |
54 |
Correct |
49 ms |
7788 KB |
Output is correct |
55 |
Correct |
57 ms |
7788 KB |
Output is correct |
56 |
Correct |
37 ms |
7660 KB |
Output is correct |
57 |
Correct |
105 ms |
7788 KB |
Output is correct |
58 |
Correct |
44 ms |
7660 KB |
Output is correct |
59 |
Correct |
49 ms |
7788 KB |
Output is correct |
60 |
Correct |
48 ms |
7788 KB |
Output is correct |
61 |
Correct |
41 ms |
7660 KB |
Output is correct |
62 |
Correct |
59 ms |
7788 KB |
Output is correct |
63 |
Correct |
59 ms |
7788 KB |
Output is correct |
64 |
Correct |
38 ms |
7660 KB |
Output is correct |
65 |
Correct |
59 ms |
7788 KB |
Output is correct |
66 |
Correct |
60 ms |
7788 KB |
Output is correct |
67 |
Correct |
60 ms |
7700 KB |
Output is correct |
68 |
Correct |
60 ms |
7788 KB |
Output is correct |
69 |
Correct |
61 ms |
7788 KB |
Output is correct |
70 |
Correct |
59 ms |
7788 KB |
Output is correct |
71 |
Correct |
1733 ms |
32236 KB |
Output is correct |
72 |
Correct |
732 ms |
32492 KB |
Output is correct |
73 |
Correct |
420 ms |
30828 KB |
Output is correct |
74 |
Correct |
558 ms |
31084 KB |
Output is correct |
75 |
Correct |
382 ms |
33132 KB |
Output is correct |
76 |
Correct |
694 ms |
31596 KB |
Output is correct |
77 |
Correct |
664 ms |
31340 KB |
Output is correct |
78 |
Correct |
285 ms |
35180 KB |
Output is correct |
79 |
Correct |
383 ms |
29124 KB |
Output is correct |
80 |
Correct |
1185 ms |
32236 KB |
Output is correct |
81 |
Correct |
1785 ms |
32236 KB |
Output is correct |
82 |
Correct |
567 ms |
31084 KB |
Output is correct |
83 |
Correct |
381 ms |
30248 KB |
Output is correct |
84 |
Correct |
467 ms |
31084 KB |
Output is correct |
85 |
Correct |
661 ms |
31340 KB |
Output is correct |
86 |
Correct |
248 ms |
29060 KB |
Output is correct |
87 |
Correct |
1618 ms |
32108 KB |
Output is correct |
88 |
Correct |
375 ms |
29292 KB |
Output is correct |
89 |
Correct |
475 ms |
30828 KB |
Output is correct |
90 |
Correct |
466 ms |
31212 KB |
Output is correct |
91 |
Correct |
337 ms |
30444 KB |
Output is correct |
92 |
Correct |
709 ms |
31724 KB |
Output is correct |
93 |
Correct |
659 ms |
31340 KB |
Output is correct |
94 |
Correct |
261 ms |
29036 KB |
Output is correct |
95 |
Correct |
704 ms |
31200 KB |
Output is correct |
96 |
Correct |
707 ms |
31212 KB |
Output is correct |
97 |
Correct |
702 ms |
31352 KB |
Output is correct |
98 |
Correct |
714 ms |
31240 KB |
Output is correct |
99 |
Correct |
709 ms |
31244 KB |
Output is correct |
100 |
Correct |
698 ms |
31212 KB |
Output is correct |