#include <bits/stdc++.h>
#define startt ios_base::sync_with_stdio(false);cin.tie(0);
//#pragma GCC optimize("Ofast")
typedef long long ll;
using namespace std;
#define vint vector<int>
#define all(v) v.begin(), v.end()
#define MOD 1000000007
#define MOD2 998244353
#define MX 1000000000
#define MXL 1000000000000000000
#define PI (ld)2*acos(0.0)
#define pb push_back
#define sc second
#define fr first
//#define int long long
#define endl '\n'
#define ld long double
#define NO cout << "NO" << endl
#define YES cout << "YES" << endl
int ceildiv(int one, int two) {if (one % two == 0) {return one / two;}else {return one / two + 1;}} int power(int n, int pow, int m) {if (pow == 0) return 1;if (pow % 2 == 0) {ll x = power(n, pow / 2, m);return (x * x) % m;}else return (power(n, pow - 1, m) * n) % m;} int gcd(int a, int b) { if (!b)return a; return gcd(b, a % b);}int lcm(int a, int b) {return (a * b) / gcd(a, b);} vector<int> read(int n) {vector<int> a; for (int i = 0; i < n; i++) { int x; cin >> x; a.pb(x);} return a;}//mesanu
int n, q;
string s;
int answer_C(string t, int qm)
{
vector<int> pos;
int base = 0;
for(int i = 0; i < n; i++)
{
if(t[i] == '1')
{
pos.pb(i);
}
if(t[i] == '?')
{
base+=1<<i;
}
}
int sum = 0;
for(int i = 0; i < 1<<qm; i++)
{
int now = base;
for(int j = 0; j < pos.size(); j++)
{
if(i & (1<<j))
{
now+=1<<pos[j];
}
}
sum+=(s[now]-'0');
}
return sum;
}
int sub[(1<<20)+5];
int super[(1<<20)+5];
int answer_B(string t)
{
vector<int> pos;
int base = 0;
for(int i = 0; i < n; i++)
{
if(t[i] == '1')
{
pos.pb(i);
}
if(t[i] == '?')
{
base+=1<<i;
}
}
int sum = 0;
int sz = pos.size();
for(int mask = (1<<sz)-1; mask >= 0; mask--)
{
int now = base;
for(int i = 0; i < pos.size(); i++)
{
if(mask & (1<<i))
{
now+=1<<pos[i];
}
}
sum+=sub[now]*power(-1, sz-__builtin_popcount(mask), MOD);
}
return sum;
}
int answer_A(string t)
{
vector<int> pos;
int base = 0;
for(int i = 0; i < n; i++)
{
if(t[i] == '0')
{
pos.pb(i);
}
if(t[i] == '1')
{
base+=1<<i;
}
}
int sum = 0;
int sz = pos.size();
for(int mask = 0; mask < 1<<sz; mask++)
{
int now = base;
for(int i = 0; i < pos.size(); i++)
{
if(mask & (1<<i))
{
now+=1<<pos[i];
}
}
sum+=super[now]*power(-1, __builtin_popcount(mask), MOD);
}
return sum;
}
void solve()
{
cin >> n >> q;
cin >> s;
for(int i = 0; i < 1<<n; i++)
{
sub[i] = (s[i]-'0');
super[i] = (s[i]-'0');
}
for(int i = 0; i < n; i++)
{
for(int mask = 0; mask < 1<< n; mask++)
{
if(mask & 1<<i)
{
sub[mask]+=sub[mask^(1<<i)];
}
}
}
for(int i = n-1; i >= 0; i--)
{
for(int mask = (1<<n)-1; mask >= 0; mask--)
{
if(!(mask&(1<<i)))
{
super[mask]+=super[mask^(1<<i)];
}
}
}
while(q--)
{
string t;
cin >> t;
reverse(all(t));
int qm = 0, ones = 0, zeros = 0;
for(int i = 0; i < n; i++)
{
if(t[i] == '?'){qm++;}
if(t[i] == '1'){ones++;}
if(t[i] == '0'){zeros++;}
}
//cout << answer_A(t) << endl;
if(ones == min({qm, ones, zeros}))
{
cout << answer_B(t) << endl;
}
else if(qm == min({qm, ones, zeros}))
{
cout << answer_C(t, qm) << endl;
}
else
{
cout << answer_A(t) << endl;
}
}
}
int32_t main(){
startt
int t = 1;
//cin >> t;
while (t--) {
solve();
}
}
/*
3 5
12345678
000
0??
1?0
?11
???
*/
Compilation message
snake_escaping.cpp: In function 'int answer_C(std::string, int)':
snake_escaping.cpp:45:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int j = 0; j < pos.size(); j++)
| ~~^~~~~~~~~~~~
snake_escaping.cpp: In function 'int answer_B(std::string)':
snake_escaping.cpp:80:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for(int i = 0; i < pos.size(); i++)
| ~~^~~~~~~~~~~~
snake_escaping.cpp: In function 'int answer_A(std::string)':
snake_escaping.cpp:112:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
112 | for(int i = 0; i < pos.size(); i++)
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |