#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e6 + 2;
const int mod = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
int m = 1 << n;
string s; cin >> s;
vector<int> sum(m);
for (int i = 0; i < m; i++) sum[i] = s[i] - '0';
for (int j = 0; j < n; j++) {
for (int i = 0; i < m; i++) {
if ((1 << j) & i) sum[i] += sum[i ^ (1 << j)];
}
}
while (q--) {
string t; cin >> t;
reverse(t.begin(), t.end());
int x = 0, y = 0, z = 0;
for (int i = 0; i < n; i++) {
if (t[i] == '1') x += 1 << i;
if (t[i] == '0') y += 1 << i;
if (t[i] == '?') z += 1 << i;
}
int xx = __builtin_popcount(x);
if (xx <= 6) {
int ans = 0;
for (int smask = x; ; smask = (smask - 1) & x) {
if ((xx - __builtin_popcount(smask)) & 1) ans -= sum[smask | z];
else ans += sum[smask | z];
if (smask == 0) break;
}
cout << ans << en;
continue;
}
int yy = __builtin_popcount(x);
if (yy <= 6) {
for (int i = 0; i < n; i++) {
if (t[i] == '0') t[i] = '1';
else if (t[i] == '1') t[i] = '0';
}
swap(x, y); swap(xx, yy);
int ans = 0;
for (int smask = x; ; smask = (smask - 1) & x) {
if ((xx - __builtin_popcount(smask)) & 1) ans -= sum[smask | z];
else ans += sum[smask | z];
if (smask == 0) break;
}
cout << sum[m - 1] - ans << en;
continue;
}
int ans = 0;
for (int smask = z; ; smask = (smask - 1) & x) {
ans += sum[smask | x];
if (smask == 0) break;
}
cout << ans << en;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
272 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
272 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
272 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
272 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
272 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |