#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
template <class X, class Y> bool minimize(X &a, Y b) {
if (a > b) return a = b, true;
return false;
}
template <class X, class Y> bool maximize(X &a, Y b) {
if (a < b) return a = b, true;
return false;
}
int lower[1 << 20];
int upper[1 << 20];
int a[1 << 20];
signed main() {
cin.tie(0)->sync_with_stdio(0);
int n, q; cin >> n >> q;
for (int i = 0; i < 1 << n; i ++) {
char c; cin >> c;
a[i] = c - '0';
}
for (int i = 0; i < 1 << n; i ++) {
lower[i] = upper[i] = a[i];
}
for (int i = 0; i < n; i ++) {
for (int mask = 0; mask < 1 << n; mask ++) {
if (mask >> i & 1) {
lower[mask] += lower[mask ^ (1 << i)];
}
}
}
for (int i = 0; i < 1 << n; i ++) {
for (int mask = (1 << n) - 1; ~mask; mask --) {
if (~mask >> i & 1) {
upper[mask] += upper[mask ^ (1 << i)];
}
}
}
while (q --) {
string s; cin >> s;
reverse(s.begin(), s.end());
int mask_a = 0, mask_b = 0, mask_c = 0;
for (int i = 0; i < n; i ++) {
if (s[i] == '0') {
mask_a |= 1 << i;
}
if (s[i] == '1') {
mask_b |= 1 << i;
}
if (s[i] == '?') {
mask_c |= 1 << i;
}
}
int res = a[mask_b];
if (__builtin_popcount(mask_c) <= 6) {
for (int mask = mask_c; mask > 0; mask = (mask - 1) & mask_c) {
res += a[mask_b | mask];
}
} else
if (__builtin_popcount(mask_a) <= 6) {
for (int mask = mask_a; mask > 0; mask = (mask - 1) & mask_a) {
int cnt = __builtin_popcount(mask);
res += ((cnt & 1) == (__builtin_popcount(mask_a) & 1) ? -1 : 1) * upper[mask | mask_b];
}
} else {
for (int mask = mask_b; mask > 0; mask = (mask - 1) & mask_b) {
int cnt = __builtin_popcount(mask);
res += ((cnt & 1) == (__builtin_popcount(mask_b) & 1) ? -1 : 1) * lower[mask | mask_c];
}
}
cout << res << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |