This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// JOI18_snake_escaping
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const ll LINF = 1e18 + 100;
#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'
#define MAXN 22
#define BND 0
int n, q;
char s[1 << MAXN];
int sosub[1 << MAXN], sosup[1 << MAXN];
int32_t main(void)
{
fast_io;
cin >> n >> q >> s;
FOR(i, 0, 1 << n) sosub[i] = sosup[i] = s[i] = s[i] - '0';
FOR(i, 0, n)
{
FOR(mask, 0, 1 << n)
if (!(mask >> i & 1)) sosup[mask] += sosup[mask ^ (1 << i)];
for (int mask = (1 << n) - 1; mask >= 0; mask--)
if (mask >> i & 1) sosub[mask] += sosub[mask ^ (1 << i)];
}
while (q--)
{
char t[MAXN];
cin >> t;
int m0 = 0, m1 = 0, mq = 0;
FOR(i, 0, n)
{
if (t[n - i - 1] == '0') m0 |= (1 << i);
else if (t[n - i - 1] == '1') m1 |= (1 << i);
else mq |= (1 << i);
}
int cnt0 = __builtin_popcount(m0);
int cnt1 = __builtin_popcount(m1);
if (n - cnt0 - cnt1 <= BND)
{
int ans = s[m1];
for (int sub = mq; sub; sub = (sub - 1) & mq)
ans += s[m1 | sub];
cout << ans << endl;
continue;
}
if (cnt0 < cnt1)
{
int ans = sosup[m1];
for (int sub = m0; sub; sub = (sub - 1) & m0)
ans += (__builtin_popcount(sub) % 2 ? -1 : 1) * sosup[sub | m1];
cout << ans << endl;
}
else
{
int ans = 0;
for (int sub = m1; ; sub = (sub - 1) & m1)
{
ans += ((cnt1 - __builtin_popcount(sub)) % 2 ? -1 : 1) * sosub[mq | sub];
if (!sub) break;
}
cout << ans << endl;
}
}
return 0;
}
# | 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... |