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>
#define f first
#define s second
////#define int long long
#define pii pair<int,int>
using namespace std;
const int N = 1048576 + 5, mod = 1e9 + 7; // !
int t,dp1[2][N],dp2[2][N],L,q,cnt[N];
main(){
			ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
            cin >> L >> q;
            string s;
            cin >> s;
            for(int i = 0; i < (1 << L); i++) { 
                        dp1[0][i] = s[i] - '0';
                        dp2[0][i] = s[i] - '0';
            
			}
			for(int j = 0; j < L; j++) {
				for(int i = 0; i < (1 << L); i++) {
                        dp1[1][i] = dp1[0][i];
                        if((1 << j) & i) dp1[1][i] += dp1[1][i ^ (1 << j)];					
				}
				swap(dp1[0],dp1[1]);
			} 
			for(int j = 0; j < L; j++) {
				for(int i = (1 << L) - 1; i >= 0; i--) {
                        dp2[1][i] = dp2[0][i];
                        if(!((1 << j) & i)) dp2[1][i] += dp2[1][i ^ (1 << j)];					
				}
				swap(dp2[0],dp2[1]);
			}
            
            while(q--) {
                        string t;
                        cin >> t;
                        int x = 0;
                        reverse(t.begin(),t.end());
                        int c = 0, a = 0, b = 0;
                        for(int i = 0; i < 3; i++) cnt[i] = 0;
                        for(int i = 0; i < (int)t.size(); i++) {
                                    if(t[i] == '0') cnt[0]++, a += 1 << i;
                                    else if(t[i] == '1') cnt[1]++,b += 1 << i;
                                    else cnt[2]++,c += 1 << i;
                        }
                        long long ans = 0;
                        if(cnt[2] < 7) {
                        		
                                    for(int i = c;; i = (i - 1) & c) {
                                                ans += s[i | b] - '0';
                                                if(i == 0) break;
                                    }
                        }
                        else if(cnt[1] < 7) {	
                                    int p = __builtin_popcount(b) % 2;
                                    for(int i = b;; i = (i - 1) & b) {
                                            ans += (__builtin_popcount(i) % 2 == p ? 1 : -1)
                                                    * dp1[0][i | c];
                                            if(i == 0) break;
                                                    
                                    }
                        }
                        else {
                                    for(int i = a;; i = (i - 1) & a) {
                                            ans += (__builtin_popcount(i) % 2 == 0 ? 1 : -1)
                                                       * dp2[0][i | b];
                                            if(i == 0) break;    
                                    }
                        }
                        cout << ans << "\n";
            }
 }
Compilation message (stderr)
snake_escaping.cpp:9:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
    9 | main(){
      | ^~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:37:29: warning: unused variable 'x' [-Wunused-variable]
   37 |                         int x = 0;
      |                             ^| # | 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... |