#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int SIZE = 20;
const int MAX = 1 << SIZE;
int n, m, q;
int a[MAX];
string s;
int dp0[MAX], dp1[MAX];
void solve() {
cin >> n >> q;
cin >> s;
m = 1 << n;
FOR (i, 0, m - 1) {
a[i] = s[i] - '0';
dp0[(m - 1) ^ i] = a[i];
dp1[i] = a[i];
}
FOR (i, 0, n - 1) FOR (mask, 0, m - 1) if (mask >> i & 1) {
dp0[mask] += dp0[mask ^ (1 << i)];
dp1[mask] += dp1[mask ^ (1 << i)];
}
while (q--) {
cin >> s;
reverse(s.begin(), s.end());
int cnt[3] = {};
for (char c : s) (c == '0' ? cnt[0] : c == '1' ? cnt[1] : cnt[2])++;
int p;
FOR (i, 0, 2) if (cnt[i] == *min_element(cnt, cnt + 3)) {
p = i;
break;
}
vector<int> v;
FOR (i, 0, n - 1) if ((s[i] == '0' && p == 0) || (s[i] == '1' && p == 1) || (s[i] == '?' && p == 2)) v.pb(i);
int ans = 0;
if (p == 2) {
auto dfs = [&](auto dfs, int i, int mask)->void {
if (i == v.size()) {
ans += a[mask];
return;
}
dfs(dfs, i + 1, mask);
dfs(dfs, i + 1, mask | (1 << v[i]));
};
int mask = 0;
FOR (i, 0, n - 1) if (s[i] == '1') mask |= 1 << i;
dfs(dfs, 0, mask);
} else if (p == 0) {
auto dfs = [&](auto dfs, int i, int mask, int cnt)->void {
if (i == v.size()) {
ans += (cnt & 1 ? -dp0[mask] : dp0[mask]);
return;
}
dfs(dfs, i + 1, mask | (1 << v[i]), cnt - 1);
dfs(dfs, i + 1, mask, cnt);
};
int mask = 0;
FOR (i, 0, n - 1) if (s[i] == '?') mask |= 1 << i;
dfs(dfs, 0, mask, cnt[0]);
} else {
auto dfs = [&](auto dfs, int i, int mask, int cnt)->void {
if (i == v.size()) {
ans += (cnt & 1 ? -dp1[mask] : dp1[mask]);
return;
}
dfs(dfs, i + 1, mask | (1 << i), cnt - 1);
dfs(dfs, i + 1, mask, cnt);
};
int mask = 0;
FOR (i, 0, n - 1) if (s[i] == '?') mask |= 1 << i;
dfs(dfs, 0, mask, cnt[1]);
}
cout << ans << '\n';
}
}
int main() {
Waimai;
solve();
}
Compilation message
snake_escaping.cpp: In instantiation of 'solve()::<lambda(auto:23, int, int)> [with auto:23 = solve()::<lambda(auto:23, int, int)>]':
snake_escaping.cpp:67:29: required from here
snake_escaping.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | if (i == v.size()) {
| ~~^~~~~~~~~~~
snake_escaping.cpp: In instantiation of 'solve()::<lambda(auto:24, int, int, int)> [with auto:24 = solve()::<lambda(auto:24, int, int, int)>]':
snake_escaping.cpp:79:37: required from here
snake_escaping.cpp:70:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | if (i == v.size()) {
| ~~^~~~~~~~~~~
snake_escaping.cpp: In instantiation of 'solve()::<lambda(auto:25, int, int, int)> [with auto:25 = solve()::<lambda(auto:25, int, int, int)>]':
snake_escaping.cpp:91:37: required from here
snake_escaping.cpp:82:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | if (i == v.size()) {
| ~~^~~~~~~~~~~
snake_escaping.cpp: In function 'void solve()':
snake_escaping.cpp:68:16: warning: 'p' may be used uninitialized in this function [-Wmaybe-uninitialized]
68 | } else if (p == 0) {
| ^~
# |
Verdict |
Execution time |
Memory |
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 |
- |
# |
Verdict |
Execution time |
Memory |
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 |
- |
# |
Verdict |
Execution time |
Memory |
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 |
- |
# |
Verdict |
Execution time |
Memory |
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 |
- |
# |
Verdict |
Execution time |
Memory |
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 |
- |