Submission #540198

#TimeUsernameProblemLanguageResultExecution timeMemory
540198browntoadSnake Escaping (JOI18_snake_escaping)C++14
100 / 100
981 ms55792 KiB
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast", "unroll-loops")
using namespace std;
#define ll long long
#define int ll
#define FOR(i,a,b) for (int i = (a); i<(b); i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n+1)
#define RREP(i,n) for (int i=(n)-1; i>=0; i--)
#define RREP1(i,n) for (int i=(n); i>=1; i--)
#define f first
#define s second
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)(x.size())
#define SQ(x) (x)*(x)
#define pii pair<int, int>
#define pdd pair<double ,double>
#define pcc pair<char, char> 
#define endl '\n'
//#define TOAD
#ifdef TOAD
#define bug(x) cerr<<__LINE__<<": "<<#x<<" is "<<x<<endl
#define IOS()
#else
#define bug(...)
#define IOS() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#endif
 
const ll inf = 1ll<<60;
const int iinf=2147483647;
const ll mod = 1e9+7;
const ll maxn=3005;
const double PI=acos(-1);
 
ll pw(ll x, ll p, ll m=mod){
    ll ret=1;
    while (p>0){
        if (p&1){
            ret*=x;
            ret%=m;
        }
        x*=x;
        x%=m;
        p>>=1;
    }
    return ret;
}
 
ll inv(ll a, ll m=mod){
    return pw(a,m-2);
}
 
//=======================================================================================
int n, res=0;
vector<int> a, b, c;
signed main (){
    IOS();
    int n,q; cin>>n>>q;
    int n2=(1<<n);
    vector<int> dp(1<<n), rdp(1<<n), val(1<<n);
    vector<bool> par(1<<n);
    REP(i,(1<<n)){
        par[i]=__builtin_popcount(i)%2;
    }
    string str; cin>>str;
    REP(i, (1<<n)){
        val[i]=str[i]-'0';
        dp[i]=val[i];
        rdp[i]=val[i];
    }
    REP(i, n){
        REP(j, (1<<n)){
            if (j&(1<<i)){
                dp[j]+=dp[j-(1<<i)];
            }
        }
    }
    REP(i, n){
        RREP(j, (1<<n)){
            if ((j&(1<<i))==0){
                rdp[j]+=rdp[j+(1<<i)];
            }
        }
    }
    REP(i, q){
        a.clear(); b.clear(); c.clear();
        res=0;
        string in; cin>>in;
        reverse(ALL(in));
        int ta=0, tb=0, tc=0;
        REP(j, SZ(in)){
            if (in[j]=='?') {
                a.pb(j);
                ta+=(1<<j);
            }
            else if (in[j]=='0') {
                b.pb(j);
                tb+=(1<<j);
            }
            else {
                c.pb(j);
                tc+=(1<<j);
            }
        }
        if (SZ(a)<=n/3){
            for (int j=ta; j>0; j=(j-1)&ta){
                res+=val[j+tc];
            }
            res+=val[tc];
            cout<<res<<endl;
        }
        else if (SZ(c)<=n/3){
            for (int j=tc; j>0; j=(j-1)&tc){
                res+=dp[j+ta]*(par[j+ta]*2-1);
            }
            res+=dp[ta]*(par[ta]*2-1);
            res=abs(res);
            cout<<res<<endl;
        }
        
        else {
            bug(rdp[2]);
            bug(rdp[6]);
            for (int j=tb; j>0; j=(j-1)&tb){
                res+=rdp[j+tc]*(par[j+tc]*2-1);
            }
            res+=rdp[tc]*(par[tc]*2-1);
            res=abs(res);
            cout<<res<<endl;
        }
    }
}   

Compilation message (stderr)

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:60:9: warning: unused variable 'n2' [-Wunused-variable]
   60 |     int n2=(1<<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...