제출 #262917

#제출 시각아이디문제언어결과실행 시간메모리
262917AM1648Snake Escaping (JOI18_snake_escaping)C++17
100 / 100
1663 ms39292 KiB
/* be name khoda */

// #define long_enable
#include <iostream>
#include <algorithm>
#include <cstring>
#include <numeric>
#include <vector>
#include <fstream>
#include <set>
#include <map>
using namespace std;

#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef pair<pii, ll> ppi;
typedef pair<ll, pii> pip;
typedef vector<ll> vi;
typedef vector<pii> vpii;

#define forifrom(i, s, n) for (ll i = s; i < n; ++i)
#define forirto(i, n, e) for (ll i = (n) - 1; i >= e; --i)
#define fori(i, n) forifrom (i, 0, n)
#define forir(i, n) forirto (i, n, 0)

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define debug(x) cout << #x << " -> " << (x) << endl
#define debug2(x, y) cout << #x << ' ' << #y << " -> " << (x) << ' ' << (y) << endl
#define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> " << (x) << ' ' << (y) << ' ' << (z) << endl
#define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> " << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << endl
#define debuga(x, n) cout << #x << " -> "; fori (i1_da, n) { cout << (x)[i1_da] << ' '; } cout << endl
#define debugaa(x, n, m) cout << #x << " ->\n"; fori (i1_daa, n) { fori (i2_daa, m) { cout << (x)[i1_daa][i2_daa] << ' '; } cout << '\n'; } cout << endl

const ll MOD = 1000000007;
const ll INF = 2000000000;
const long long BIG = 1446803456761533460LL;

#define XL (x << 1)
#define XR (XL | 1)
#define MD ((l + r) >> 1)
#define Add(a, b) a = ((a) + (b)) % MOD
#define Mul(a, b) a = (1LL * (a) * (b)) % MOD

// -----------------------------------------------------------------------

const ll maxn = 20;
const ll maxn2 = 1 << maxn;

ll L, N, Q, dp[2][maxn2]; // dp[1] is over reversed S
string S; // inc-exc SOS over L/3 of masks

void calc(string &S, ll *dp) {
    fori (i, N) dp[i] = S[i] - '0';
    fori (i, L) forir (j, N) {
        if (j >> i & 1) dp[j] += dp[j ^ (1 << i)];
    }
}

ll inc_exc(string &q, ll *dp) {
    ll a = 0, t = 0;
    fori (i, L) {
        if (q[i] == '?') t ^= 1 << (L - 1 - i);
        else if (q[i] == '1') a ^= 1 << (L - 1 - i);
    }
    ll s = dp[t ^ a];
    for (ll b = a; b; b = (b - 1) & a) {
        ll pc = __builtin_popcount(b);
        ll v = dp[t ^ a ^ b];
        if (pc % 2) s -= v;
        else s += v;
    }
    return s;
}

ll naive(string &q) {
    ll a = 0, t = 0;
    fori (i, L) {
        if (q[i] == '?') a ^= 1 << (L - 1 - i);
        else if (q[i] == '1') t ^= 1 << (L - 1 - i);
    }
    ll s = S[t] - '0';
    for (ll b = a; b; b = (b - 1) & a) {
        s += S[t ^ b] - '0';
    }
    return s;
}

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    cin >> L >> Q >> S;
    N = 1 << L;
    calc(S, dp[0]);
    reverse(all(S));
    calc(S, dp[1]);
    reverse(all(S));
    fori (qi, Q) {
        string q; cin >> q;
        ll c[3] = {0, 0, 0};
        fori (i, L) {
            if (q[i] == '?') c[2]++;
            else c[q[i] - '0']++;
        }
        ll ans = 0;
        if (c[0] <= c[1] && c[0] <= c[2]) {
            fori (i, L) if (q[i] != '?') q[i] ^= 1;
            ans = inc_exc(q, dp[1]);
        } else if (c[1] <= c[0] && c[1] <= c[2]) {
            ans = inc_exc(q, dp[0]);
        } else {
            ans = naive(q);
        }
        cout << ans << '\n';
    }

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...