#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int l, q;
cin >> l >> q;
int n = (1 << l);
vector<int> a(n);
for (int i = 0; i < n; ++i) {
char c;
cin >> c;
a[i] = c - '0';
}
vector<int> sub(n);
for (int i = 0; i < n; ++i) {
sub[i] = a[i];
}
for (int j = 0; j < l; ++j) {
for (int s = 0; s < n; ++s) {
if (s >> j & 1) {
sub[s] += sub[s ^ (1 << j)];
}
}
}
vector<int> sup(n);
for (int i = 0; i < n; ++i) {
sup[i] = a[i];
}
for (int j = 0; j < l; ++j) {
for (int s = 0; s < n; ++s) {
if (!(s >> j & 1)) {
sup[s] += sup[s | (1 << j)];
}
}
}
while (q--) {
string qs;
cin >> qs;
int msk = 0;
vector<int> zeros, ones, qm;
for (int i = 0; i < l; ++i) {
if (qs[i] == '0') {
zeros.push_back(l - i - 1);
} else if (qs[i] == '1') {
ones.push_back(l - i - 1);
msk |= 1 << (l - i - 1);
} else {
qm.push_back(l - i - 1);
}
}
int ans = 0;
if (qm.size() <= 6) {
int m = (int) qm.size();
for (int i = 0; i < (1 << m); ++i) {
int msk2 = 0;
for (int j = 0; j < m; ++j) {
if (i >> j & 1) {
msk2 |= 1 << qm[j];
}
}
ans += a[msk | msk2];
}
} else if (ones.size() <= 6) {
int m = (int) ones.size();
for (int i = 0; i < (int) qs.size(); ++i) {
if (qs[i] == '?') {
msk |= 1 << (l - i - 1);
} else if (qs[i] == '1') {
msk ^= 1 << (l - i - 1);
}
}
for (int i = 0; i < (1 << m); ++i) {
int msk2 = 0;
bool even = true; //is off even or not
for (int j = 0; j < m; ++j) {
if (!(i >> j & 1)) {
even ^= 1;
} else {
msk2 |= 1 << ones[j];
}
}
ans += (even ? 1 : -1) * sub[msk | msk2];
}
} else {
int m = zeros.size();
for (int i = 0; i < (1 << m); ++i) {
int msk2 = 0;
bool even = true;
for (int j = 0; j < m; ++j) {
if (i >> j & 1) {
even ^= 1;
msk2 |= 1 << zeros[j];
}
}
ans += (even ? 1 : -1) * sup[msk | msk2];
}
}
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
567 ms |
15268 KB |
Output is correct |
12 |
Correct |
559 ms |
14992 KB |
Output is correct |
13 |
Correct |
459 ms |
14160 KB |
Output is correct |
14 |
Correct |
426 ms |
14112 KB |
Output is correct |
15 |
Correct |
914 ms |
15056 KB |
Output is correct |
16 |
Correct |
573 ms |
14416 KB |
Output is correct |
17 |
Correct |
527 ms |
14160 KB |
Output is correct |
18 |
Correct |
301 ms |
16208 KB |
Output is correct |
19 |
Correct |
370 ms |
13384 KB |
Output is correct |
20 |
Correct |
560 ms |
14676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
567 ms |
15268 KB |
Output is correct |
12 |
Correct |
559 ms |
14992 KB |
Output is correct |
13 |
Correct |
459 ms |
14160 KB |
Output is correct |
14 |
Correct |
426 ms |
14112 KB |
Output is correct |
15 |
Correct |
914 ms |
15056 KB |
Output is correct |
16 |
Correct |
573 ms |
14416 KB |
Output is correct |
17 |
Correct |
527 ms |
14160 KB |
Output is correct |
18 |
Correct |
301 ms |
16208 KB |
Output is correct |
19 |
Correct |
370 ms |
13384 KB |
Output is correct |
20 |
Correct |
560 ms |
14676 KB |
Output is correct |
21 |
Correct |
989 ms |
18080 KB |
Output is correct |
22 |
Correct |
647 ms |
18440 KB |
Output is correct |
23 |
Correct |
586 ms |
17232 KB |
Output is correct |
24 |
Correct |
544 ms |
17236 KB |
Output is correct |
25 |
Correct |
519 ms |
19264 KB |
Output is correct |
26 |
Correct |
677 ms |
17756 KB |
Output is correct |
27 |
Correct |
671 ms |
17500 KB |
Output is correct |
28 |
Correct |
282 ms |
20216 KB |
Output is correct |
29 |
Correct |
380 ms |
16212 KB |
Output is correct |
30 |
Correct |
793 ms |
18640 KB |
Output is correct |
31 |
Correct |
992 ms |
18148 KB |
Output is correct |
32 |
Correct |
699 ms |
18116 KB |
Output is correct |
33 |
Correct |
493 ms |
17700 KB |
Output is correct |
34 |
Correct |
556 ms |
17236 KB |
Output is correct |
35 |
Correct |
678 ms |
17600 KB |
Output is correct |
36 |
Correct |
257 ms |
16208 KB |
Output is correct |
37 |
Correct |
908 ms |
18220 KB |
Output is correct |
38 |
Correct |
422 ms |
16044 KB |
Output is correct |
39 |
Correct |
584 ms |
17540 KB |
Output is correct |
40 |
Correct |
543 ms |
17192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
74 ms |
15064 KB |
Output is correct |
12 |
Correct |
82 ms |
15048 KB |
Output is correct |
13 |
Correct |
91 ms |
14976 KB |
Output is correct |
14 |
Correct |
117 ms |
14932 KB |
Output is correct |
15 |
Correct |
85 ms |
14932 KB |
Output is correct |
16 |
Correct |
109 ms |
14928 KB |
Output is correct |
17 |
Correct |
102 ms |
14804 KB |
Output is correct |
18 |
Correct |
78 ms |
15056 KB |
Output is correct |
19 |
Correct |
81 ms |
14800 KB |
Output is correct |
20 |
Correct |
81 ms |
14932 KB |
Output is correct |
21 |
Correct |
95 ms |
14928 KB |
Output is correct |
22 |
Correct |
117 ms |
14812 KB |
Output is correct |
23 |
Correct |
83 ms |
14804 KB |
Output is correct |
24 |
Correct |
107 ms |
14928 KB |
Output is correct |
25 |
Correct |
104 ms |
14932 KB |
Output is correct |
26 |
Correct |
63 ms |
14808 KB |
Output is correct |
27 |
Correct |
83 ms |
14920 KB |
Output is correct |
28 |
Correct |
76 ms |
14676 KB |
Output is correct |
29 |
Correct |
96 ms |
15004 KB |
Output is correct |
30 |
Correct |
104 ms |
14924 KB |
Output is correct |
31 |
Correct |
82 ms |
14932 KB |
Output is correct |
32 |
Correct |
120 ms |
14804 KB |
Output is correct |
33 |
Correct |
136 ms |
14804 KB |
Output is correct |
34 |
Correct |
63 ms |
14676 KB |
Output is correct |
35 |
Correct |
93 ms |
14932 KB |
Output is correct |
36 |
Correct |
113 ms |
14812 KB |
Output is correct |
37 |
Correct |
101 ms |
14928 KB |
Output is correct |
38 |
Correct |
97 ms |
14928 KB |
Output is correct |
39 |
Correct |
95 ms |
14804 KB |
Output is correct |
40 |
Correct |
95 ms |
14860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
567 ms |
15268 KB |
Output is correct |
12 |
Correct |
559 ms |
14992 KB |
Output is correct |
13 |
Correct |
459 ms |
14160 KB |
Output is correct |
14 |
Correct |
426 ms |
14112 KB |
Output is correct |
15 |
Correct |
914 ms |
15056 KB |
Output is correct |
16 |
Correct |
573 ms |
14416 KB |
Output is correct |
17 |
Correct |
527 ms |
14160 KB |
Output is correct |
18 |
Correct |
301 ms |
16208 KB |
Output is correct |
19 |
Correct |
370 ms |
13384 KB |
Output is correct |
20 |
Correct |
560 ms |
14676 KB |
Output is correct |
21 |
Correct |
989 ms |
18080 KB |
Output is correct |
22 |
Correct |
647 ms |
18440 KB |
Output is correct |
23 |
Correct |
586 ms |
17232 KB |
Output is correct |
24 |
Correct |
544 ms |
17236 KB |
Output is correct |
25 |
Correct |
519 ms |
19264 KB |
Output is correct |
26 |
Correct |
677 ms |
17756 KB |
Output is correct |
27 |
Correct |
671 ms |
17500 KB |
Output is correct |
28 |
Correct |
282 ms |
20216 KB |
Output is correct |
29 |
Correct |
380 ms |
16212 KB |
Output is correct |
30 |
Correct |
793 ms |
18640 KB |
Output is correct |
31 |
Correct |
992 ms |
18148 KB |
Output is correct |
32 |
Correct |
699 ms |
18116 KB |
Output is correct |
33 |
Correct |
493 ms |
17700 KB |
Output is correct |
34 |
Correct |
556 ms |
17236 KB |
Output is correct |
35 |
Correct |
678 ms |
17600 KB |
Output is correct |
36 |
Correct |
257 ms |
16208 KB |
Output is correct |
37 |
Correct |
908 ms |
18220 KB |
Output is correct |
38 |
Correct |
422 ms |
16044 KB |
Output is correct |
39 |
Correct |
584 ms |
17540 KB |
Output is correct |
40 |
Correct |
543 ms |
17192 KB |
Output is correct |
41 |
Correct |
74 ms |
15064 KB |
Output is correct |
42 |
Correct |
82 ms |
15048 KB |
Output is correct |
43 |
Correct |
91 ms |
14976 KB |
Output is correct |
44 |
Correct |
117 ms |
14932 KB |
Output is correct |
45 |
Correct |
85 ms |
14932 KB |
Output is correct |
46 |
Correct |
109 ms |
14928 KB |
Output is correct |
47 |
Correct |
102 ms |
14804 KB |
Output is correct |
48 |
Correct |
78 ms |
15056 KB |
Output is correct |
49 |
Correct |
81 ms |
14800 KB |
Output is correct |
50 |
Correct |
81 ms |
14932 KB |
Output is correct |
51 |
Correct |
95 ms |
14928 KB |
Output is correct |
52 |
Correct |
117 ms |
14812 KB |
Output is correct |
53 |
Correct |
83 ms |
14804 KB |
Output is correct |
54 |
Correct |
107 ms |
14928 KB |
Output is correct |
55 |
Correct |
104 ms |
14932 KB |
Output is correct |
56 |
Correct |
63 ms |
14808 KB |
Output is correct |
57 |
Correct |
83 ms |
14920 KB |
Output is correct |
58 |
Correct |
76 ms |
14676 KB |
Output is correct |
59 |
Correct |
96 ms |
15004 KB |
Output is correct |
60 |
Correct |
104 ms |
14924 KB |
Output is correct |
61 |
Correct |
82 ms |
14932 KB |
Output is correct |
62 |
Correct |
120 ms |
14804 KB |
Output is correct |
63 |
Correct |
136 ms |
14804 KB |
Output is correct |
64 |
Correct |
63 ms |
14676 KB |
Output is correct |
65 |
Correct |
93 ms |
14932 KB |
Output is correct |
66 |
Correct |
113 ms |
14812 KB |
Output is correct |
67 |
Correct |
101 ms |
14928 KB |
Output is correct |
68 |
Correct |
97 ms |
14928 KB |
Output is correct |
69 |
Correct |
95 ms |
14804 KB |
Output is correct |
70 |
Correct |
95 ms |
14860 KB |
Output is correct |
71 |
Correct |
612 ms |
39168 KB |
Output is correct |
72 |
Correct |
776 ms |
39628 KB |
Output is correct |
73 |
Correct |
948 ms |
37968 KB |
Output is correct |
74 |
Correct |
1362 ms |
38224 KB |
Output is correct |
75 |
Correct |
824 ms |
40412 KB |
Output is correct |
76 |
Correct |
1317 ms |
38484 KB |
Output is correct |
77 |
Correct |
1161 ms |
38424 KB |
Output is correct |
78 |
Correct |
404 ms |
42196 KB |
Output is correct |
79 |
Correct |
569 ms |
36308 KB |
Output is correct |
80 |
Correct |
658 ms |
39344 KB |
Output is correct |
81 |
Correct |
1010 ms |
39100 KB |
Output is correct |
82 |
Correct |
1253 ms |
38688 KB |
Output is correct |
83 |
Correct |
725 ms |
37464 KB |
Output is correct |
84 |
Correct |
1276 ms |
38380 KB |
Output is correct |
85 |
Correct |
1198 ms |
38572 KB |
Output is correct |
86 |
Correct |
370 ms |
36176 KB |
Output is correct |
87 |
Correct |
686 ms |
39324 KB |
Output is correct |
88 |
Correct |
593 ms |
36464 KB |
Output is correct |
89 |
Correct |
950 ms |
37824 KB |
Output is correct |
90 |
Correct |
1256 ms |
38204 KB |
Output is correct |
91 |
Correct |
772 ms |
37516 KB |
Output is correct |
92 |
Correct |
1365 ms |
38668 KB |
Output is correct |
93 |
Correct |
1248 ms |
38488 KB |
Output is correct |
94 |
Correct |
365 ms |
36180 KB |
Output is correct |
95 |
Correct |
1056 ms |
38528 KB |
Output is correct |
96 |
Correct |
1018 ms |
38596 KB |
Output is correct |
97 |
Correct |
1044 ms |
38276 KB |
Output is correct |
98 |
Correct |
1062 ms |
38492 KB |
Output is correct |
99 |
Correct |
1065 ms |
38468 KB |
Output is correct |
100 |
Correct |
1042 ms |
38228 KB |
Output is correct |