#include <bits/stdc++.h>
using namespace std;
int L, Q;
char ch;
int A[1 << 20];
int dp0[1 << 20];//1 jadi 1, semua 0 jadi ?
int dp1[1 << 20];//0 jadi 0, semua 1 jadi ?
int main() {
// ios_base::sync_with_stdio(0);
cin >> L >> Q;
for(int i = 0; i < (1 << L); i++) {
cin >> ch;
A[i] = ch - '0';
dp0[i] = dp1[i] = A[i];
}
for(int i = 0; i < L; i++) {
for(int mask = 0; mask < (1 << L); mask++) {
if(mask & (1 << i)) {
dp1[mask] += dp1[mask ^ (1 << i)];
}else dp0[mask] += dp0[mask ^ (1 << i)];
}
}
// cout << "-> " << dp0[0] << "\n";
// cout << "-> " << dp0[(1 << L) - 1] << "\n";
int mask1, mask0, maskq;
int cnt1, cnt0, cntq;
int ans, tmp, bit;
while(Q--) {
ans = mask1 = mask0 = maskq = cnt1 = cnt0 = cntq = 0;
for(int i = L - 1; i >= 0; i--) {
cin >> ch;
if(ch == '0') mask0 |= (1 << i), cnt0++;
if(ch == '1') mask1 |= (1 << i), cnt1++;
if(ch == '?') maskq |= (1 << i), cntq++;
}
if(cntq <= cnt0 && cntq < cnt1) {
tmp = maskq;
while(tmp) {
// cout << Q << ": " << A[mask1 | tmp] << " " << A[mask1] << "\n";
ans += A[mask1 | tmp];
tmp = maskq & (tmp - 1);
}
printf("%d\n", ans + A[mask1]);
}else if(cnt0 < cnt1) {
tmp = mask0;
while(tmp) {
ans += dp0[mask1 | tmp] * (__builtin_popcount(tmp) & 1 ? -1 : 1);
tmp = mask0 & (tmp - 1);
}
printf("%d\n", ans + dp0[mask1]);
}else {
tmp = mask1;
while(tmp) {
bit = ((((1 << L) - 1) ^ tmp) & (((1 << L) - 1) & mask1));
// cout << tmp << " " << bit << " " << dp1[maskq | bit] << "\n";
ans += dp1[maskq | bit] * (__builtin_popcount(bit) & 1 ? 1 : -1) * (__builtin_popcount(mask1) & 1 ? 1 : -1);;
tmp = mask1 & (tmp - 1);
}
printf("%d\n", ans + dp1[maskq + mask1]);
}
};
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Execution timed out |
2087 ms |
11412 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Execution timed out |
2087 ms |
11412 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
313 ms |
14972 KB |
Output is correct |
12 |
Correct |
299 ms |
14968 KB |
Output is correct |
13 |
Correct |
313 ms |
14968 KB |
Output is correct |
14 |
Correct |
335 ms |
14840 KB |
Output is correct |
15 |
Correct |
299 ms |
14968 KB |
Output is correct |
16 |
Correct |
335 ms |
14840 KB |
Output is correct |
17 |
Correct |
328 ms |
15008 KB |
Output is correct |
18 |
Correct |
288 ms |
15096 KB |
Output is correct |
19 |
Correct |
303 ms |
14876 KB |
Output is correct |
20 |
Correct |
305 ms |
15004 KB |
Output is correct |
21 |
Correct |
333 ms |
15012 KB |
Output is correct |
22 |
Correct |
343 ms |
14984 KB |
Output is correct |
23 |
Correct |
304 ms |
14844 KB |
Output is correct |
24 |
Correct |
330 ms |
14840 KB |
Output is correct |
25 |
Correct |
323 ms |
14840 KB |
Output is correct |
26 |
Correct |
319 ms |
14900 KB |
Output is correct |
27 |
Correct |
300 ms |
14992 KB |
Output is correct |
28 |
Correct |
305 ms |
14840 KB |
Output is correct |
29 |
Correct |
317 ms |
14840 KB |
Output is correct |
30 |
Correct |
314 ms |
14840 KB |
Output is correct |
31 |
Correct |
305 ms |
14968 KB |
Output is correct |
32 |
Correct |
337 ms |
14840 KB |
Output is correct |
33 |
Correct |
328 ms |
14840 KB |
Output is correct |
34 |
Correct |
296 ms |
14840 KB |
Output is correct |
35 |
Correct |
319 ms |
14840 KB |
Output is correct |
36 |
Correct |
316 ms |
14840 KB |
Output is correct |
37 |
Correct |
314 ms |
14852 KB |
Output is correct |
38 |
Correct |
316 ms |
14968 KB |
Output is correct |
39 |
Correct |
315 ms |
14968 KB |
Output is correct |
40 |
Correct |
321 ms |
14952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Execution timed out |
2087 ms |
11412 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |