#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 = 12;
int ans[MAX_Q];
int dp[MAX_N];
int max_mask[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);
max_mask[shift] = max(max_mask[shift], __lg(mask) + 1);
}
}
for (const auto& [shift, values] : shifts) {
int n = 1 << max_mask[shift];
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 | shift) < 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 mask = 0; (mask | shift) < n; ++mask) {
dp[mask] = 0;
}
}
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 |
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 |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 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 |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 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 |
208 ms |
8140 KB |
Output is correct |
12 |
Correct |
260 ms |
7844 KB |
Output is correct |
13 |
Correct |
175 ms |
7144 KB |
Output is correct |
14 |
Correct |
168 ms |
7232 KB |
Output is correct |
15 |
Correct |
247 ms |
8260 KB |
Output is correct |
16 |
Correct |
197 ms |
7396 KB |
Output is correct |
17 |
Correct |
200 ms |
7332 KB |
Output is correct |
18 |
Correct |
1207 ms |
9264 KB |
Output is correct |
19 |
Correct |
157 ms |
6208 KB |
Output is correct |
20 |
Correct |
252 ms |
7896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 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 |
208 ms |
8140 KB |
Output is correct |
12 |
Correct |
260 ms |
7844 KB |
Output is correct |
13 |
Correct |
175 ms |
7144 KB |
Output is correct |
14 |
Correct |
168 ms |
7232 KB |
Output is correct |
15 |
Correct |
247 ms |
8260 KB |
Output is correct |
16 |
Correct |
197 ms |
7396 KB |
Output is correct |
17 |
Correct |
200 ms |
7332 KB |
Output is correct |
18 |
Correct |
1207 ms |
9264 KB |
Output is correct |
19 |
Correct |
157 ms |
6208 KB |
Output is correct |
20 |
Correct |
252 ms |
7896 KB |
Output is correct |
21 |
Correct |
345 ms |
8212 KB |
Output is correct |
22 |
Correct |
403 ms |
8360 KB |
Output is correct |
23 |
Correct |
197 ms |
7408 KB |
Output is correct |
24 |
Correct |
190 ms |
7224 KB |
Output is correct |
25 |
Correct |
480 ms |
9168 KB |
Output is correct |
26 |
Correct |
243 ms |
7796 KB |
Output is correct |
27 |
Correct |
238 ms |
7680 KB |
Output is correct |
28 |
Correct |
176 ms |
18260 KB |
Output is correct |
29 |
Correct |
145 ms |
6152 KB |
Output is correct |
30 |
Correct |
454 ms |
8432 KB |
Output is correct |
31 |
Correct |
263 ms |
8268 KB |
Output is correct |
32 |
Correct |
248 ms |
8204 KB |
Output is correct |
33 |
Correct |
175 ms |
7128 KB |
Output is correct |
34 |
Correct |
196 ms |
7252 KB |
Output is correct |
35 |
Correct |
239 ms |
7636 KB |
Output is correct |
36 |
Correct |
142 ms |
6208 KB |
Output is correct |
37 |
Correct |
268 ms |
8252 KB |
Output is correct |
38 |
Correct |
145 ms |
6140 KB |
Output is correct |
39 |
Correct |
197 ms |
7412 KB |
Output is correct |
40 |
Correct |
221 ms |
7244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 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 |
75 ms |
2416 KB |
Output is correct |
12 |
Correct |
100 ms |
6012 KB |
Output is correct |
13 |
Correct |
19 ms |
2416 KB |
Output is correct |
14 |
Correct |
32 ms |
2416 KB |
Output is correct |
15 |
Execution timed out |
2047 ms |
10020 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 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 |
208 ms |
8140 KB |
Output is correct |
12 |
Correct |
260 ms |
7844 KB |
Output is correct |
13 |
Correct |
175 ms |
7144 KB |
Output is correct |
14 |
Correct |
168 ms |
7232 KB |
Output is correct |
15 |
Correct |
247 ms |
8260 KB |
Output is correct |
16 |
Correct |
197 ms |
7396 KB |
Output is correct |
17 |
Correct |
200 ms |
7332 KB |
Output is correct |
18 |
Correct |
1207 ms |
9264 KB |
Output is correct |
19 |
Correct |
157 ms |
6208 KB |
Output is correct |
20 |
Correct |
252 ms |
7896 KB |
Output is correct |
21 |
Correct |
345 ms |
8212 KB |
Output is correct |
22 |
Correct |
403 ms |
8360 KB |
Output is correct |
23 |
Correct |
197 ms |
7408 KB |
Output is correct |
24 |
Correct |
190 ms |
7224 KB |
Output is correct |
25 |
Correct |
480 ms |
9168 KB |
Output is correct |
26 |
Correct |
243 ms |
7796 KB |
Output is correct |
27 |
Correct |
238 ms |
7680 KB |
Output is correct |
28 |
Correct |
176 ms |
18260 KB |
Output is correct |
29 |
Correct |
145 ms |
6152 KB |
Output is correct |
30 |
Correct |
454 ms |
8432 KB |
Output is correct |
31 |
Correct |
263 ms |
8268 KB |
Output is correct |
32 |
Correct |
248 ms |
8204 KB |
Output is correct |
33 |
Correct |
175 ms |
7128 KB |
Output is correct |
34 |
Correct |
196 ms |
7252 KB |
Output is correct |
35 |
Correct |
239 ms |
7636 KB |
Output is correct |
36 |
Correct |
142 ms |
6208 KB |
Output is correct |
37 |
Correct |
268 ms |
8252 KB |
Output is correct |
38 |
Correct |
145 ms |
6140 KB |
Output is correct |
39 |
Correct |
197 ms |
7412 KB |
Output is correct |
40 |
Correct |
221 ms |
7244 KB |
Output is correct |
41 |
Correct |
75 ms |
2416 KB |
Output is correct |
42 |
Correct |
100 ms |
6012 KB |
Output is correct |
43 |
Correct |
19 ms |
2416 KB |
Output is correct |
44 |
Correct |
32 ms |
2416 KB |
Output is correct |
45 |
Execution timed out |
2047 ms |
10020 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |