#include <bits/stdc++.h>
using namespace std;
constexpr int lax = 20, qax = 1e6, nax = (1 << lax);
int l, q, n, tab[nax], pre[3][nax];
string s;
vector< int > ind;
int id(const char c)
{
if (c == '?') return 2;
return c - '0';
}
int rek(const int t, string& s, const int v, const int msk = 0)
{
for (int j = v; j < ind.size(); ++j)
{
int i = ind[j];
if (id(s[i]) == t)
{
int res = 0;
if (t == 2)
{
s[i] = '0';
res += rek(t, s, j + 1, msk);
s[i] = '1';
res += rek(t, s, j + 1, msk + (1 << i));
s[i] = '?';
return res;
}
if (t == 0)
{
s[i] = '?';
res += rek(t, s, j + 1, msk + (1 << i));
s[i] = '1';
res -= rek(t, s, j + 1, msk);
s[i] = '0';
return res;
}
if (t == 1)
{
s[i] = '?';
res += rek(t, s, j + 1, msk + (1 << i));
s[i] = '0';
res -= rek(t, s, j + 1, msk);
s[i] = '1';
return res;
}
}
}
return pre[t][msk]; //spo
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> l >> q;
n = (1 << l);
cin >> s;
for (int i = 0; i < n; ++i)
tab[i] = s[i] - '0';
for (int i = 0; i < n; ++i)
pre[0][i] = tab[(n - 1) - i];
for (int i = 0; i < n; ++i)
pre[1][i] = tab[i];
for (int i = 0; i < n; ++i)
pre[2][i] = tab[i];
for (int i = 0; i < l; ++i)
for (int j = 0; j < n; ++j)
if ((1 << i) & j)
pre[0][j] += pre[0][j - (1 << i)],
pre[1][j] += pre[1][j - (1 << i)];
for (int i = 0; i < q; ++i)
{
string s; cin >> s;
reverse(begin(s), end(s));
int zl[2] = {0, 0};
for (char c : s)
if (c == '0' or c == '1')
++zl[c - '0'];
if (zl[0] <= lax / 3)
{
ind.clear();
for (int i = 0; i < l; ++i)
if (s[i] == '0')
ind.push_back(i);
int msk = 0;
for (int i = 0; i < l; ++i)
if (s[i] == '?')
msk += (1 << i);
cout << rek(0, s, 0, msk) << '\n';
}
else if (zl[1] <= lax / 3)
{
ind.clear();
for (int i = 0; i < l; ++i)
if (s[i] == '1')
ind.push_back(i);
int msk = 0;
for (int i = 0; i < l; ++i)
if (s[i] == '?')
msk += (1 << i);
cout << rek(1, s, 0, msk) << '\n';
}
else if (l - zl[0] - zl[1] <= lax / 3)
{
ind.clear();
for (int i = 0; i < l; ++i)
if (s[i] == '?')
ind.push_back(i);
int msk = 0;
for (int i = 0; i < l; ++i)
if (s[i] == '1')
msk += (1 << i);
cout << rek(2, s, 0, msk) << '\n';
}
}
}
Compilation message
snake_escaping.cpp: In function 'int rek(int, std::__cxx11::string&, int, int)':
snake_escaping.cpp:18:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = v; j < ind.size(); ++j)
~~^~~~~~~~~~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:79:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if ((1 << i) & j)
^~
snake_escaping.cpp:83:17: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
for (int i = 0; i < q; ++i)
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
432 KB |
Output is correct |
11 |
Correct |
305 ms |
5212 KB |
Output is correct |
12 |
Correct |
770 ms |
5180 KB |
Output is correct |
13 |
Correct |
842 ms |
7088 KB |
Output is correct |
14 |
Correct |
606 ms |
14328 KB |
Output is correct |
15 |
Correct |
427 ms |
15228 KB |
Output is correct |
16 |
Correct |
494 ms |
14328 KB |
Output is correct |
17 |
Correct |
610 ms |
14200 KB |
Output is correct |
18 |
Correct |
228 ms |
16120 KB |
Output is correct |
19 |
Correct |
766 ms |
13164 KB |
Output is correct |
20 |
Correct |
357 ms |
14996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
432 KB |
Output is correct |
11 |
Correct |
305 ms |
5212 KB |
Output is correct |
12 |
Correct |
770 ms |
5180 KB |
Output is correct |
13 |
Correct |
842 ms |
7088 KB |
Output is correct |
14 |
Correct |
606 ms |
14328 KB |
Output is correct |
15 |
Correct |
427 ms |
15228 KB |
Output is correct |
16 |
Correct |
494 ms |
14328 KB |
Output is correct |
17 |
Correct |
610 ms |
14200 KB |
Output is correct |
18 |
Correct |
228 ms |
16120 KB |
Output is correct |
19 |
Correct |
766 ms |
13164 KB |
Output is correct |
20 |
Correct |
357 ms |
14996 KB |
Output is correct |
21 |
Correct |
409 ms |
18288 KB |
Output is correct |
22 |
Correct |
749 ms |
18464 KB |
Output is correct |
23 |
Correct |
1458 ms |
17596 KB |
Output is correct |
24 |
Correct |
908 ms |
17308 KB |
Output is correct |
25 |
Correct |
539 ms |
19360 KB |
Output is correct |
26 |
Correct |
704 ms |
17788 KB |
Output is correct |
27 |
Correct |
789 ms |
17684 KB |
Output is correct |
28 |
Correct |
250 ms |
20348 KB |
Output is correct |
29 |
Correct |
1326 ms |
16356 KB |
Output is correct |
30 |
Correct |
434 ms |
18532 KB |
Output is correct |
31 |
Correct |
719 ms |
18168 KB |
Output is correct |
32 |
Correct |
704 ms |
18168 KB |
Output is correct |
33 |
Correct |
502 ms |
17144 KB |
Output is correct |
34 |
Correct |
804 ms |
17400 KB |
Output is correct |
35 |
Correct |
787 ms |
17784 KB |
Output is correct |
36 |
Correct |
262 ms |
16248 KB |
Output is correct |
37 |
Correct |
429 ms |
18260 KB |
Output is correct |
38 |
Correct |
956 ms |
16312 KB |
Output is correct |
39 |
Correct |
686 ms |
17400 KB |
Output is correct |
40 |
Correct |
688 ms |
17336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
432 KB |
Output is correct |
11 |
Correct |
83 ms |
19624 KB |
Output is correct |
12 |
Correct |
86 ms |
19500 KB |
Output is correct |
13 |
Correct |
111 ms |
19452 KB |
Output is correct |
14 |
Correct |
150 ms |
19536 KB |
Output is correct |
15 |
Correct |
97 ms |
19792 KB |
Output is correct |
16 |
Correct |
150 ms |
19864 KB |
Output is correct |
17 |
Correct |
138 ms |
19812 KB |
Output is correct |
18 |
Correct |
66 ms |
19988 KB |
Output is correct |
19 |
Correct |
78 ms |
19616 KB |
Output is correct |
20 |
Correct |
82 ms |
19864 KB |
Output is correct |
21 |
Correct |
111 ms |
19864 KB |
Output is correct |
22 |
Correct |
148 ms |
19736 KB |
Output is correct |
23 |
Correct |
92 ms |
19708 KB |
Output is correct |
24 |
Correct |
153 ms |
19864 KB |
Output is correct |
25 |
Correct |
144 ms |
19736 KB |
Output is correct |
26 |
Correct |
69 ms |
19608 KB |
Output is correct |
27 |
Correct |
83 ms |
19860 KB |
Output is correct |
28 |
Correct |
85 ms |
19736 KB |
Output is correct |
29 |
Correct |
110 ms |
19848 KB |
Output is correct |
30 |
Correct |
148 ms |
19736 KB |
Output is correct |
31 |
Correct |
92 ms |
19696 KB |
Output is correct |
32 |
Correct |
164 ms |
19776 KB |
Output is correct |
33 |
Correct |
141 ms |
19740 KB |
Output is correct |
34 |
Correct |
68 ms |
18860 KB |
Output is correct |
35 |
Correct |
118 ms |
19068 KB |
Output is correct |
36 |
Correct |
127 ms |
18968 KB |
Output is correct |
37 |
Correct |
129 ms |
19008 KB |
Output is correct |
38 |
Correct |
119 ms |
18940 KB |
Output is correct |
39 |
Correct |
123 ms |
18984 KB |
Output is correct |
40 |
Correct |
121 ms |
19056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
432 KB |
Output is correct |
11 |
Correct |
305 ms |
5212 KB |
Output is correct |
12 |
Correct |
770 ms |
5180 KB |
Output is correct |
13 |
Correct |
842 ms |
7088 KB |
Output is correct |
14 |
Correct |
606 ms |
14328 KB |
Output is correct |
15 |
Correct |
427 ms |
15228 KB |
Output is correct |
16 |
Correct |
494 ms |
14328 KB |
Output is correct |
17 |
Correct |
610 ms |
14200 KB |
Output is correct |
18 |
Correct |
228 ms |
16120 KB |
Output is correct |
19 |
Correct |
766 ms |
13164 KB |
Output is correct |
20 |
Correct |
357 ms |
14996 KB |
Output is correct |
21 |
Correct |
409 ms |
18288 KB |
Output is correct |
22 |
Correct |
749 ms |
18464 KB |
Output is correct |
23 |
Correct |
1458 ms |
17596 KB |
Output is correct |
24 |
Correct |
908 ms |
17308 KB |
Output is correct |
25 |
Correct |
539 ms |
19360 KB |
Output is correct |
26 |
Correct |
704 ms |
17788 KB |
Output is correct |
27 |
Correct |
789 ms |
17684 KB |
Output is correct |
28 |
Correct |
250 ms |
20348 KB |
Output is correct |
29 |
Correct |
1326 ms |
16356 KB |
Output is correct |
30 |
Correct |
434 ms |
18532 KB |
Output is correct |
31 |
Correct |
719 ms |
18168 KB |
Output is correct |
32 |
Correct |
704 ms |
18168 KB |
Output is correct |
33 |
Correct |
502 ms |
17144 KB |
Output is correct |
34 |
Correct |
804 ms |
17400 KB |
Output is correct |
35 |
Correct |
787 ms |
17784 KB |
Output is correct |
36 |
Correct |
262 ms |
16248 KB |
Output is correct |
37 |
Correct |
429 ms |
18260 KB |
Output is correct |
38 |
Correct |
956 ms |
16312 KB |
Output is correct |
39 |
Correct |
686 ms |
17400 KB |
Output is correct |
40 |
Correct |
688 ms |
17336 KB |
Output is correct |
41 |
Correct |
83 ms |
19624 KB |
Output is correct |
42 |
Correct |
86 ms |
19500 KB |
Output is correct |
43 |
Correct |
111 ms |
19452 KB |
Output is correct |
44 |
Correct |
150 ms |
19536 KB |
Output is correct |
45 |
Correct |
97 ms |
19792 KB |
Output is correct |
46 |
Correct |
150 ms |
19864 KB |
Output is correct |
47 |
Correct |
138 ms |
19812 KB |
Output is correct |
48 |
Correct |
66 ms |
19988 KB |
Output is correct |
49 |
Correct |
78 ms |
19616 KB |
Output is correct |
50 |
Correct |
82 ms |
19864 KB |
Output is correct |
51 |
Correct |
111 ms |
19864 KB |
Output is correct |
52 |
Correct |
148 ms |
19736 KB |
Output is correct |
53 |
Correct |
92 ms |
19708 KB |
Output is correct |
54 |
Correct |
153 ms |
19864 KB |
Output is correct |
55 |
Correct |
144 ms |
19736 KB |
Output is correct |
56 |
Correct |
69 ms |
19608 KB |
Output is correct |
57 |
Correct |
83 ms |
19860 KB |
Output is correct |
58 |
Correct |
85 ms |
19736 KB |
Output is correct |
59 |
Correct |
110 ms |
19848 KB |
Output is correct |
60 |
Correct |
148 ms |
19736 KB |
Output is correct |
61 |
Correct |
92 ms |
19696 KB |
Output is correct |
62 |
Correct |
164 ms |
19776 KB |
Output is correct |
63 |
Correct |
141 ms |
19740 KB |
Output is correct |
64 |
Correct |
68 ms |
18860 KB |
Output is correct |
65 |
Correct |
118 ms |
19068 KB |
Output is correct |
66 |
Correct |
127 ms |
18968 KB |
Output is correct |
67 |
Correct |
129 ms |
19008 KB |
Output is correct |
68 |
Correct |
119 ms |
18940 KB |
Output is correct |
69 |
Correct |
123 ms |
18984 KB |
Output is correct |
70 |
Correct |
121 ms |
19056 KB |
Output is correct |
71 |
Correct |
720 ms |
44596 KB |
Output is correct |
72 |
Correct |
790 ms |
44644 KB |
Output is correct |
73 |
Correct |
1274 ms |
43416 KB |
Output is correct |
74 |
Execution timed out |
2009 ms |
38864 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |