This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |