#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)
{
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);
s[i] = '1';
res += rek(t, s, j + 1);
s[i] = '?';
return res;
}
if (t == 0)
{
s[i] = '?';
res += rek(t, s, j + 1);
s[i] = '1';
res -= rek(t, s, j + 1);
s[i] = '0';
return res;
}
if (t == 1)
{
s[i] = '?';
res += rek(t, s, j + 1);
s[i] = '0';
res -= rek(t, s, j + 1);
s[i] = '1';
return res;
}
}
}
int msk = 0;
for (int i = 0; i < l; ++i)
{
if (s[i] == '0') continue;
if (s[i] == '1' and t == 2)
msk += (1 << i);
if (s[i] == '?')
msk += (1 << i);
}
//cerr << s << ' ' << msk << ' ' << pre[t][msk] << '\n';
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);
cout << rek(0, s, 0) << '\n';
}
else if (zl[1] <= lax / 3)
{
ind.clear();
for (int i = 0; i < l; ++i)
if (s[i] == '1')
ind.push_back(i);
cout << rek(1, s, 0) << '\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);
cout << rek(2, s, 0) << '\n';
}
}
}
Compilation message
snake_escaping.cpp: In function 'int rek(int, std::__cxx11::string&, 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:89:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if ((1 << i) & j)
^~
snake_escaping.cpp:93: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 |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
4 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 |
4 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
4 ms |
380 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
4 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 |
4 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
4 ms |
380 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
313 ms |
4836 KB |
Output is correct |
12 |
Correct |
1850 ms |
4600 KB |
Output is correct |
13 |
Execution timed out |
2070 ms |
10204 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
4 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 |
4 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
4 ms |
380 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
313 ms |
4836 KB |
Output is correct |
12 |
Correct |
1850 ms |
4600 KB |
Output is correct |
13 |
Execution timed out |
2070 ms |
10204 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
4 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 |
4 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
4 ms |
380 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
82 ms |
19580 KB |
Output is correct |
12 |
Correct |
96 ms |
19580 KB |
Output is correct |
13 |
Correct |
236 ms |
19656 KB |
Output is correct |
14 |
Correct |
464 ms |
19608 KB |
Output is correct |
15 |
Correct |
169 ms |
19820 KB |
Output is correct |
16 |
Correct |
526 ms |
19616 KB |
Output is correct |
17 |
Correct |
340 ms |
19608 KB |
Output is correct |
18 |
Correct |
68 ms |
19736 KB |
Output is correct |
19 |
Correct |
79 ms |
19440 KB |
Output is correct |
20 |
Correct |
82 ms |
19608 KB |
Output is correct |
21 |
Correct |
263 ms |
19608 KB |
Output is correct |
22 |
Correct |
551 ms |
19608 KB |
Output is correct |
23 |
Correct |
166 ms |
19480 KB |
Output is correct |
24 |
Correct |
509 ms |
19568 KB |
Output is correct |
25 |
Correct |
346 ms |
19484 KB |
Output is correct |
26 |
Correct |
70 ms |
19480 KB |
Output is correct |
27 |
Correct |
86 ms |
19600 KB |
Output is correct |
28 |
Correct |
104 ms |
19536 KB |
Output is correct |
29 |
Correct |
263 ms |
19764 KB |
Output is correct |
30 |
Correct |
459 ms |
19564 KB |
Output is correct |
31 |
Correct |
153 ms |
19480 KB |
Output is correct |
32 |
Correct |
457 ms |
19612 KB |
Output is correct |
33 |
Correct |
345 ms |
19624 KB |
Output is correct |
34 |
Correct |
70 ms |
19864 KB |
Output is correct |
35 |
Correct |
264 ms |
20120 KB |
Output is correct |
36 |
Correct |
271 ms |
19864 KB |
Output is correct |
37 |
Correct |
270 ms |
20000 KB |
Output is correct |
38 |
Correct |
267 ms |
19888 KB |
Output is correct |
39 |
Correct |
268 ms |
19996 KB |
Output is correct |
40 |
Correct |
268 ms |
19868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
4 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 |
4 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
4 ms |
380 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
313 ms |
4836 KB |
Output is correct |
12 |
Correct |
1850 ms |
4600 KB |
Output is correct |
13 |
Execution timed out |
2070 ms |
10204 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |