// #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];
}
if (cnt[2] <= 6) cout << backtrack1(0, 0) << endl;
else if (cnt[1] <= 6) cout << backtrack2(0, 0, 0) << endl;
else cout << backtrack3(0, 0, 0) << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1343 ms |
4592 KB |
Output is correct |
12 |
Correct |
1337 ms |
4128 KB |
Output is correct |
13 |
Correct |
1315 ms |
3412 KB |
Output is correct |
14 |
Correct |
1307 ms |
3444 KB |
Output is correct |
15 |
Correct |
1665 ms |
4596 KB |
Output is correct |
16 |
Correct |
1379 ms |
3556 KB |
Output is correct |
17 |
Correct |
1388 ms |
3520 KB |
Output is correct |
18 |
Correct |
1072 ms |
5600 KB |
Output is correct |
19 |
Correct |
1112 ms |
2496 KB |
Output is correct |
20 |
Correct |
1367 ms |
4136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1343 ms |
4592 KB |
Output is correct |
12 |
Correct |
1337 ms |
4128 KB |
Output is correct |
13 |
Correct |
1315 ms |
3412 KB |
Output is correct |
14 |
Correct |
1307 ms |
3444 KB |
Output is correct |
15 |
Correct |
1665 ms |
4596 KB |
Output is correct |
16 |
Correct |
1379 ms |
3556 KB |
Output is correct |
17 |
Correct |
1388 ms |
3520 KB |
Output is correct |
18 |
Correct |
1072 ms |
5600 KB |
Output is correct |
19 |
Correct |
1112 ms |
2496 KB |
Output is correct |
20 |
Correct |
1367 ms |
4136 KB |
Output is correct |
21 |
Correct |
1778 ms |
5708 KB |
Output is correct |
22 |
Correct |
1369 ms |
5852 KB |
Output is correct |
23 |
Correct |
1485 ms |
4764 KB |
Output is correct |
24 |
Correct |
1532 ms |
4680 KB |
Output is correct |
25 |
Correct |
1320 ms |
6664 KB |
Output is correct |
26 |
Correct |
1576 ms |
5260 KB |
Output is correct |
27 |
Correct |
1558 ms |
5052 KB |
Output is correct |
28 |
Correct |
1102 ms |
7668 KB |
Output is correct |
29 |
Correct |
1179 ms |
3612 KB |
Output is correct |
30 |
Correct |
1603 ms |
5848 KB |
Output is correct |
31 |
Correct |
1925 ms |
5660 KB |
Output is correct |
32 |
Correct |
1701 ms |
5728 KB |
Output is correct |
33 |
Correct |
1318 ms |
4560 KB |
Output is correct |
34 |
Correct |
1537 ms |
4632 KB |
Output is correct |
35 |
Correct |
1555 ms |
5152 KB |
Output is correct |
36 |
Correct |
1099 ms |
3656 KB |
Output is correct |
37 |
Correct |
1682 ms |
5752 KB |
Output is correct |
38 |
Correct |
1210 ms |
3660 KB |
Output is correct |
39 |
Correct |
1466 ms |
4876 KB |
Output is correct |
40 |
Correct |
1510 ms |
4640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Runtime error |
38 ms |
65536 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1343 ms |
4592 KB |
Output is correct |
12 |
Correct |
1337 ms |
4128 KB |
Output is correct |
13 |
Correct |
1315 ms |
3412 KB |
Output is correct |
14 |
Correct |
1307 ms |
3444 KB |
Output is correct |
15 |
Correct |
1665 ms |
4596 KB |
Output is correct |
16 |
Correct |
1379 ms |
3556 KB |
Output is correct |
17 |
Correct |
1388 ms |
3520 KB |
Output is correct |
18 |
Correct |
1072 ms |
5600 KB |
Output is correct |
19 |
Correct |
1112 ms |
2496 KB |
Output is correct |
20 |
Correct |
1367 ms |
4136 KB |
Output is correct |
21 |
Correct |
1778 ms |
5708 KB |
Output is correct |
22 |
Correct |
1369 ms |
5852 KB |
Output is correct |
23 |
Correct |
1485 ms |
4764 KB |
Output is correct |
24 |
Correct |
1532 ms |
4680 KB |
Output is correct |
25 |
Correct |
1320 ms |
6664 KB |
Output is correct |
26 |
Correct |
1576 ms |
5260 KB |
Output is correct |
27 |
Correct |
1558 ms |
5052 KB |
Output is correct |
28 |
Correct |
1102 ms |
7668 KB |
Output is correct |
29 |
Correct |
1179 ms |
3612 KB |
Output is correct |
30 |
Correct |
1603 ms |
5848 KB |
Output is correct |
31 |
Correct |
1925 ms |
5660 KB |
Output is correct |
32 |
Correct |
1701 ms |
5728 KB |
Output is correct |
33 |
Correct |
1318 ms |
4560 KB |
Output is correct |
34 |
Correct |
1537 ms |
4632 KB |
Output is correct |
35 |
Correct |
1555 ms |
5152 KB |
Output is correct |
36 |
Correct |
1099 ms |
3656 KB |
Output is correct |
37 |
Correct |
1682 ms |
5752 KB |
Output is correct |
38 |
Correct |
1210 ms |
3660 KB |
Output is correct |
39 |
Correct |
1466 ms |
4876 KB |
Output is correct |
40 |
Correct |
1510 ms |
4640 KB |
Output is correct |
41 |
Runtime error |
38 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Halted |
0 ms |
0 KB |
- |