#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll N = 20;
ll dp[1 << N], dp2[1 << N];
ll cnt[256];
void get_locs(string &t, vector<ll> &loc, ll &val, char ch, bool q) {
for (ll i = 0; i < t.length(); ++i) {
if (t[i] == ch) {
loc.push_back(t.length() - i - 1);
}
val += (1 << (t.length() - i - 1)) * (t[i] == '1' || (q && t[i] == '?'));
}
}
void populate(queue<pair<ll, ll>> &q, vector<ll> &loc, ll val, ll n) {
q.push({val, -1});
for (ll i = 0; i < n; ++i) {
ll sz = q.size();
while (sz--) {
ll v = q.front().first;
ll idx = q.front().second + 1;
q.pop();
q.push({v, idx});
v ^= (1 << loc[idx]);
q.push({v, idx});
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll l, q;
cin >> l >> q;
string s;
cin >> s;
for (ll i = 0; i < s.length(); ++i) {
dp[i] = dp2[i] = s[i] - '0';
}
for (ll i = 0; i < N; ++i) {
for (ll mask = 0; mask < (1 << N); ++mask) {
if (mask & (1 << i)) {
dp[mask] += dp[mask ^ (1 << i)];
} else {
dp2[mask] += dp2[mask ^ (1 << i)];
}
}
}
while (q--) {
string t;
cin >> t;
ll ans = 0;
cnt['1'] = cnt['0'] = cnt['?'] = 0;
for (auto &i : t) {
cnt[i]++;
}
vector<ll> loc;
queue<pair<ll, ll>> q;
ll val = 0;
if (cnt['?'] <= 6) {
get_locs(t, loc, val, '?', false);
populate(q, loc, val, cnt['?']);
while (!q.empty()) {
ll v = q.front().first;
q.pop();
ans += s[v] - '0';
}
} else if (cnt['1'] <= 6) {
get_locs(t, loc, val, '1', true);
populate(q, loc, val, cnt['1']);
while (!q.empty()) {
ll v = q.front().first;
q.pop();
ans +=
dp[v] *
((__builtin_popcount(v) - cnt['?']) % 2 == cnt['1'] % 2 ? 1 : -1);
}
} else {
get_locs(t, loc, val, '0', false);
populate(q, loc, val, cnt['0']);
while (!q.empty()) {
ll v = q.front().first;
q.pop();
ans += dp2[v] * ((__builtin_popcount(v) - cnt['1']) % 2 == 0 ? 1 : -1);
}
}
cout << ans << "\n";
}
}
Compilation message
snake_escaping.cpp: In function 'void get_locs(std::string&, std::vector<long long int>&, long long int&, char, bool)':
snake_escaping.cpp:13:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | for (ll i = 0; i < t.length(); ++i) {
| ~~^~~~~~~~~~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:45:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (ll i = 0; i < s.length(); ++i) {
| ~~^~~~~~~~~~~~
snake_escaping.cpp:64:11: warning: array subscript has type 'char' [-Wchar-subscripts]
64 | cnt[i]++;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
16732 KB |
Output is correct |
2 |
Correct |
27 ms |
16728 KB |
Output is correct |
3 |
Correct |
28 ms |
16732 KB |
Output is correct |
4 |
Correct |
29 ms |
16732 KB |
Output is correct |
5 |
Correct |
28 ms |
16732 KB |
Output is correct |
6 |
Correct |
25 ms |
16732 KB |
Output is correct |
7 |
Correct |
25 ms |
16888 KB |
Output is correct |
8 |
Correct |
25 ms |
16896 KB |
Output is correct |
9 |
Correct |
26 ms |
16864 KB |
Output is correct |
10 |
Correct |
26 ms |
16732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
16732 KB |
Output is correct |
2 |
Correct |
27 ms |
16728 KB |
Output is correct |
3 |
Correct |
28 ms |
16732 KB |
Output is correct |
4 |
Correct |
29 ms |
16732 KB |
Output is correct |
5 |
Correct |
28 ms |
16732 KB |
Output is correct |
6 |
Correct |
25 ms |
16732 KB |
Output is correct |
7 |
Correct |
25 ms |
16888 KB |
Output is correct |
8 |
Correct |
25 ms |
16896 KB |
Output is correct |
9 |
Correct |
26 ms |
16864 KB |
Output is correct |
10 |
Correct |
26 ms |
16732 KB |
Output is correct |
11 |
Correct |
881 ms |
29620 KB |
Output is correct |
12 |
Correct |
743 ms |
29152 KB |
Output is correct |
13 |
Correct |
421 ms |
28424 KB |
Output is correct |
14 |
Correct |
485 ms |
28464 KB |
Output is correct |
15 |
Correct |
1502 ms |
29664 KB |
Output is correct |
16 |
Correct |
614 ms |
28672 KB |
Output is correct |
17 |
Correct |
575 ms |
28496 KB |
Output is correct |
18 |
Correct |
228 ms |
30448 KB |
Output is correct |
19 |
Correct |
235 ms |
27348 KB |
Output is correct |
20 |
Correct |
785 ms |
29164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
16732 KB |
Output is correct |
2 |
Correct |
27 ms |
16728 KB |
Output is correct |
3 |
Correct |
28 ms |
16732 KB |
Output is correct |
4 |
Correct |
29 ms |
16732 KB |
Output is correct |
5 |
Correct |
28 ms |
16732 KB |
Output is correct |
6 |
Correct |
25 ms |
16732 KB |
Output is correct |
7 |
Correct |
25 ms |
16888 KB |
Output is correct |
8 |
Correct |
25 ms |
16896 KB |
Output is correct |
9 |
Correct |
26 ms |
16864 KB |
Output is correct |
10 |
Correct |
26 ms |
16732 KB |
Output is correct |
11 |
Correct |
881 ms |
29620 KB |
Output is correct |
12 |
Correct |
743 ms |
29152 KB |
Output is correct |
13 |
Correct |
421 ms |
28424 KB |
Output is correct |
14 |
Correct |
485 ms |
28464 KB |
Output is correct |
15 |
Correct |
1502 ms |
29664 KB |
Output is correct |
16 |
Correct |
614 ms |
28672 KB |
Output is correct |
17 |
Correct |
575 ms |
28496 KB |
Output is correct |
18 |
Correct |
228 ms |
30448 KB |
Output is correct |
19 |
Correct |
235 ms |
27348 KB |
Output is correct |
20 |
Correct |
785 ms |
29164 KB |
Output is correct |
21 |
Correct |
1704 ms |
31940 KB |
Output is correct |
22 |
Correct |
648 ms |
31960 KB |
Output is correct |
23 |
Correct |
614 ms |
30912 KB |
Output is correct |
24 |
Correct |
626 ms |
31052 KB |
Output is correct |
25 |
Correct |
439 ms |
32788 KB |
Output is correct |
26 |
Correct |
782 ms |
31648 KB |
Output is correct |
27 |
Correct |
766 ms |
31320 KB |
Output is correct |
28 |
Correct |
255 ms |
33876 KB |
Output is correct |
29 |
Correct |
278 ms |
29864 KB |
Output is correct |
30 |
Correct |
1134 ms |
32012 KB |
Output is correct |
31 |
Correct |
1503 ms |
31812 KB |
Output is correct |
32 |
Correct |
988 ms |
32040 KB |
Output is correct |
33 |
Correct |
465 ms |
30548 KB |
Output is correct |
34 |
Correct |
659 ms |
30800 KB |
Output is correct |
35 |
Correct |
758 ms |
31260 KB |
Output is correct |
36 |
Correct |
253 ms |
29864 KB |
Output is correct |
37 |
Correct |
1491 ms |
31876 KB |
Output is correct |
38 |
Correct |
278 ms |
29660 KB |
Output is correct |
39 |
Correct |
637 ms |
31032 KB |
Output is correct |
40 |
Correct |
630 ms |
30832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
16732 KB |
Output is correct |
2 |
Correct |
27 ms |
16728 KB |
Output is correct |
3 |
Correct |
28 ms |
16732 KB |
Output is correct |
4 |
Correct |
29 ms |
16732 KB |
Output is correct |
5 |
Correct |
28 ms |
16732 KB |
Output is correct |
6 |
Correct |
25 ms |
16732 KB |
Output is correct |
7 |
Correct |
25 ms |
16888 KB |
Output is correct |
8 |
Correct |
25 ms |
16896 KB |
Output is correct |
9 |
Correct |
26 ms |
16864 KB |
Output is correct |
10 |
Correct |
26 ms |
16732 KB |
Output is correct |
11 |
Correct |
41 ms |
19956 KB |
Output is correct |
12 |
Correct |
49 ms |
20236 KB |
Output is correct |
13 |
Correct |
69 ms |
19956 KB |
Output is correct |
14 |
Correct |
111 ms |
19956 KB |
Output is correct |
15 |
Correct |
58 ms |
19956 KB |
Output is correct |
16 |
Correct |
97 ms |
19960 KB |
Output is correct |
17 |
Correct |
83 ms |
19952 KB |
Output is correct |
18 |
Correct |
38 ms |
20140 KB |
Output is correct |
19 |
Correct |
43 ms |
19688 KB |
Output is correct |
20 |
Correct |
47 ms |
19952 KB |
Output is correct |
21 |
Correct |
96 ms |
20048 KB |
Output is correct |
22 |
Correct |
112 ms |
19952 KB |
Output is correct |
23 |
Correct |
63 ms |
19936 KB |
Output is correct |
24 |
Correct |
102 ms |
19812 KB |
Output is correct |
25 |
Correct |
92 ms |
19960 KB |
Output is correct |
26 |
Correct |
39 ms |
19808 KB |
Output is correct |
27 |
Correct |
36 ms |
19956 KB |
Output is correct |
28 |
Correct |
45 ms |
19796 KB |
Output is correct |
29 |
Correct |
71 ms |
20004 KB |
Output is correct |
30 |
Correct |
109 ms |
20064 KB |
Output is correct |
31 |
Correct |
56 ms |
20208 KB |
Output is correct |
32 |
Correct |
108 ms |
20236 KB |
Output is correct |
33 |
Correct |
88 ms |
20056 KB |
Output is correct |
34 |
Correct |
36 ms |
19952 KB |
Output is correct |
35 |
Correct |
76 ms |
20236 KB |
Output is correct |
36 |
Correct |
79 ms |
20204 KB |
Output is correct |
37 |
Correct |
70 ms |
20060 KB |
Output is correct |
38 |
Correct |
87 ms |
20396 KB |
Output is correct |
39 |
Correct |
71 ms |
20260 KB |
Output is correct |
40 |
Correct |
73 ms |
20200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
16732 KB |
Output is correct |
2 |
Correct |
27 ms |
16728 KB |
Output is correct |
3 |
Correct |
28 ms |
16732 KB |
Output is correct |
4 |
Correct |
29 ms |
16732 KB |
Output is correct |
5 |
Correct |
28 ms |
16732 KB |
Output is correct |
6 |
Correct |
25 ms |
16732 KB |
Output is correct |
7 |
Correct |
25 ms |
16888 KB |
Output is correct |
8 |
Correct |
25 ms |
16896 KB |
Output is correct |
9 |
Correct |
26 ms |
16864 KB |
Output is correct |
10 |
Correct |
26 ms |
16732 KB |
Output is correct |
11 |
Correct |
881 ms |
29620 KB |
Output is correct |
12 |
Correct |
743 ms |
29152 KB |
Output is correct |
13 |
Correct |
421 ms |
28424 KB |
Output is correct |
14 |
Correct |
485 ms |
28464 KB |
Output is correct |
15 |
Correct |
1502 ms |
29664 KB |
Output is correct |
16 |
Correct |
614 ms |
28672 KB |
Output is correct |
17 |
Correct |
575 ms |
28496 KB |
Output is correct |
18 |
Correct |
228 ms |
30448 KB |
Output is correct |
19 |
Correct |
235 ms |
27348 KB |
Output is correct |
20 |
Correct |
785 ms |
29164 KB |
Output is correct |
21 |
Correct |
1704 ms |
31940 KB |
Output is correct |
22 |
Correct |
648 ms |
31960 KB |
Output is correct |
23 |
Correct |
614 ms |
30912 KB |
Output is correct |
24 |
Correct |
626 ms |
31052 KB |
Output is correct |
25 |
Correct |
439 ms |
32788 KB |
Output is correct |
26 |
Correct |
782 ms |
31648 KB |
Output is correct |
27 |
Correct |
766 ms |
31320 KB |
Output is correct |
28 |
Correct |
255 ms |
33876 KB |
Output is correct |
29 |
Correct |
278 ms |
29864 KB |
Output is correct |
30 |
Correct |
1134 ms |
32012 KB |
Output is correct |
31 |
Correct |
1503 ms |
31812 KB |
Output is correct |
32 |
Correct |
988 ms |
32040 KB |
Output is correct |
33 |
Correct |
465 ms |
30548 KB |
Output is correct |
34 |
Correct |
659 ms |
30800 KB |
Output is correct |
35 |
Correct |
758 ms |
31260 KB |
Output is correct |
36 |
Correct |
253 ms |
29864 KB |
Output is correct |
37 |
Correct |
1491 ms |
31876 KB |
Output is correct |
38 |
Correct |
278 ms |
29660 KB |
Output is correct |
39 |
Correct |
637 ms |
31032 KB |
Output is correct |
40 |
Correct |
630 ms |
30832 KB |
Output is correct |
41 |
Correct |
41 ms |
19956 KB |
Output is correct |
42 |
Correct |
49 ms |
20236 KB |
Output is correct |
43 |
Correct |
69 ms |
19956 KB |
Output is correct |
44 |
Correct |
111 ms |
19956 KB |
Output is correct |
45 |
Correct |
58 ms |
19956 KB |
Output is correct |
46 |
Correct |
97 ms |
19960 KB |
Output is correct |
47 |
Correct |
83 ms |
19952 KB |
Output is correct |
48 |
Correct |
38 ms |
20140 KB |
Output is correct |
49 |
Correct |
43 ms |
19688 KB |
Output is correct |
50 |
Correct |
47 ms |
19952 KB |
Output is correct |
51 |
Correct |
96 ms |
20048 KB |
Output is correct |
52 |
Correct |
112 ms |
19952 KB |
Output is correct |
53 |
Correct |
63 ms |
19936 KB |
Output is correct |
54 |
Correct |
102 ms |
19812 KB |
Output is correct |
55 |
Correct |
92 ms |
19960 KB |
Output is correct |
56 |
Correct |
39 ms |
19808 KB |
Output is correct |
57 |
Correct |
36 ms |
19956 KB |
Output is correct |
58 |
Correct |
45 ms |
19796 KB |
Output is correct |
59 |
Correct |
71 ms |
20004 KB |
Output is correct |
60 |
Correct |
109 ms |
20064 KB |
Output is correct |
61 |
Correct |
56 ms |
20208 KB |
Output is correct |
62 |
Correct |
108 ms |
20236 KB |
Output is correct |
63 |
Correct |
88 ms |
20056 KB |
Output is correct |
64 |
Correct |
36 ms |
19952 KB |
Output is correct |
65 |
Correct |
76 ms |
20236 KB |
Output is correct |
66 |
Correct |
79 ms |
20204 KB |
Output is correct |
67 |
Correct |
70 ms |
20060 KB |
Output is correct |
68 |
Correct |
87 ms |
20396 KB |
Output is correct |
69 |
Correct |
71 ms |
20260 KB |
Output is correct |
70 |
Correct |
73 ms |
20200 KB |
Output is correct |
71 |
Correct |
391 ms |
42668 KB |
Output is correct |
72 |
Correct |
380 ms |
42884 KB |
Output is correct |
73 |
Correct |
939 ms |
41220 KB |
Output is correct |
74 |
Correct |
1908 ms |
41768 KB |
Output is correct |
75 |
Correct |
727 ms |
43372 KB |
Output is correct |
76 |
Correct |
1719 ms |
41752 KB |
Output is correct |
77 |
Correct |
1297 ms |
41624 KB |
Output is correct |
78 |
Correct |
304 ms |
45300 KB |
Output is correct |
79 |
Correct |
347 ms |
39288 KB |
Output is correct |
80 |
Correct |
505 ms |
42388 KB |
Output is correct |
81 |
Correct |
997 ms |
39672 KB |
Output is correct |
82 |
Correct |
1969 ms |
38532 KB |
Output is correct |
83 |
Correct |
679 ms |
37744 KB |
Output is correct |
84 |
Correct |
1647 ms |
38680 KB |
Output is correct |
85 |
Correct |
1320 ms |
39152 KB |
Output is correct |
86 |
Correct |
281 ms |
36596 KB |
Output is correct |
87 |
Correct |
359 ms |
39612 KB |
Output is correct |
88 |
Correct |
353 ms |
36752 KB |
Output is correct |
89 |
Correct |
952 ms |
38296 KB |
Output is correct |
90 |
Correct |
1637 ms |
38656 KB |
Output is correct |
91 |
Correct |
678 ms |
37840 KB |
Output is correct |
92 |
Correct |
1643 ms |
39300 KB |
Output is correct |
93 |
Correct |
1342 ms |
38772 KB |
Output is correct |
94 |
Correct |
308 ms |
39316 KB |
Output is correct |
95 |
Correct |
1008 ms |
41480 KB |
Output is correct |
96 |
Correct |
1024 ms |
41572 KB |
Output is correct |
97 |
Correct |
1035 ms |
41508 KB |
Output is correct |
98 |
Correct |
1025 ms |
41416 KB |
Output is correct |
99 |
Correct |
1062 ms |
41456 KB |
Output is correct |
100 |
Correct |
1014 ms |
38624 KB |
Output is correct |