// #cheat_when_I_was_young
// #cheatkhitacontre #khionhatoicheat
// #thaycuckythatvong
#include "bits/stdc++.h"
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
const int N = 1<<20, M = 21;
int n, q, dp[N][M], sup[N][M];
string s, t;
int backtrack1(int idx, int num) {
if (idx == n) return s[num];
if (t[idx] == '0') return backtrack1(idx+1, num);
if (t[idx] == '1') return backtrack1(idx+1, num | (1<<idx));
return backtrack1(idx+1, num) + backtrack1(idx+1, num | (1<<idx));
}
int backtrack2(int idx, int num, int change) {
if (idx == n) {
if (change % 2) return -dp[num][0];
return dp[num][0];
}
if (t[idx] == '0') return backtrack2(idx+1, num, change);
if (t[idx] == '?') return backtrack2(idx+1, num | (1<<idx), change);
return backtrack2(idx+1, num, change+1) + backtrack2(idx+1, num | (1<<idx), change);
}
int backtrack3(int idx, int num, int change) {
if (idx == n) {
if (change % 2) return -sup[num][0];
return sup[num][0];
}
if (t[idx] == '1') return backtrack3(idx+1, num | (1<<idx), change);
if (t[idx] == '?') return backtrack3(idx+1, num, change);
return backtrack3(idx+1, num, change) + backtrack3(idx+1, num | (1<<idx), change+1);
}
signed main() {
IOS;
cin >> n >> q >> s;
for (int mask = 0; mask < 1<<n; ++mask) {
s[mask] -= '0';
dp[mask][n] = sup[mask][n] = s[mask];
}
for (int mask = 0; mask < 1<<n; ++mask) for (int i = n-1; i >= 0; --i) {
dp[mask][i] = dp[mask][i+1];
if (mask & (1<<i)) dp[mask][i] += dp[mask^(1<<i)][i+1];
}
for (int mask = (1<<n)-1; mask >= 0; --mask) for (int i = n-1; i >= 0; --i) {
sup[mask][i] = sup[mask][i+1];
if (!(mask & (1<<i))) sup[mask][i] += sup[mask^(1<<i)][i+1];
}
while (q--) {
cin >> t;
reverse(t.begin(), t.end());
int cnt[] = {0, 0, 0};
for (char &c: t) {
if (c == '0') ++cnt[0];
else if (c == '1') ++cnt[1];
else ++cnt[2];
}
int mi = *min_element(cnt, cnt+3);
if (cnt[2] == mi) cout << backtrack1(0, 0) << "\n";
else if (cnt[1] == mi) cout << backtrack2(0, 0, 0) << "\n";
else cout << backtrack3(0, 0, 0) << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
188 ms |
15164 KB |
Output is correct |
12 |
Correct |
236 ms |
14924 KB |
Output is correct |
13 |
Correct |
300 ms |
14228 KB |
Output is correct |
14 |
Correct |
391 ms |
14180 KB |
Output is correct |
15 |
Correct |
295 ms |
15188 KB |
Output is correct |
16 |
Correct |
371 ms |
14424 KB |
Output is correct |
17 |
Correct |
351 ms |
14300 KB |
Output is correct |
18 |
Correct |
151 ms |
16216 KB |
Output is correct |
19 |
Correct |
178 ms |
13200 KB |
Output is correct |
20 |
Correct |
240 ms |
14856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
188 ms |
15164 KB |
Output is correct |
12 |
Correct |
236 ms |
14924 KB |
Output is correct |
13 |
Correct |
300 ms |
14228 KB |
Output is correct |
14 |
Correct |
391 ms |
14180 KB |
Output is correct |
15 |
Correct |
295 ms |
15188 KB |
Output is correct |
16 |
Correct |
371 ms |
14424 KB |
Output is correct |
17 |
Correct |
351 ms |
14300 KB |
Output is correct |
18 |
Correct |
151 ms |
16216 KB |
Output is correct |
19 |
Correct |
178 ms |
13200 KB |
Output is correct |
20 |
Correct |
240 ms |
14856 KB |
Output is correct |
21 |
Correct |
252 ms |
19356 KB |
Output is correct |
22 |
Correct |
270 ms |
19572 KB |
Output is correct |
23 |
Correct |
441 ms |
18524 KB |
Output is correct |
24 |
Correct |
598 ms |
18396 KB |
Output is correct |
25 |
Correct |
304 ms |
20428 KB |
Output is correct |
26 |
Correct |
535 ms |
18824 KB |
Output is correct |
27 |
Correct |
466 ms |
18836 KB |
Output is correct |
28 |
Correct |
164 ms |
21324 KB |
Output is correct |
29 |
Correct |
248 ms |
17380 KB |
Output is correct |
30 |
Correct |
269 ms |
19540 KB |
Output is correct |
31 |
Correct |
416 ms |
19468 KB |
Output is correct |
32 |
Correct |
629 ms |
19340 KB |
Output is correct |
33 |
Correct |
313 ms |
18256 KB |
Output is correct |
34 |
Correct |
542 ms |
18436 KB |
Output is correct |
35 |
Correct |
505 ms |
18868 KB |
Output is correct |
36 |
Correct |
167 ms |
17380 KB |
Output is correct |
37 |
Correct |
253 ms |
19348 KB |
Output is correct |
38 |
Correct |
265 ms |
17392 KB |
Output is correct |
39 |
Correct |
431 ms |
18604 KB |
Output is correct |
40 |
Correct |
582 ms |
18336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Runtime error |
32 ms |
65536 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
188 ms |
15164 KB |
Output is correct |
12 |
Correct |
236 ms |
14924 KB |
Output is correct |
13 |
Correct |
300 ms |
14228 KB |
Output is correct |
14 |
Correct |
391 ms |
14180 KB |
Output is correct |
15 |
Correct |
295 ms |
15188 KB |
Output is correct |
16 |
Correct |
371 ms |
14424 KB |
Output is correct |
17 |
Correct |
351 ms |
14300 KB |
Output is correct |
18 |
Correct |
151 ms |
16216 KB |
Output is correct |
19 |
Correct |
178 ms |
13200 KB |
Output is correct |
20 |
Correct |
240 ms |
14856 KB |
Output is correct |
21 |
Correct |
252 ms |
19356 KB |
Output is correct |
22 |
Correct |
270 ms |
19572 KB |
Output is correct |
23 |
Correct |
441 ms |
18524 KB |
Output is correct |
24 |
Correct |
598 ms |
18396 KB |
Output is correct |
25 |
Correct |
304 ms |
20428 KB |
Output is correct |
26 |
Correct |
535 ms |
18824 KB |
Output is correct |
27 |
Correct |
466 ms |
18836 KB |
Output is correct |
28 |
Correct |
164 ms |
21324 KB |
Output is correct |
29 |
Correct |
248 ms |
17380 KB |
Output is correct |
30 |
Correct |
269 ms |
19540 KB |
Output is correct |
31 |
Correct |
416 ms |
19468 KB |
Output is correct |
32 |
Correct |
629 ms |
19340 KB |
Output is correct |
33 |
Correct |
313 ms |
18256 KB |
Output is correct |
34 |
Correct |
542 ms |
18436 KB |
Output is correct |
35 |
Correct |
505 ms |
18868 KB |
Output is correct |
36 |
Correct |
167 ms |
17380 KB |
Output is correct |
37 |
Correct |
253 ms |
19348 KB |
Output is correct |
38 |
Correct |
265 ms |
17392 KB |
Output is correct |
39 |
Correct |
431 ms |
18604 KB |
Output is correct |
40 |
Correct |
582 ms |
18336 KB |
Output is correct |
41 |
Runtime error |
32 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Halted |
0 ms |
0 KB |
- |