| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 1278653 | wedonttalkanymore | Snake Escaping (JOI18_snake_escaping) | C++20 | 1 ms | 332 KiB | 
#include <bits/stdc++.h>
/*
    Wake up, I'm wake up
    Thu sang roi, em thay khong?
*/
using namespace std;
using ll = long long;
#define int long long
#define pii pair<ll, ll>
#define fi first
#define se second
const ll N = 2e5 + 5, inf = 1e18, mod = 1e9 + 7, block = 320, lim = 20;
int L, q, n;
string s;
int submask[1 << lim], supermask[1 << lim], sum[1 << lim];
void solve(string t) {
    int sz = t.size();
    reverse(t.begin(), t.end());
    int cnt0 = 0, cnt1 = 0, cntx = 0;
    int mask0 = 0, mask1 = 0, maskx = 0;
    for (int i = 0; i < sz; i++) {
        if (t[i] == '0') cnt0++, mask0 |= (1 << i);
        if (t[i] == '1') cnt1++, mask1 |= (1 << i);
        if (t[i] == '?') cntx++, maskx |= (1 << i);
    }
    if (cntx <= 6) {
        int res = sum[mask1];
        for (int sub = maskx; sub > 0; sub = (sub - 1) & maskx) {
            res += sum[mask1 | sub];
        }
        cout << res << '\n';
    }
    else if (cnt0 <= 6) {
        int res = supermask[mask1];
        for (int sub = mask0; sub > 0; sub = (sub - 1) & mask0) {
            int t = __builtin_popcountll(sub);
            if (t % 2 == 0) {
                res += supermask[sub | mask1];
            }
            else {
                res -= supermask[sub | mask1];
            }
        }
        cout << res << '\n';
    }
    else {
        int res = submask[maskx] * (cnt1 % 2 ? 1 : -1);
        for (int sub = mask1; sub > 0; sub = (sub - 1) & mask1) {
            int tt = __builtin_popcountll(sub);
            int remain = cnt1 - tt;
            if (remain % 2 == 0) {
                res += submask[maskx | sub];
            }
            else {
                res -= submask[maskx | sub];
            }
        }
        cout << res << '\n';
    }
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    if (fopen(".inp", "r")) {
        freopen(".inp", "r", stdin);
        freopen(".out", "w", stdout);
    }
    cin >> L >> q >> s;
    n = s.size();
    for (int i = 0; i < n; i++) {
        sum[i] = submask[i] = supermask[i] = s[i] - '0';
    }
    for (int i = 0; i < n; i++) {
        for (int mask = 0; mask < (1 << n); mask++) {
            if ((mask >> i) & 1) {
                submask[mask] += submask[mask ^ (1 << i)];
            }
            else {
                supermask[mask] += supermask[mask ^ (1 << i)];
            }
        }
    }
    for (int i = 1; i <= q; i++) {
        string t;
        cin >> t;
        solve(t);
    }
    return 0;
}
Compilation message (stderr)
| # | 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... | ||||
