#include <bits/stdc++.h>
using namespace std;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, q; cin >> n >> q;
string s; cin >> s;
for ( auto &v: s ) v -= '0';
vector <int> pw(n + 1, 1);
for ( int i = 1; i <= n; i++ ){
pw[i] = pw[i - 1] * 3;
}
vector <int> dp(pw[n]);
for ( int mask = 0; mask < pw[n]; mask++ ){
bool has_q = false;
for ( int i = 0; i < n; i++ ){
int b = mask / pw[i] % 3;
if ( b == 2 ){
has_q = true;
dp[mask] = dp[mask - pw[i]] + dp[mask - pw[i] * 2];
break;
}
}
if ( !has_q ){
int x = 0;
for ( int i = 0; i < n; i++ ){
int b = mask / pw[i] % 3;
x = x * 2 + b;
}
dp[mask] = s[x];
}
}
while ( q-- ){
string t; cin >> t;
int x = 0;
for ( int i = 0; i < n; i++ ){
x = x + pw[i] * (t[i] == '?' ? 2 : t[i] - '0');
}
cout << dp[x] << '\n';
}
}
# | 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... |