This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//khodaya khodet komak kon
# include <bits/stdc++.h>
 
/*
// ordered_set 
# include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
# define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
*/
 
using namespace std;
 
typedef long long                                        ll;
typedef long double                                      ld;
typedef pair <int, int>                                  pii;
typedef pair <pii, int>                                  ppi;
typedef pair <int, pii>                                  pip;
typedef pair <pii, pii>                                  ppp;
typedef pair <ll, ll>                                    pll;
 
# define A                                               first
# define B                                               second
# define endl                                            '\n'
# define sep                                             ' '
# define lc                                              id << 1
# define rc                                              lc | 1
# define all(x)                                          x.begin(), x.end()
# define kill(x)                                         return cout << x << endl, 0
# define SZ(x)                                           int(x.size())
# define InTheNameOfGod                                  ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
 
ll power(ll a, ll b, ll md) {return (!b ? 1 : (b & 1 ? a * power(a * a % md, b / 2, md) % md : power(a * a % md, b / 2, md) % md));}
 
const int xn = 21;
const int xm = - 20 + 10;
const int sq = 320;
const ll inf = 1e18 + 10;
const ll INF = 1e18 + 10;
const int mod = 998244353;
const int base = 257;
int n, q, a[1 << xn], cnt[3], ted[1 << xn];
pii dp[1 << xn];
string s;
void solve3(){
	int mask = 0, tot = 0, ans = 0;
	for (int i = 0; i < n; ++ i){
		if (s[i] == '?')
			mask += (1 << i);
		else
			tot += (s[i] - '0') * (1 << i);
	}
	for (int sub = mask; sub > 0; sub = ((sub - 1) & mask))
		ans += a[tot + sub];
	ans += a[tot];
	cout << ans << endl;
}
void solve2(){
	int tot = 0, ans = 0, mask = 0;
	for (int i = 0; i < n; ++ i){
		if (s[i] != '0')
			tot += (1 << i);
		if (s[i] == '1')
			mask += (1 << i);
	}
	ans = dp[tot].A;
	for (int sub = mask; sub > 0; sub = ((sub - 1) & mask)){
		if (ted[sub] % 2)
			ans -= dp[tot - sub].A;
		else
			ans += dp[tot - sub].A;
	}
	cout << ans << endl;
}
void solve1(){
	int tot = 0, ans = 0, mask = 0;
	for (int i = 0; i < n; ++ i){
		if (s[i] == '1')
			tot += (1 << i);
		else if (s[i] == '0')
			mask += (1 << i);
	}
	ans = dp[tot].B;
	for (int sub = mask; sub > 0; sub = ((sub - 1) & mask)){
		if (ted[sub] % 2)
			ans -= dp[tot + sub].B;
		else
			ans += dp[tot + sub].B;
	}
	cout << ans << endl;
}
int main(){
	InTheNameOfGod;
	
	cin >> n >> q >> s;
	for (int i = 0; i < (1 << n); ++ i)
		a[i] = dp[i].A = dp[i].B = s[i] - '0';
	for (int bit = 0; bit < n; ++ bit){
		for (int mask = (1 << n) - 1; mask >= 0; -- mask)
			if ((mask & (1 << bit)))
				dp[mask].A += dp[mask - (1 << bit)].A;
		for (int mask = 0; mask < (1 << n); ++ mask)
			if (!(mask & (1 << bit)))
				dp[mask].B += dp[mask + (1 << bit)].B;
	}
	for (int mask = 1; mask < (1 << n); ++ mask)
		ted[mask] = ted[mask - (mask & - mask)] ^ 1;
	while (q --){
		cin >> s, reverse(all(s));
		cnt[0] = cnt[1] = cnt[2] = 0;
		for (char c : s){
			if (c == '0')
				++ cnt[0];
			else if (c == '1')
				++ cnt[1];
			else
				++ cnt[2];
		}
		if (cnt[0] <= n / 3)
			solve1();
		else if (cnt[1] <= n / 3)
			solve2();
		else
			solve3();
	}
	
	return 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... |