Submission #1164396

#TimeUsernameProblemLanguageResultExecution timeMemory
1164396hahahoang132Snake Escaping (JOI18_snake_escaping)C++20
100 / 100
790 ms32972 KiB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ll N = 2e6 + 5;
const ll mod = 1e9 + 7;
const ll inf = LLONG_MAX/5;
#define bit(x,y) ((x >> y) & 1)
ll a[N],f0[N],f1[N];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll n,q;
    cin >> n >> q;
    ll  i,j;
    for(i = 0;i < (1 << n);i++)
    {
        char tmp;
        cin >> tmp;
        tmp -= '0';
        a[i] = tmp;
    }
    //1
    for(i = 0;i < (1 << n);i++) f0[i] = f1[i] = a[i];
    for(i = 0;i < n;i++)
    {
        for(j = 0;j < (1 << n);j++)
        {
            if(bit(j,i)) f1[j] += f1[j ^ (1 << i)];
        }
    }
    //0
    for(i = 0;i < n;i++)
    {
        for(j = 0;j < (1 << n);j++)
        {
            if(!bit(j,i)) f0[j] += f0[j ^ (1 << i)];
        }
    }
    vector<ll> haha;
    while(q--)
    {
        ll c0 = 0,c1 = 0,c = 0;
        string tmp;
        cin >> tmp;
        for(i = 0;i < tmp.size();i++)
        {
            if(tmp[i] == '0') c0++;
            if(tmp[i] == '1') c1++;
            if(tmp[i] == '?') c++;
        }
        if(c1 <= 6)
        {
            haha.clear();
            ll m = 0;
            for(i = tmp.size() - 1;i >= 0;i--)
            {
                if(tmp[i] == '1') haha.push_back(tmp.size() - 1 - i);
                if(tmp[i] == '?') m = m | (1 << (tmp.size() - 1 - i));
            }
            ll ans = 0;
            for(i = 0;i < (1 << haha.size());i++)
            {
                ll tm = 0;
                ll lost = 0;
                for(j = 0;j < haha.size();j++)
                {
                    if(bit(i,j)) tm |= (1 << haha[j]);
                    else lost++;
                }
                tm |= m;
                if(lost % 2 == 0) ans += f1[tm];
                else ans -= f1[tm];
            }
            cout << ans << "\n";
        }else
        {
            if(c0 <= 6)
            {
                haha.clear();
                ll m = (1 << n) - 1;
                for(i = tmp.size() - 1;i >= 0;i--)
                {
                    if(tmp[i] == '0') haha.push_back(tmp.size() - 1 - i);
                    if(tmp[i] == '?') m ^= (1 << (tmp.size() - 1 - i));
                }
                ll ans = 0;
                for(i = 0;i < (1 << haha.size());i++)
                {
                    ll tm = (1 << n) - 1;
                    ll lost = 0;
                    for(j = 0;j < haha.size();j++)
                    {
                        if(bit(i,j)) tm ^= (1 << haha[j]);
                        else lost++;
                    }
                    tm &= m;
                    if(lost % 2 == 0) ans += f0[tm];
                    else ans -= f0[tm];
                }
                cout << ans << "\n";
            }else
            {
                haha.clear();
                ll m = 0;
                for(i = tmp.size() - 1;i >= 0;i--)
                {
                    if(tmp[i] == '?')  haha.push_back(tmp.size() - 1 - i);
                    else m |= (tmp[i] - '0') << (tmp.size() - 1 - i);
                }
                ll ans = 0;
                for(i = 0;i < (1 << haha.size());i++)
                {
                    ll tm = 0;
                    for(j = 0;j < haha.size();j++)
                    {
                        if(bit(i,j)) tm |= (1 << haha[j]);
                    }
                    tm |= m;
                    ans += a[tm];
                }
                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...