#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 <string> qu(q);
for ( auto &u: qu ) cin >> u;
int B = min(n, 7), m = n - B;
vector <int> ans(q);
for ( int lf = 0; lf < (1 << B); lf++ ){
vector <int> dp(pw[m]);
for ( int mask = 0; mask < pw[m]; mask++ ){
bool has_q = false;
for ( int i = 0; i < m; i++ ){
int b = mask / pw[i] % 3;
if ( b == 2 ){
dp[mask] = dp[mask - pw[i]] + dp[mask - pw[i] * 2];
has_q = 1; break;
}
}
if ( !has_q ){
int x = 0;
for ( int i = 0; i < m; i++ ){
x = x * 2 + mask / pw[i] % 3;
}
for ( int i = 0; i < B; i++ ) x = x * 2 + (lf >> i & 1);
dp[mask] = s[x];
}
}
for ( int i = 0; i < q; i++ ){
bool bad = false;
string &t = qu[i];
for ( int j = n - 1; j >= n - B; j-- ){
int p = n - 1 - j;
if ( t[j] != '?' && (lf >> (B - p - 1) & 1) != t[j] - '0' ) bad = 1;
}
if ( bad ) continue;
int x = 0;
for ( int j = 0; j < m; j++ ){
x = x + pw[j] * (t[j] == '?' ? 2 : t[j] - '0');
}
ans[i] += dp[x];
}
}
for ( auto &x: ans ) cout << 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... |