Submission #255185

#TimeUsernameProblemLanguageResultExecution timeMemory
255185Atill83Snake Escaping (JOI18_snake_escaping)C++14
100 / 100
1795 ms65536 KiB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 1e6;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
int n, l, q;
string s;
int say[MAXN][2];
int ans[MAXN];

const int n37 = 2187;
const int n313 = 1594323;
int child[n313][2];
bool sub[n37][(1<<7)];
ll dp[n313];

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);

    #ifdef Local
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
    #endif

    cin>>l>>q;

    cin>>s;

    for(int i = 0; i < q; i++){
        string t;
        cin>>t;
        for(int j = 0; j < min(l, 7); j++){
            say[i][0] += (t[j] == '?' ? 2 : t[j] - '0');
            say[i][0] *= 3;
        }
        say[i][0] /= 3;
        for(int j = 7; j < l; j++){
            say[i][1] += (t[j] == '?' ? 2 : t[j] - '0');
            say[i][1] *= 3;
        }
        say[i][1] /= 3;
    }

    for(int i = 0; i < n313; i++){
        int cur = i;
        child[i][0] = child[i][1] = -1;
        int cc = 1;
        for(int j = 0; j < 13; j++){
            if(cur % 3 == 2){
                child[i][0] = i - cc;
                child[i][1] = i - 2*cc;
                break;
            }
            cc *= 3;
            cur /= 3;
        }
    }


    for(int i = 0; i < n37; i++){
        for(int j = 0; j < (1<<7); j++){
            int cur1 = i, cur2 = j;
            bool tru = 1;
            for(int k = 0; k < 7; k++){
                if(cur1 % 3 != 2 && cur1 % 3 != cur2 % 2){
                    tru = 0;
                    break;
                }
                cur1 /= 3;
                cur2 /= 2;
            }
            sub[i][j] = tru;
        }
    }

    int rhs = max(0, l - 7);

    for(int j = 0; j < (1<<min(l, 7)); j++){
        
        for(int i = 0; i < n313; i++){
            if(child[i][0] == -1){
                int kal = 0;
                int cur = i;
                for(int k = 0; k < rhs; k++){
                    if(cur % 3) kal += (1<<k);
                    cur /= 3;
                }
                dp[i] = s[(j<<rhs) + kal] - '0';
            }else{
                dp[i] = dp[child[i][0]] + dp[child[i][1]];
            }
        }

        for(int i = 0; i < q; i++){
            if(!sub[say[i][0]][j]) continue;
            ans[i] += dp[say[i][1]];
        }
    }

    for(int i = 0; i < q; i++)
        cout<<ans[i]<<endl;


    

    #ifdef Local
        cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
    #endif
}
#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...