제출 #396724

#제출 시각아이디문제언어결과실행 시간메모리
396724rqiSnake Escaping (JOI18_snake_escaping)C++14
22 / 100
413 ms57572 KiB
#include <bits/stdc++.h>
using namespace std;

#define all(x) begin(x), end(x)

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int mx = 1000005;
int L, Q;
string S;
string T[mx];

int three_pow[15];
int three_sub[1594323];

void smallL(){
    three_pow[0] = 1;
    for(int i = 1; i <= L; i++){
        three_pow[i] = three_pow[i-1]*3;
    }
    for(int i = 0; i < three_pow[L]; i++){
        int sub = i;
        int sum = 0;
        for(int j = 0; j < L; j++){
            if(sub % 3 == 2){
                //cout << i << " " << i-2*three_pow[j] << " " << i-1*three_pow[j] << "\n";
                three_sub[i] = three_sub[i-2*three_pow[j]]+three_sub[i-1*three_pow[j]];
                sum = -1;
                break;
            }
            if(sub % 3 == 1){
                sum+=(1<<j);
            }
            sub/=3;
        }
        if(sum != -1){
            three_sub[i] = S[sum]-'0';
            //cout << i << " " << three_sub[i] << "\n";
        }
    }

    //cout << "three_sub[2]: " << three_sub[8] << "\n";

    for(int i = 1; i <= Q; i++){
        int sub = 0;
        for(int j = 0; j < L; j++){
            if(T[i][j] == '1'){
                sub+=three_pow[j];
            }
            else if(T[i][j] == '?'){
                sub+=2*three_pow[j];
            }
        }
        cout << three_sub[sub] << "\n";
    }
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    cin >> L >> Q;
    cin >> S;
    for(int i = 1; i <= Q; i++){
        cin >> T[i];
        reverse(all(T[i]));
    }

    if(L <= 13){
        smallL();
        exit(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...