제출 #1348694

#제출 시각아이디문제언어결과실행 시간메모리
1348694iamhereforfunSnake Escaping (JOI18_snake_escaping)C++20
100 / 100
692 ms28964 KiB
// Starcraft 2 enjoyer //

#include <bits/stdc++.h>

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

#define LSOne(X) ((X) & -(X))

const int N = 1e6 + 5;
const int M = 1 << 20;
const int K = 19;
const int LG = 20;
const long long INF = 3e18;
const int C = 26;
const int B = 1000;
const int MOD = 1e9 + 7;

int l, q, val[M], dp0[M][2], dp1[M][2];
vector<int> v0, v1, v2, a, b;

inline void solve()
{
    cin >> l >> q;
    for (int x = 0; x < (1 << l); x++)
    {
        char c;
        cin >> c;
        val[x] = c - '0';
        dp0[x][0] = val[x];
        dp1[(1 << l) - 1 - x][0] = val[x];
    }
    for (int x = 0; x < LG; x++)
    {
        for (int y = 0; y < (1 << l); y++)
        {
            if (y >> x & 1)
            {
                dp0[y][1] += dp0[y][0];
                dp0[y][1] += dp0[y ^ (1 << x)][0];
                dp1[y][1] += dp1[y ^ (1 << x)][0];
                dp1[y][1] += dp1[y][0];
            }
            else
            {
                dp1[y][1] += dp1[y][0];
                dp0[y][1] += dp0[y][0];
            }
        }
        for (int y = 0; y < (1 << l); y++)
        {
            dp0[y][0] = dp0[y][1];
            dp1[y][0] = dp1[y][1];
            dp0[y][1] = dp1[y][1] = 0;
        }
    }
    a.reserve((1 << 8));
    b.reserve((1 << 8));
    for (int x = 0; x < q; x++)
    {
        string s;
        cin >> s;
        reverse(s.begin(), s.end());
        int c0 = 0, c1 = 0, c2 = 0, c = 0;
        long long ans = 0;
        v0.clear();
        v1.clear();
        v2.clear();
        a.clear();
        b.clear();
        a.push_back(0);
        for (int y = 0; y < l; y++)
        {
            char ch = s[y];
            if (ch == '0')
            {
                c0++;
                v0.push_back(y);
            }
            if (ch == '1')
            {
                c1++;
                v1.push_back(y);
            }
            if (ch == '?')
            {
                c2++;
                v2.push_back(y);
            }
        }
        if (c2 <= 7)
        {
            for (int y = 0; y < l; y++)
            {
                char ch = s[y];
                if (ch == '1')
                {
                    c += (1 << y);
                }
            }
            for (int y = 0; y < c2; y++)
            {
                for (auto &i : a)
                {
                    b.push_back(i + (1 << v2[y]));
                    b.push_back(i);
                }
                swap(a, b);
                b.clear();
            }
            for (int &i : a)
            {
                ans += val[c + i];
            }
        }
        else if (c1 <= 7)
        {
            for (int y = 0; y < l; y++)
            {
                char ch = s[y];
                if (ch == '?')
                {
                    c += (1 << y);
                }
            }
            for (int y = 0; y < c1; y++)
            {
                for (auto &i : a)
                {
                    b.emplace_back(i + (1 << v1[y]));
                    b.emplace_back(i);
                }
                swap(a, b);
                b.clear();
            }
            for (int &i : a)
            {
                if (__builtin_popcount(i) % 2 == 1)
                    ans -= dp0[c + i][0];
                else
                    ans += dp0[c + i][0];
            }
        }
        else if (c0 <= 7)
        {
            for (int y = 0; y < l; y++)
            {
                char ch = s[y];
                if (ch == '?')
                {
                    c += (1 << y);
                }
            }
            for (int y = 0; y < c0; y++)
            {
                for (auto &i : a)
                {
                    b.emplace_back(i + (1 << v0[y]));
                    b.emplace_back(i);
                }
                swap(a, b);
                b.clear();
            }
            for (int &i : a)
            {
                if (__builtin_popcount(i) % 2 == 1)
                    ans -= dp1[c + i][0];
                else
                    ans += dp1[c + i][0];
            }
        }
        cout << abs(ans) << "\n";
    }
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    for (int x = 1; x <= t; x++)
    {
        solve();
    }
    return 0;
}
#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...