// #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--) {
for (int i = 0; i < n; ++i) {
char c;
cin >> c;
t += c;
}
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 |
20 ms |
460 KB |
Output is correct |
2 |
Correct |
20 ms |
532 KB |
Output is correct |
3 |
Correct |
30 ms |
568 KB |
Output is correct |
4 |
Correct |
30 ms |
552 KB |
Output is correct |
5 |
Correct |
17 ms |
468 KB |
Output is correct |
6 |
Correct |
27 ms |
536 KB |
Output is correct |
7 |
Correct |
31 ms |
524 KB |
Output is correct |
8 |
Correct |
8 ms |
512 KB |
Output is correct |
9 |
Correct |
21 ms |
596 KB |
Output is correct |
10 |
Correct |
26 ms |
536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
460 KB |
Output is correct |
2 |
Correct |
20 ms |
532 KB |
Output is correct |
3 |
Correct |
30 ms |
568 KB |
Output is correct |
4 |
Correct |
30 ms |
552 KB |
Output is correct |
5 |
Correct |
17 ms |
468 KB |
Output is correct |
6 |
Correct |
27 ms |
536 KB |
Output is correct |
7 |
Correct |
31 ms |
524 KB |
Output is correct |
8 |
Correct |
8 ms |
512 KB |
Output is correct |
9 |
Correct |
21 ms |
596 KB |
Output is correct |
10 |
Correct |
26 ms |
536 KB |
Output is correct |
11 |
Execution timed out |
2063 ms |
728 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
460 KB |
Output is correct |
2 |
Correct |
20 ms |
532 KB |
Output is correct |
3 |
Correct |
30 ms |
568 KB |
Output is correct |
4 |
Correct |
30 ms |
552 KB |
Output is correct |
5 |
Correct |
17 ms |
468 KB |
Output is correct |
6 |
Correct |
27 ms |
536 KB |
Output is correct |
7 |
Correct |
31 ms |
524 KB |
Output is correct |
8 |
Correct |
8 ms |
512 KB |
Output is correct |
9 |
Correct |
21 ms |
596 KB |
Output is correct |
10 |
Correct |
26 ms |
536 KB |
Output is correct |
11 |
Execution timed out |
2063 ms |
728 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
460 KB |
Output is correct |
2 |
Correct |
20 ms |
532 KB |
Output is correct |
3 |
Correct |
30 ms |
568 KB |
Output is correct |
4 |
Correct |
30 ms |
552 KB |
Output is correct |
5 |
Correct |
17 ms |
468 KB |
Output is correct |
6 |
Correct |
27 ms |
536 KB |
Output is correct |
7 |
Correct |
31 ms |
524 KB |
Output is correct |
8 |
Correct |
8 ms |
512 KB |
Output is correct |
9 |
Correct |
21 ms |
596 KB |
Output is correct |
10 |
Correct |
26 ms |
536 KB |
Output is correct |
11 |
Runtime error |
31 ms |
65536 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
460 KB |
Output is correct |
2 |
Correct |
20 ms |
532 KB |
Output is correct |
3 |
Correct |
30 ms |
568 KB |
Output is correct |
4 |
Correct |
30 ms |
552 KB |
Output is correct |
5 |
Correct |
17 ms |
468 KB |
Output is correct |
6 |
Correct |
27 ms |
536 KB |
Output is correct |
7 |
Correct |
31 ms |
524 KB |
Output is correct |
8 |
Correct |
8 ms |
512 KB |
Output is correct |
9 |
Correct |
21 ms |
596 KB |
Output is correct |
10 |
Correct |
26 ms |
536 KB |
Output is correct |
11 |
Execution timed out |
2063 ms |
728 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |