#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;
}
int B = min(n, 7), m = n - B;
vector <int> val(q), lst(q);
for ( int i = 0; i < q; i++ ){
string t; cin >> t;
for ( int j = 0; j < m; j++ ){
val[i] = val[i] + pw[j] * (t[j] == '?' ? 2 : t[j] - '0');
}
for ( int j = n - 1; j >= m; j-- ){
lst[i] = lst[i] * 3 + (t[j] == '?' ? 2 : t[j] - '0');
}
}
vector <int> f(pw[m]), pt(pw[m], -1);
for ( int mask = 0; mask < pw[m]; mask++ ){
for ( int i = 0; i < m; i++ ){
f[mask] = f[mask] * 2 + mask / pw[i] % 3;
if ( mask / pw[i] % 3 == 2 ) pt[mask] = i;
}
}
vector <int> ans(q);
for ( int lf = 0; lf < (1 << B); lf++ ){
vector <int> is(pw[B]);
for ( int mask = 0; mask < (1 << B); mask++ ){
int cnt = 0;
for ( int i = 0; i < n; i++ ){
int b = lf >> i & 1;
if ( mask >> i & 1 ) cnt = cnt + pw[i] * 2;
else cnt = cnt + pw[i] * b;
}
is[cnt] = 1;
}
int cur = 0;
for ( int i = 0; i < B; i++ ) cur = cur * 2 + (lf >> i & 1);
vector <int> dp(pw[m]);
for ( int mask = 0; mask < pw[m]; mask++ ){
if ( pt[mask] != -1 ){
dp[mask] = dp[mask - pw[pt[mask]]] + dp[mask - pw[pt[mask]] * 2];
} else dp[mask] = s[(f[mask] << B) + cur];
}
for ( int i = 0; i < q; i++ ){
ans[i] += dp[val[i]] * is[lst[i]];
}
}
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... |