This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
string S;
void solve(vector<uint>&V, vector<int>&ans, int l, int r, uint akt) {
	if(!V.size()) return;
	if(l==r) {
		ans[0]=S[l]-'0';
		return;
	}
	int ile1=0, ile2=0, ile3=0;
	rep(i, V.size()) {
		if(V[i]/akt==0) ++ile1;
		else if(V[i]/akt==1) ++ile2;
		else ++ile3;
		V[i]%=akt;
	}
	vector<uint>zero;
	int l1=0, l2=0;
	while(l1<ile1 && l2<ile2) {
		if(V[l1]<=V[ile1+l2]) {
			if(V[l1]==V[ile1+l2]) ++l2;
			zero.pb(V[l1]);
			++l1;
		} else {
			zero.pb(V[ile1+l2]);
			++l2;
		}
	}
	while(l1<ile1) {
		zero.pb(V[l1]); ++l1;
	}
	while(l2<ile2) {
		zero.pb(V[ile1+l2]); ++l2;
	}
	vector<int>zans(zero.size());
	solve(zero, zans, l, (l+r)/2, akt/3);
	l1=0;
	rep(i, ile1) {
		while(zero[l1]<V[i]) ++l1;
		ans[i]+=zans[l1];
	}
	l1=0;
	rep(i, ile2) {
		while(zero[l1]<V[ile1+i]) ++l1;
		ans[ile1+i]+=zans[l1];
	}
	zero.clear();
	zans.clear();
	l1=0; l2=0;
	while(l1<ile1 && l2<ile3) {
		if(V[l1]<=V[ile1+ile2+l2]) {
			if(V[l1]==V[ile1+ile2+l2]) ++l2;
			zero.pb(V[l1]);
			++l1;
		} else {
			zero.pb(V[ile1+ile2+l2]);
			++l2;
		}
	}
	while(l1<ile1) {
		zero.pb(V[l1]); ++l1;
	}
	while(l2<ile3) {
		zero.pb(V[ile1+ile2+l2]); ++l2;
	}
	zans.resize(zero.size(), 0);
	solve(zero, zans, (l+r)/2+1, r, akt/3);
	l1=0;
	rep(i, ile1) {
		while(zero[l1]<V[i]) ++l1;
		ans[i]+=zans[l1];
	}
	l1=0;
	rep(i, ile3) {
		while(zero[l1]<V[ile1+ile2+i]) ++l1;
		ans[ile1+ile2+i]+=zans[l1];
	}
	rep(i, ile2) V[ile1+i]+=akt;
	rep(i, ile3) V[ile1+ile2+i]+=2*akt;
}
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int l, q, n=1;
	cin >> l >> q >> S;
	rep(i, l) n*=2;
	pair<uint,int>T[q];
	rep(i, q) {
		string x;
		cin >> x;
		ll p=0;
		for(auto j : x) {
			p*=3;
			if(j=='0') ++p;
			if(j=='1') p+=2;
		}
		T[i]={p, i};
	}
	sort(T, T+q);
	uint akt=1;
	rep(i, l-1) akt*=3;
	vector<uint>V;
	V.pb(T[0].st);
	for(int i=1; i<q; ++i) if(T[i].st!=T[i-1].st) V.pb(T[i].st);
	vector<int>ans(V.size());
	solve(V, ans, 0, n-1, akt);
	int l1=0;
	rep(i, q) {
		while(V[l1]<T[i].st) ++l1;
		T[i]={T[i].nd, ans[l1]};
	}
	sort(T, T+q);
	rep(i, q) cout << T[i].nd << '\n';
}
Compilation message (stderr)
snake_escaping.cpp: In function 'void solve(std::vector<unsigned int>&, std::vector<int>&, int, int, uint)':
snake_escaping.cpp:6:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<unsigned int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    6 | #define rep(a, b) for(int a = 0; a < (b); ++a)
      |                                    ^
snake_escaping.cpp:19:2: note: in expansion of macro 'rep'
   19 |  rep(i, V.size()) {
      |  ^~~| # | 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... |