Submission #900559

# Submission time Handle Problem Language Result Execution time Memory
900559 2024-01-08T14:22:44 Z oviyan_gandhi Snake Escaping (JOI18_snake_escaping) C++17
0 / 100
18 ms 65536 KB
#include <bits/stdc++.h>
using namespace std;

#define MAXL 21
#define MAXN 1048576
int n, q;
string s;
int dp[MAXL][MAXN], sub1[MAXN];

int flip(int x){
    return ~x ^ ~((1 << n) - 1);
}

int curr = 0;
int brute(string t, int pos = 0){
    if (pos == 0)
        curr = 0;
    if (pos == n)
        return s[curr] - '0';
    int ret = 0;
    if (t[pos] != '1')
        ret += brute(t, pos+1);
    if (t[pos] != '0'){
        curr |= (1 << pos);
        ret += brute(t, pos+1);
        curr ^= (1 << pos);
    }
    return ret;
}

void sos(bool one){
    memset(dp, 0, sizeof(dp));
    for (int mask = 0; mask < (1 << n); mask++){
        dp[0][mask] = s[one ? mask : flip(mask)]-'0';
        if (mask & 1)
            dp[0][mask] += s[one ? (mask ^ 1) : flip(mask ^ 1)]-'0';
        for (int i = 1; i <= n; i++){
            dp[i][mask] = dp[i-1][mask];
            if (mask & (1 << i))
                dp[i][mask] += dp[i-1][mask ^ (1 << i)];
        }
        if (one) sub1[mask] = dp[n][mask];
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> q >> s;
    sos(1), sos(0);
    while (q--){
        string t; cin >> t;
        reverse(t.begin(), t.end());
        int cnt[]{0, 0, 0};
        for (char c : t){
            if (c == '?')
                cnt[0]++;
            else cnt[1+c-'0']++;
        }
        int least = min_element(cnt, cnt+3) - cnt - 1;
        if (least == -1){
            cout << brute(t) << '\n';
            continue;
        }
        int m = cnt[least+1], omask = 0;
        vector<int> pos;
        for (int i = 0; i < n; i++){
            if (t[i] == '?' || t[i] == ('0' + least))
                omask |= (1 << i);
            if (t[i] == ('0' + least))
                pos.push_back(i);
        }
        int *sub = (least ? sub1 : dp[n]);
        int ans = 0;
        for (int i = 0; i < (1 << m); i++){
            int mask = omask;
            for (int j = 0; j < m; j++)
                if (i & (1 << j))
                    mask ^= (1 << pos[j]);
            if (__builtin_popcount(i) % 2) ans -= sub[mask];
            else ans += sub[mask];
        }
        cout << ans << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Runtime error 18 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 18 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 18 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 18 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 18 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -