#include <bits/stdc++.h>
using namespace std;
#define dbg(x) cerr << #x << ": " << x << endl;
constexpr int MAX_Q = 1e6 + 5;
constexpr int MAX_BIT = 20;
constexpr int MAX_N = 1 << 20;
constexpr int MAGIC = 3;
int ans[MAX_Q];
int dp[MAX_N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int k, q;
cin >> k >> q;
string s;
cin >> s;
map<int, vector<pair<int, int>>> shifts;
for (int j = 0; j < q; ++j) {
string t;
cin >> t;
reverse(t.begin(), t.end());
int mask = 0;
int shift = 0;
for (int i = 0; i < k; ++i) {
if (t[i] == '?') {
mask |= 1 << i;
} else if (t[i] == '1') {
shift |= 1 << i;
}
}
shifts[shift].emplace_back(mask, j);
}
for (const auto& [shift, values] : shifts) {
if (values.size() >= MAGIC) {
int total_mask = ((1 << k) - 1) & (~shift);
dp[0] = s[shift] - '0';
for (int mask = total_mask; mask > 0; mask = (mask - 1) & total_mask) {
dp[mask] = s[mask | shift] - '0';
}
for (int bit = 0; bit < k; ++bit) {
if (shift & (1 << bit)) continue;
for (int mask = total_mask; mask > 0; mask = (mask - 1) & total_mask) {
if (mask & (1 << bit)) {
dp[mask] += dp[mask ^ (1 << bit)];
}
}
}
for (const auto& [mask, idx] : values) {
ans[idx] = dp[mask];
}
for (int mask = total_mask; mask > 0; mask = (mask - 1) & total_mask) {
dp[mask] = 0;
}
} else {
for (const auto& [mask, idx] : values) {
ans[idx] = s[shift] - '0';
for (int submask = mask; submask > 0; submask = (submask - 1) & mask) {
ans[idx] += s[submask | shift] - '0';
}
}
}
}
for (int i = 0; i < q; ++i) {
cout << ans[i] << '\n';
}
return 0;
}
/*
4
1
3141592653589793
?01?
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
211 ms |
17244 KB |
Output is correct |
12 |
Correct |
198 ms |
15828 KB |
Output is correct |
13 |
Correct |
230 ms |
17400 KB |
Output is correct |
14 |
Correct |
220 ms |
19692 KB |
Output is correct |
15 |
Correct |
228 ms |
20196 KB |
Output is correct |
16 |
Correct |
247 ms |
19244 KB |
Output is correct |
17 |
Correct |
258 ms |
20084 KB |
Output is correct |
18 |
Correct |
153 ms |
17196 KB |
Output is correct |
19 |
Correct |
188 ms |
15208 KB |
Output is correct |
20 |
Correct |
243 ms |
17628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
211 ms |
17244 KB |
Output is correct |
12 |
Correct |
198 ms |
15828 KB |
Output is correct |
13 |
Correct |
230 ms |
17400 KB |
Output is correct |
14 |
Correct |
220 ms |
19692 KB |
Output is correct |
15 |
Correct |
228 ms |
20196 KB |
Output is correct |
16 |
Correct |
247 ms |
19244 KB |
Output is correct |
17 |
Correct |
258 ms |
20084 KB |
Output is correct |
18 |
Correct |
153 ms |
17196 KB |
Output is correct |
19 |
Correct |
188 ms |
15208 KB |
Output is correct |
20 |
Correct |
243 ms |
17628 KB |
Output is correct |
21 |
Correct |
270 ms |
23720 KB |
Output is correct |
22 |
Correct |
210 ms |
16340 KB |
Output is correct |
23 |
Correct |
311 ms |
19096 KB |
Output is correct |
24 |
Correct |
258 ms |
19540 KB |
Output is correct |
25 |
Correct |
294 ms |
20460 KB |
Output is correct |
26 |
Correct |
340 ms |
20808 KB |
Output is correct |
27 |
Correct |
342 ms |
21236 KB |
Output is correct |
28 |
Correct |
152 ms |
18172 KB |
Output is correct |
29 |
Correct |
236 ms |
21620 KB |
Output is correct |
30 |
Correct |
364 ms |
21880 KB |
Output is correct |
31 |
Correct |
323 ms |
20076 KB |
Output is correct |
32 |
Correct |
267 ms |
20436 KB |
Output is correct |
33 |
Correct |
244 ms |
18648 KB |
Output is correct |
34 |
Correct |
333 ms |
20128 KB |
Output is correct |
35 |
Correct |
350 ms |
21248 KB |
Output is correct |
36 |
Correct |
148 ms |
14116 KB |
Output is correct |
37 |
Correct |
197 ms |
16168 KB |
Output is correct |
38 |
Correct |
320 ms |
19816 KB |
Output is correct |
39 |
Correct |
271 ms |
22732 KB |
Output is correct |
40 |
Correct |
261 ms |
18724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
205 ms |
10204 KB |
Output is correct |
12 |
Correct |
41 ms |
6820 KB |
Output is correct |
13 |
Execution timed out |
2082 ms |
9176 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
211 ms |
17244 KB |
Output is correct |
12 |
Correct |
198 ms |
15828 KB |
Output is correct |
13 |
Correct |
230 ms |
17400 KB |
Output is correct |
14 |
Correct |
220 ms |
19692 KB |
Output is correct |
15 |
Correct |
228 ms |
20196 KB |
Output is correct |
16 |
Correct |
247 ms |
19244 KB |
Output is correct |
17 |
Correct |
258 ms |
20084 KB |
Output is correct |
18 |
Correct |
153 ms |
17196 KB |
Output is correct |
19 |
Correct |
188 ms |
15208 KB |
Output is correct |
20 |
Correct |
243 ms |
17628 KB |
Output is correct |
21 |
Correct |
270 ms |
23720 KB |
Output is correct |
22 |
Correct |
210 ms |
16340 KB |
Output is correct |
23 |
Correct |
311 ms |
19096 KB |
Output is correct |
24 |
Correct |
258 ms |
19540 KB |
Output is correct |
25 |
Correct |
294 ms |
20460 KB |
Output is correct |
26 |
Correct |
340 ms |
20808 KB |
Output is correct |
27 |
Correct |
342 ms |
21236 KB |
Output is correct |
28 |
Correct |
152 ms |
18172 KB |
Output is correct |
29 |
Correct |
236 ms |
21620 KB |
Output is correct |
30 |
Correct |
364 ms |
21880 KB |
Output is correct |
31 |
Correct |
323 ms |
20076 KB |
Output is correct |
32 |
Correct |
267 ms |
20436 KB |
Output is correct |
33 |
Correct |
244 ms |
18648 KB |
Output is correct |
34 |
Correct |
333 ms |
20128 KB |
Output is correct |
35 |
Correct |
350 ms |
21248 KB |
Output is correct |
36 |
Correct |
148 ms |
14116 KB |
Output is correct |
37 |
Correct |
197 ms |
16168 KB |
Output is correct |
38 |
Correct |
320 ms |
19816 KB |
Output is correct |
39 |
Correct |
271 ms |
22732 KB |
Output is correct |
40 |
Correct |
261 ms |
18724 KB |
Output is correct |
41 |
Correct |
205 ms |
10204 KB |
Output is correct |
42 |
Correct |
41 ms |
6820 KB |
Output is correct |
43 |
Execution timed out |
2082 ms |
9176 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |