제출 #1298118

#제출 시각아이디문제언어결과실행 시간메모리
1298118BahaminSnake Escaping (JOI18_snake_escaping)C++20
100 / 100
544 ms21748 KiB
#include <bits/stdc++.h>

using namespace std;

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }

#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#define sui cout.tie(NULL); cin.tie(NULL); ios_base::sync_with_stdio(false)
const int MAX_N = 1e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;
const int LOG = 6;

int sum[(1 << LOG)];

void solve() {
    int n, q;
    cin >> n >> q;
    string s;
    cin >> s;
    int a[(1 << n)];
    int dp[(1 << n)];
    int dp2[(1 << n)];
    for (int i = 0; i < (1 << n); i++) dp[i] = dp2[i] = a[i] = s[i] - '0';
    for (int i = 0; i < n; i++) 
    {
        for (int j = (1 << n) - 1; j >= 0; j--) if (j & (1 << i)) dp[j] += dp[j ^ (1 << i)];
        for (int j = 0; j < (1 << n); j++) if (!(j & (1 << i))) dp2[j] += dp2[j ^ (1 << i)];
    }
    while (q--)
    {
        string t;
        cin >> t;
        reverse(all(t));
        vector<int> al;
        int sum1 = 0;
        int sum0 = 0;
        for (int i = 0; i < n; i++)
        {
            if (t[i] == '?') al.push_back(i);
            else if (t[i] == '1') sum1 += (1 << i);
            else sum0 += (1 << i);
        }
        if ((int) al.size() <= LOG)
        {
            int ans = a[sum1];
            sum[0] = 0;
            for (int j = 1; j < (1 << al.size()); j++) 
            {
                int ind = 31 - __builtin_clz(j);
                sum[j] = sum[j - (1 << ind)] ^ (1 << al[ind]);
                ans += a[sum[j] + sum1];
            }
            cout << ans << "\n";
        } else if (__builtin_popcount(sum1) <= LOG)
        {
            int num1 = 0;
            for (int x : al) num1 ^= (1 << x);
            int ans = 0;
            int num2 = sum1;
            while (num2)
            {
                ans += (__builtin_popcount(num2 ^ sum1) & 1 ? -1 : 1) * dp[num1 + num2];
                num2 = (num2 - 1) & sum1;
            }
            ans += (__builtin_popcount(num2 ^ sum1) & 1 ? -1 : 1) * dp[num1 + num2];
            cout << ans << "\n";
        } else 
        {
            int ans = 0;
            int num2 = sum0;
            while (num2)
            {
                ans += (__builtin_popcount(num2) & 1 ? -1 : 1) * dp2[num2 + sum1];
                num2 = (num2 - 1) & sum0;
            }
            ans += (__builtin_popcount(num2) & 1 ? -1 : 1) * dp2[num2 + sum1];
            cout << ans << "\n";
        }
    }
}

int main() {
    sui;
    int tc = 1;
    //cin >> tc;
    for (int t = 1; t <= tc; t++) {
        solve();
    }
}
#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...