#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];
char 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 ++) {
cin >> a[i];
}
for (int i = 0; i < 1 << n; i ++) {
lower[i] = upper[i] = a[i] - '0';
}
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 < 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 = 0;
if (__builtin_popcount(mask_c) <= 6) {
for (int mask = mask_c; ~mask; mask = (mask - 1) & mask_c) {
res += a[mask_b | mask] - '0';
if (mask == 0) {
break;
}
}
} else
if (__builtin_popcount(mask_a) <= 6) {
for (int mask = mask_a; ~mask; mask = (mask - 1) & mask_a) {
int cnt = __builtin_popcount(mask);
res += ((cnt & 1) == (__builtin_popcount(mask_a) & 1) ? 1 : -1) * upper[mask | mask_b];
if (mask == 0) {
break;
}
}
} else {
for (int mask = mask_b; ~mask; mask = (mask - 1) & mask_b) {
int cnt = __builtin_popcount(mask);
res += ((cnt & 1) == (__builtin_popcount(mask_b) & 1) ? 1 : -1) * lower[mask | mask_c];
if (mask == 0) {
break;
}
}
}
cout << res << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |