| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 437214 | cpp219 | Snake Escaping (JOI18_snake_escaping) | C++14 | 2077 ms | 17712 KiB | 
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimization O2
#pragma GCC optimization "unroll-loop"
#pragma target ("avx2")
#include <bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
using namespace std;
typedef pair<ll,ll> LL;
const ll N = (1 << 20) + 9;
const ll inf = 1e9 + 7;
char c;
string s;
ll a[N];
ll L,Q,dp[N],dpR[N];
bool bit(ll x,ll i){
    return (x >> i) & 1;
}
ll take,now,ans;
void fQ(ll pos){
    if (pos >= L){
        //cout<<now<<"x\n";
        ans += a[now]; return;
    }
    if (s[pos] == '1'){
        now = now*2 + 1; fQ(pos + 1); now = (now - 1)/2;
    }
    else if (s[pos] == '0'){
        now = now*2; fQ(pos + 1); now /= 2;
    }
    else{
        now = now*2; fQ(pos + 1); now /= 2;
        now = now*2 + 1; fQ(pos + 1); now = (now - 1)/2;
    }
}
void f1(ll pos){
    if (pos >= L){
        //cout<<now<<"x\n";
        if (take%2 == 0) ans += dp[now];
        else ans -= dp[now];
        return;
    }
    if (s[pos] == '1'){
        now = now*2; take++; f1(pos + 1); now /= 2; take--;
        now = now*2 + 1; f1(pos + 1); now = (now - 1)/2;
    }
    else if (s[pos] == '0'){
        now = now*2; f1(pos + 1); now /= 2;
    }
    else{
        now = now*2 + 1; f1(pos + 1); now = (now - 1)/2;
    }
}
void f0(ll pos){
    if (pos >= L){
        if (take%2 == 0) ans += dpR[now];
        else ans -= dpR[now];
        return;
    }
    if (s[pos] == '1'){
        now = now*2 + 1; f0(pos + 1); now = (now - 1)/2;
    }
    else if (s[pos] == '0'){
        now = now*2 + 1; take++; f0(pos + 1); now = (now - 1)/2; take--;
        now = now*2; f0(pos + 1); now /= 2;
    }
    else{
        now = now*2; f0(pos + 1); now /= 2;
    }
}
int main(){
    ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
    #define task "test"
    if (fopen(task".INP","r")){
        freopen(task".INP","r",stdin);
        //freopen(task".OUT","w",stdout);
    }
    cin>>L>>Q;
    for (ll i = 0;i < (1 << L);i++){
        cin>>c; a[i] = c - '0';
        dp[i] += a[i]; dpR[i] += a[i];
    }
    for (ll i = 0;i < L;i++){
        for (ll mask = 0;mask < (1 << L);mask++)
            if (bit(mask,i)) dp[mask] += dp[mask ^ (1 << i)];
        for (ll mask = (1 << L) - 1;mask >= 0;mask--)
            if (!bit(mask,i)) dpR[mask] += dpR[mask ^ (1 << i)];
    }
    while(Q--){
        cin>>s; ll cnt1 = 0,cnt0 = 0,cntQ = 0; take = now = ans = 0;
        for (ll i = 0;i < L;i++){
            if (s[i] == '1') cnt1++;
            else if (s[i] == '0') cnt0++;
            else cntQ++;
        }
        if (cntQ <= 6) fQ(0);
        else if (cnt1 <= 6) f1(0);
        else f0(0);
        cout<<ans<<"\n";
    }
}
Compilation message (stderr)
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
