#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 1e6 + 2;
int f[1 << 20];
int ans[N];
unsigned char type[N]; // 255 if ?, otherwise 0/1 for majority
short masks[1 << 20];
int get_mask(const string& s, char target)
{
int mask = 0;
for (auto ch : s) {
mask <<= 1;
mask |= ch == target;
}
return mask;
}
int main()
{
cin.tie(NULL); ios_base::sync_with_stdio(false);
int l, q; cin >> l >> q;
string toxicity; cin >> toxicity;
for (int i = 0; i < q; ++i) {
string s; cin >> s;
int maskQ = get_mask(s, '?');
int mask0 = get_mask(s, '0');
int mask1 = get_mask(s, '1');
masks[i] = (maskQ << 10) | (mask1 << 5) | mask0;
int cnt[3] = {};
for (auto ch : s)
++cnt[ch == '?' ? 2 : ch - '0'];
int mn = *min_element(cnt, cnt + 3);
if (cnt[2] == mn) {
int maskQ = masks[i] >> 10;
int mask1 = (masks[i] >> 5) & 31;
for (int submask = maskQ; submask > 0; submask = (submask - 1) & maskQ)
ans[i] += toxicity[mask1 | submask] - '0';
ans[i] += toxicity[mask1] - '0'; // all 0
type[i] = 255;
}
else
type[i] = cnt[0] > cnt[1];
}
transform(toxicity.begin(), toxicity.end(), f, [](char ch) { return ch - '0'; });
for (int b = 0; b < l; ++b)
for (int i = 0; i < 1 << l; ++i)
if ((i >> b) & 1)
f[i] += f[i ^ (1 << b)];
for (int i = 0; i < q; ++i)
if (type[i] == 1) {
int maskQ = masks[i] >> 10;
int mask1 = (masks[i] >> 5) & 31;
for (int submask = mask1; submask > 0; submask = (submask - 1) & mask1) // mask of 0 within 1
ans[i] += f[maskQ | (mask1 ^ submask)] * (__builtin_popcount(submask) & 1 ? -1 : 1);
ans[i] += f[maskQ | mask1]; // base: all 1 are ?
}
transform(toxicity.begin(), toxicity.end(), f, [](char ch) { return ch - '0'; });
for (int b = 0; b < l; ++b)
for (int i = (1 << l) - 1; i >= 0; --i)
if (!((i >> b) & 1))
f[i] += f[i ^ (1 << b)];
int fullmask = (1 << l) - 1;
for (int i = 0; i < q; ++i)
if (type[i] == 0) {
int maskQ = masks[i] >> 10;
int mask0 = masks[i] & 31;
for (int submask = mask0; submask > 0; submask = (submask - 1) & mask0) // mask of 1 within 0
ans[i] += f[fullmask ^ (maskQ | (mask0 ^ submask))] * (__builtin_popcount(submask) & 1 ? -1 : 1);
ans[i] += f[fullmask ^ (maskQ | mask0)]; // base: all 0 are ?
}
for (int i = 0; i < q; ++i) cout << ans[i] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |