#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 = 1;
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;
int bits_cnt = 0;
for (int i = 0; i < k; ++i) {
if (t[i] == '?') {
mask |= 1 << i;
++bits_cnt;
} else if (t[i] == '1') {
shift |= 1 << i;
}
}
if (bits_cnt < MAGIC) {
int res = s[shift] - '0';
for (int submask = mask; submask > 0; submask = (submask - 1) & mask) {
res += s[submask + shift] - '0';
}
ans[j] = res;
} else {
shifts[shift].emplace_back(mask, j);
}
}
int n = 1 << k;
for (auto [shift, values] : shifts) {
memset(dp, 0, sizeof(dp));
for (int mask = 0; (mask | shift) < n; ++mask) {
if ((mask & shift) == 0) {
dp[mask] = s[mask | shift] - '0';
}
}
for (int bit = 0; bit < k; ++bit) {
if (shift & (1 << bit)) continue;
for (int mask = 0; mask < n; ++mask) {
if ((mask & shift) != 0) continue;
if (mask & (1 << bit)) {
dp[mask] += dp[mask ^ (1 << bit)];
}
}
}
for (const auto& [mask, idx] : values) {
ans[idx] = dp[mask];
}
}
for (int i = 0; i < q; ++i) {
cout << ans[i] << '\n';
}
return 0;
}
/*
4
1
3141592653589793
?01?
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
4436 KB |
Output is correct |
2 |
Correct |
2 ms |
4436 KB |
Output is correct |
3 |
Correct |
34 ms |
4436 KB |
Output is correct |
4 |
Correct |
22 ms |
4436 KB |
Output is correct |
5 |
Correct |
25 ms |
4468 KB |
Output is correct |
6 |
Correct |
53 ms |
4436 KB |
Output is correct |
7 |
Correct |
65 ms |
4436 KB |
Output is correct |
8 |
Correct |
2 ms |
4436 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
69 ms |
4504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
4436 KB |
Output is correct |
2 |
Correct |
2 ms |
4436 KB |
Output is correct |
3 |
Correct |
34 ms |
4436 KB |
Output is correct |
4 |
Correct |
22 ms |
4436 KB |
Output is correct |
5 |
Correct |
25 ms |
4468 KB |
Output is correct |
6 |
Correct |
53 ms |
4436 KB |
Output is correct |
7 |
Correct |
65 ms |
4436 KB |
Output is correct |
8 |
Correct |
2 ms |
4436 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
69 ms |
4504 KB |
Output is correct |
11 |
Correct |
262 ms |
23720 KB |
Output is correct |
12 |
Correct |
220 ms |
26556 KB |
Output is correct |
13 |
Correct |
306 ms |
23800 KB |
Output is correct |
14 |
Correct |
244 ms |
26100 KB |
Output is correct |
15 |
Correct |
290 ms |
26884 KB |
Output is correct |
16 |
Correct |
353 ms |
25440 KB |
Output is correct |
17 |
Correct |
398 ms |
25924 KB |
Output is correct |
18 |
Correct |
168 ms |
34764 KB |
Output is correct |
19 |
Correct |
157 ms |
16948 KB |
Output is correct |
20 |
Correct |
380 ms |
32520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
4436 KB |
Output is correct |
2 |
Correct |
2 ms |
4436 KB |
Output is correct |
3 |
Correct |
34 ms |
4436 KB |
Output is correct |
4 |
Correct |
22 ms |
4436 KB |
Output is correct |
5 |
Correct |
25 ms |
4468 KB |
Output is correct |
6 |
Correct |
53 ms |
4436 KB |
Output is correct |
7 |
Correct |
65 ms |
4436 KB |
Output is correct |
8 |
Correct |
2 ms |
4436 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
69 ms |
4504 KB |
Output is correct |
11 |
Correct |
262 ms |
23720 KB |
Output is correct |
12 |
Correct |
220 ms |
26556 KB |
Output is correct |
13 |
Correct |
306 ms |
23800 KB |
Output is correct |
14 |
Correct |
244 ms |
26100 KB |
Output is correct |
15 |
Correct |
290 ms |
26884 KB |
Output is correct |
16 |
Correct |
353 ms |
25440 KB |
Output is correct |
17 |
Correct |
398 ms |
25924 KB |
Output is correct |
18 |
Correct |
168 ms |
34764 KB |
Output is correct |
19 |
Correct |
157 ms |
16948 KB |
Output is correct |
20 |
Correct |
380 ms |
32520 KB |
Output is correct |
21 |
Correct |
573 ms |
41392 KB |
Output is correct |
22 |
Correct |
225 ms |
37784 KB |
Output is correct |
23 |
Correct |
968 ms |
36804 KB |
Output is correct |
24 |
Correct |
417 ms |
37320 KB |
Output is correct |
25 |
Correct |
659 ms |
38328 KB |
Output is correct |
26 |
Correct |
1402 ms |
38552 KB |
Output is correct |
27 |
Correct |
1501 ms |
39028 KB |
Output is correct |
28 |
Correct |
173 ms |
37728 KB |
Output is correct |
29 |
Correct |
156 ms |
19908 KB |
Output is correct |
30 |
Correct |
1495 ms |
39728 KB |
Output is correct |
31 |
Correct |
1248 ms |
37968 KB |
Output is correct |
32 |
Correct |
399 ms |
38096 KB |
Output is correct |
33 |
Correct |
435 ms |
36416 KB |
Output is correct |
34 |
Correct |
1449 ms |
37960 KB |
Output is correct |
35 |
Correct |
1482 ms |
39012 KB |
Output is correct |
36 |
Correct |
159 ms |
19852 KB |
Output is correct |
37 |
Correct |
216 ms |
37772 KB |
Output is correct |
38 |
Correct |
167 ms |
19916 KB |
Output is correct |
39 |
Correct |
558 ms |
40500 KB |
Output is correct |
40 |
Correct |
481 ms |
36496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
4436 KB |
Output is correct |
2 |
Correct |
2 ms |
4436 KB |
Output is correct |
3 |
Correct |
34 ms |
4436 KB |
Output is correct |
4 |
Correct |
22 ms |
4436 KB |
Output is correct |
5 |
Correct |
25 ms |
4468 KB |
Output is correct |
6 |
Correct |
53 ms |
4436 KB |
Output is correct |
7 |
Correct |
65 ms |
4436 KB |
Output is correct |
8 |
Correct |
2 ms |
4436 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
69 ms |
4504 KB |
Output is correct |
11 |
Execution timed out |
2012 ms |
11616 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
4436 KB |
Output is correct |
2 |
Correct |
2 ms |
4436 KB |
Output is correct |
3 |
Correct |
34 ms |
4436 KB |
Output is correct |
4 |
Correct |
22 ms |
4436 KB |
Output is correct |
5 |
Correct |
25 ms |
4468 KB |
Output is correct |
6 |
Correct |
53 ms |
4436 KB |
Output is correct |
7 |
Correct |
65 ms |
4436 KB |
Output is correct |
8 |
Correct |
2 ms |
4436 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
69 ms |
4504 KB |
Output is correct |
11 |
Correct |
262 ms |
23720 KB |
Output is correct |
12 |
Correct |
220 ms |
26556 KB |
Output is correct |
13 |
Correct |
306 ms |
23800 KB |
Output is correct |
14 |
Correct |
244 ms |
26100 KB |
Output is correct |
15 |
Correct |
290 ms |
26884 KB |
Output is correct |
16 |
Correct |
353 ms |
25440 KB |
Output is correct |
17 |
Correct |
398 ms |
25924 KB |
Output is correct |
18 |
Correct |
168 ms |
34764 KB |
Output is correct |
19 |
Correct |
157 ms |
16948 KB |
Output is correct |
20 |
Correct |
380 ms |
32520 KB |
Output is correct |
21 |
Correct |
573 ms |
41392 KB |
Output is correct |
22 |
Correct |
225 ms |
37784 KB |
Output is correct |
23 |
Correct |
968 ms |
36804 KB |
Output is correct |
24 |
Correct |
417 ms |
37320 KB |
Output is correct |
25 |
Correct |
659 ms |
38328 KB |
Output is correct |
26 |
Correct |
1402 ms |
38552 KB |
Output is correct |
27 |
Correct |
1501 ms |
39028 KB |
Output is correct |
28 |
Correct |
173 ms |
37728 KB |
Output is correct |
29 |
Correct |
156 ms |
19908 KB |
Output is correct |
30 |
Correct |
1495 ms |
39728 KB |
Output is correct |
31 |
Correct |
1248 ms |
37968 KB |
Output is correct |
32 |
Correct |
399 ms |
38096 KB |
Output is correct |
33 |
Correct |
435 ms |
36416 KB |
Output is correct |
34 |
Correct |
1449 ms |
37960 KB |
Output is correct |
35 |
Correct |
1482 ms |
39012 KB |
Output is correct |
36 |
Correct |
159 ms |
19852 KB |
Output is correct |
37 |
Correct |
216 ms |
37772 KB |
Output is correct |
38 |
Correct |
167 ms |
19916 KB |
Output is correct |
39 |
Correct |
558 ms |
40500 KB |
Output is correct |
40 |
Correct |
481 ms |
36496 KB |
Output is correct |
41 |
Execution timed out |
2012 ms |
11616 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |