#pragma optimize("O3")
#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:1:0: warning: ignoring #pragma optimize [-Wunknown-pragmas]
#pragma optimize("O3")
snake_escaping.cpp: In function 'int rek(int, std::__cxx11::string&, int, int)':
snake_escaping.cpp:19: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:80:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if ((1 << i) & j)
^~
snake_escaping.cpp:84: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)
^~~
# |
결과 |
실행 시간 |
메모리 |
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 |
3 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 |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
3 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 |
376 KB |
Output is correct |
11 |
Correct |
293 ms |
5240 KB |
Output is correct |
12 |
Correct |
762 ms |
4896 KB |
Output is correct |
13 |
Correct |
836 ms |
4300 KB |
Output is correct |
14 |
Correct |
598 ms |
3832 KB |
Output is correct |
15 |
Correct |
423 ms |
4932 KB |
Output is correct |
16 |
Correct |
496 ms |
4060 KB |
Output is correct |
17 |
Correct |
607 ms |
3960 KB |
Output is correct |
18 |
Correct |
222 ms |
5880 KB |
Output is correct |
19 |
Correct |
757 ms |
2936 KB |
Output is correct |
20 |
Correct |
350 ms |
4472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
3 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 |
376 KB |
Output is correct |
11 |
Correct |
293 ms |
5240 KB |
Output is correct |
12 |
Correct |
762 ms |
4896 KB |
Output is correct |
13 |
Correct |
836 ms |
4300 KB |
Output is correct |
14 |
Correct |
598 ms |
3832 KB |
Output is correct |
15 |
Correct |
423 ms |
4932 KB |
Output is correct |
16 |
Correct |
496 ms |
4060 KB |
Output is correct |
17 |
Correct |
607 ms |
3960 KB |
Output is correct |
18 |
Correct |
222 ms |
5880 KB |
Output is correct |
19 |
Correct |
757 ms |
2936 KB |
Output is correct |
20 |
Correct |
350 ms |
4472 KB |
Output is correct |
21 |
Correct |
410 ms |
4988 KB |
Output is correct |
22 |
Correct |
744 ms |
5112 KB |
Output is correct |
23 |
Correct |
1382 ms |
4148 KB |
Output is correct |
24 |
Correct |
923 ms |
4048 KB |
Output is correct |
25 |
Correct |
538 ms |
5984 KB |
Output is correct |
26 |
Correct |
718 ms |
4544 KB |
Output is correct |
27 |
Correct |
783 ms |
4752 KB |
Output is correct |
28 |
Correct |
243 ms |
7032 KB |
Output is correct |
29 |
Correct |
1263 ms |
3084 KB |
Output is correct |
30 |
Correct |
430 ms |
5188 KB |
Output is correct |
31 |
Correct |
727 ms |
5092 KB |
Output is correct |
32 |
Correct |
699 ms |
4984 KB |
Output is correct |
33 |
Correct |
510 ms |
3996 KB |
Output is correct |
34 |
Correct |
798 ms |
3948 KB |
Output is correct |
35 |
Correct |
779 ms |
4392 KB |
Output is correct |
36 |
Correct |
260 ms |
2936 KB |
Output is correct |
37 |
Correct |
414 ms |
4984 KB |
Output is correct |
38 |
Correct |
970 ms |
3040 KB |
Output is correct |
39 |
Correct |
673 ms |
4200 KB |
Output is correct |
40 |
Correct |
686 ms |
4020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
3 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 |
376 KB |
Output is correct |
11 |
Correct |
81 ms |
18968 KB |
Output is correct |
12 |
Correct |
87 ms |
19232 KB |
Output is correct |
13 |
Correct |
114 ms |
19096 KB |
Output is correct |
14 |
Correct |
159 ms |
18972 KB |
Output is correct |
15 |
Correct |
96 ms |
18892 KB |
Output is correct |
16 |
Correct |
156 ms |
18588 KB |
Output is correct |
17 |
Correct |
137 ms |
18456 KB |
Output is correct |
18 |
Correct |
68 ms |
18712 KB |
Output is correct |
19 |
Correct |
83 ms |
18456 KB |
Output is correct |
20 |
Correct |
82 ms |
18584 KB |
Output is correct |
21 |
Correct |
114 ms |
18584 KB |
Output is correct |
22 |
Correct |
152 ms |
18456 KB |
Output is correct |
23 |
Correct |
91 ms |
18456 KB |
Output is correct |
24 |
Correct |
156 ms |
18456 KB |
Output is correct |
25 |
Correct |
139 ms |
18584 KB |
Output is correct |
26 |
Correct |
69 ms |
18388 KB |
Output is correct |
27 |
Correct |
83 ms |
18588 KB |
Output is correct |
28 |
Correct |
95 ms |
18456 KB |
Output is correct |
29 |
Correct |
122 ms |
18540 KB |
Output is correct |
30 |
Correct |
175 ms |
18596 KB |
Output is correct |
31 |
Correct |
102 ms |
18544 KB |
Output is correct |
32 |
Correct |
152 ms |
18584 KB |
Output is correct |
33 |
Correct |
136 ms |
18460 KB |
Output is correct |
34 |
Correct |
68 ms |
18388 KB |
Output is correct |
35 |
Correct |
121 ms |
18532 KB |
Output is correct |
36 |
Correct |
120 ms |
18536 KB |
Output is correct |
37 |
Correct |
118 ms |
18428 KB |
Output is correct |
38 |
Correct |
122 ms |
18504 KB |
Output is correct |
39 |
Correct |
118 ms |
18456 KB |
Output is correct |
40 |
Correct |
130 ms |
18428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
3 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 |
376 KB |
Output is correct |
11 |
Correct |
293 ms |
5240 KB |
Output is correct |
12 |
Correct |
762 ms |
4896 KB |
Output is correct |
13 |
Correct |
836 ms |
4300 KB |
Output is correct |
14 |
Correct |
598 ms |
3832 KB |
Output is correct |
15 |
Correct |
423 ms |
4932 KB |
Output is correct |
16 |
Correct |
496 ms |
4060 KB |
Output is correct |
17 |
Correct |
607 ms |
3960 KB |
Output is correct |
18 |
Correct |
222 ms |
5880 KB |
Output is correct |
19 |
Correct |
757 ms |
2936 KB |
Output is correct |
20 |
Correct |
350 ms |
4472 KB |
Output is correct |
21 |
Correct |
410 ms |
4988 KB |
Output is correct |
22 |
Correct |
744 ms |
5112 KB |
Output is correct |
23 |
Correct |
1382 ms |
4148 KB |
Output is correct |
24 |
Correct |
923 ms |
4048 KB |
Output is correct |
25 |
Correct |
538 ms |
5984 KB |
Output is correct |
26 |
Correct |
718 ms |
4544 KB |
Output is correct |
27 |
Correct |
783 ms |
4752 KB |
Output is correct |
28 |
Correct |
243 ms |
7032 KB |
Output is correct |
29 |
Correct |
1263 ms |
3084 KB |
Output is correct |
30 |
Correct |
430 ms |
5188 KB |
Output is correct |
31 |
Correct |
727 ms |
5092 KB |
Output is correct |
32 |
Correct |
699 ms |
4984 KB |
Output is correct |
33 |
Correct |
510 ms |
3996 KB |
Output is correct |
34 |
Correct |
798 ms |
3948 KB |
Output is correct |
35 |
Correct |
779 ms |
4392 KB |
Output is correct |
36 |
Correct |
260 ms |
2936 KB |
Output is correct |
37 |
Correct |
414 ms |
4984 KB |
Output is correct |
38 |
Correct |
970 ms |
3040 KB |
Output is correct |
39 |
Correct |
673 ms |
4200 KB |
Output is correct |
40 |
Correct |
686 ms |
4020 KB |
Output is correct |
41 |
Correct |
81 ms |
18968 KB |
Output is correct |
42 |
Correct |
87 ms |
19232 KB |
Output is correct |
43 |
Correct |
114 ms |
19096 KB |
Output is correct |
44 |
Correct |
159 ms |
18972 KB |
Output is correct |
45 |
Correct |
96 ms |
18892 KB |
Output is correct |
46 |
Correct |
156 ms |
18588 KB |
Output is correct |
47 |
Correct |
137 ms |
18456 KB |
Output is correct |
48 |
Correct |
68 ms |
18712 KB |
Output is correct |
49 |
Correct |
83 ms |
18456 KB |
Output is correct |
50 |
Correct |
82 ms |
18584 KB |
Output is correct |
51 |
Correct |
114 ms |
18584 KB |
Output is correct |
52 |
Correct |
152 ms |
18456 KB |
Output is correct |
53 |
Correct |
91 ms |
18456 KB |
Output is correct |
54 |
Correct |
156 ms |
18456 KB |
Output is correct |
55 |
Correct |
139 ms |
18584 KB |
Output is correct |
56 |
Correct |
69 ms |
18388 KB |
Output is correct |
57 |
Correct |
83 ms |
18588 KB |
Output is correct |
58 |
Correct |
95 ms |
18456 KB |
Output is correct |
59 |
Correct |
122 ms |
18540 KB |
Output is correct |
60 |
Correct |
175 ms |
18596 KB |
Output is correct |
61 |
Correct |
102 ms |
18544 KB |
Output is correct |
62 |
Correct |
152 ms |
18584 KB |
Output is correct |
63 |
Correct |
136 ms |
18460 KB |
Output is correct |
64 |
Correct |
68 ms |
18388 KB |
Output is correct |
65 |
Correct |
121 ms |
18532 KB |
Output is correct |
66 |
Correct |
120 ms |
18536 KB |
Output is correct |
67 |
Correct |
118 ms |
18428 KB |
Output is correct |
68 |
Correct |
122 ms |
18504 KB |
Output is correct |
69 |
Correct |
118 ms |
18456 KB |
Output is correct |
70 |
Correct |
130 ms |
18428 KB |
Output is correct |
71 |
Correct |
701 ms |
22980 KB |
Output is correct |
72 |
Correct |
782 ms |
23168 KB |
Output is correct |
73 |
Correct |
1226 ms |
21656 KB |
Output is correct |
74 |
Execution timed out |
2072 ms |
25488 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |