#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<=(int)b;i++)
#define FORD(i,a,b) for(int i=a;i>=(int)b;i--)
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(),a.end()
#define BIT(mask,i) ((mask>>(i))&1)
#define MASK(a) (1LL<<((a)))
#define uni(v) sort(all(v)); v.resize(unique(all(v)) - v.begin())
#define pii pair <int, int>
#define vi vector <int>
#define vl vector <ll>
template <class A,class B>
bool maximize(A &a, const B b)
{
if(a < b){ a = b; return 1;} return 0;
}
template <class A,class B>
bool minimize(A &a, const B b)
{
if(a > b){ a = b; return 1;} return 0;
}
const int maxn = 3e6 + 5;
int n, q;
string s;
ll dp[maxn];
void solve()
{
cin >> n >> q >> s;
FOR(i, 0, MASK(n) - 1) dp[i] = s[i] - '0';
for(int i = 0; i < n; i++) for(int mask = 0; mask < (1 << n); mask++) if(BIT(mask, i))
{
dp[mask]+= dp[mask ^ (1 << i)];
// if(mask == 6) cout << (mask ^ (1 << i)) << "\n";
}
while(q--)
{
string s; cin >> s;
int mask = 0, cnt = 0;
reverse(all(s));
for(char c : s)
{
if(c == '?' || c == '1') mask |= (1 << cnt);
cnt++;
}
ll ans = 0;
vector <int> vec1;
FOR(i, 0, n - 1) if(s[i] == '1') vec1.push_back(i);
FOR(sm, 0, MASK(vec1.size()) - 1)
{
int cnt = __builtin_popcount(sm);
int smm = mask;
FOR(i, 0, vec1.size() - 1) if(BIT(sm, i)) smm ^= (1 << vec1[i]);
if(cnt & 1) ans-= dp[smm];
else ans+= dp[smm];
}
cout << ans << "\n";
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define kieuoanh "kieuoanh"
if(fopen(kieuoanh".inp","r"))
{
freopen(kieuoanh".inp","r",stdin);
freopen(kieuoanh".out","w",stdout);
}
int tst = 1;
// cin >> tst;
while(tst--) solve();
return 0;
}
Compilation message (stderr)
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen(kieuoanh".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | freopen(kieuoanh".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |