#include <bits/stdc++.h>
using namespace std;
#define cerr cerr << "DEBUG "
constexpr int N = (1 << 20) + 10;
int a[N], f[N], g[N], h[N];
int n, m;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < (1 << n); ++i) {
char c;
cin >> c;
a[i] = c - '0';
}
int comp = (1 << n) - 1;
for (int i = 0; i < (1 << n); ++i) {
f[i] = g[i ^ comp] = a[i];
}
for (int i = 0; i < n; ++i) {
for (int mask = 0; mask < (1 << n); ++mask) {
if ((mask >> i) & 1) {
f[mask] += f[mask ^ (1 << i)];
g[mask] += g[mask ^ (1 << i)];
}
}
}
for (int i = 0; i < (1 << n); ++i) {
h[i] = g[i ^ comp];
}
while (m--) {
string s;
cin >> s;
int cnt0 = 0, cnt1 = 0, num = 0, numm = 0;
reverse(s.begin(), s.end());
for (int i = 0; i < n; ++i) {
cnt0 += s[i] == '0';
cnt1 += s[i] == '1';
if (s[i] == '1') {
num += (1 << i);
numm += (1 << i);
}
if (s[i] == '?') {
numm += (1 << i);
}
}
int sum = 0;
if (cnt1 <= 6) {
for (int mask = 0; mask < (1 << cnt1); ++mask) {
int num2 = numm;
for (int c = 0, i = 0; i < n; ++i) {
if (s[i] == '1') {
num2 ^= ((mask >> c) & 1) << i;
++c;
}
}
sum += (__builtin_parity(mask) ? -1 : +1) * f[num2];
}
} else if (n - cnt0 - cnt1 <= 6) {
int cnt = n - cnt0 - cnt1;
for (int mask = 0; mask < (1 << cnt); ++mask) {
int num2 = num;
for (int c = 0, i = 0; i < n; ++i) {
if (s[i] == '?') {
num2 ^= ((mask >> c) & 1) << i;
++c;
}
}
sum += a[num2];
}
} else {
for (int mask = 0; mask < (1 << cnt0); ++mask) {
int num2 = num;
for (int c = 0, i = 0; i < n; ++i) {
if (s[i] == '0') {
num2 ^= ((mask >> c) & 1) << i;
++c;
}
}
sum += (__builtin_parity(mask) ? -1 : +1) * h[num2];
}
}
cout << sum << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
637 ms |
4320 KB |
Output is correct |
12 |
Correct |
175 ms |
3928 KB |
Output is correct |
13 |
Correct |
334 ms |
3336 KB |
Output is correct |
14 |
Correct |
312 ms |
3352 KB |
Output is correct |
15 |
Correct |
273 ms |
4364 KB |
Output is correct |
16 |
Correct |
474 ms |
3528 KB |
Output is correct |
17 |
Correct |
459 ms |
3516 KB |
Output is correct |
18 |
Correct |
205 ms |
5292 KB |
Output is correct |
19 |
Correct |
650 ms |
2300 KB |
Output is correct |
20 |
Correct |
619 ms |
4032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
637 ms |
4320 KB |
Output is correct |
12 |
Correct |
175 ms |
3928 KB |
Output is correct |
13 |
Correct |
334 ms |
3336 KB |
Output is correct |
14 |
Correct |
312 ms |
3352 KB |
Output is correct |
15 |
Correct |
273 ms |
4364 KB |
Output is correct |
16 |
Correct |
474 ms |
3528 KB |
Output is correct |
17 |
Correct |
459 ms |
3516 KB |
Output is correct |
18 |
Correct |
205 ms |
5292 KB |
Output is correct |
19 |
Correct |
650 ms |
2300 KB |
Output is correct |
20 |
Correct |
619 ms |
4032 KB |
Output is correct |
21 |
Correct |
1306 ms |
4484 KB |
Output is correct |
22 |
Correct |
192 ms |
4676 KB |
Output is correct |
23 |
Correct |
563 ms |
3560 KB |
Output is correct |
24 |
Correct |
541 ms |
3472 KB |
Output is correct |
25 |
Correct |
379 ms |
5452 KB |
Output is correct |
26 |
Correct |
710 ms |
3960 KB |
Output is correct |
27 |
Correct |
677 ms |
4064 KB |
Output is correct |
28 |
Correct |
195 ms |
6476 KB |
Output is correct |
29 |
Correct |
222 ms |
2428 KB |
Output is correct |
30 |
Correct |
969 ms |
4720 KB |
Output is correct |
31 |
Correct |
566 ms |
4400 KB |
Output is correct |
32 |
Correct |
558 ms |
4444 KB |
Output is correct |
33 |
Correct |
395 ms |
3404 KB |
Output is correct |
34 |
Correct |
759 ms |
3508 KB |
Output is correct |
35 |
Correct |
682 ms |
3896 KB |
Output is correct |
36 |
Correct |
175 ms |
2380 KB |
Output is correct |
37 |
Correct |
187 ms |
4428 KB |
Output is correct |
38 |
Correct |
604 ms |
2432 KB |
Output is correct |
39 |
Correct |
1303 ms |
3816 KB |
Output is correct |
40 |
Correct |
806 ms |
3464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
59 ms |
16960 KB |
Output is correct |
12 |
Correct |
60 ms |
16984 KB |
Output is correct |
13 |
Correct |
101 ms |
16844 KB |
Output is correct |
14 |
Correct |
153 ms |
16848 KB |
Output is correct |
15 |
Correct |
82 ms |
16968 KB |
Output is correct |
16 |
Correct |
156 ms |
16844 KB |
Output is correct |
17 |
Correct |
137 ms |
16844 KB |
Output is correct |
18 |
Correct |
57 ms |
17152 KB |
Output is correct |
19 |
Correct |
59 ms |
16732 KB |
Output is correct |
20 |
Correct |
70 ms |
16884 KB |
Output is correct |
21 |
Correct |
101 ms |
16948 KB |
Output is correct |
22 |
Correct |
193 ms |
16852 KB |
Output is correct |
23 |
Correct |
82 ms |
16796 KB |
Output is correct |
24 |
Correct |
147 ms |
16840 KB |
Output is correct |
25 |
Correct |
136 ms |
16892 KB |
Output is correct |
26 |
Correct |
54 ms |
16720 KB |
Output is correct |
27 |
Correct |
56 ms |
16968 KB |
Output is correct |
28 |
Correct |
63 ms |
16720 KB |
Output is correct |
29 |
Correct |
101 ms |
16856 KB |
Output is correct |
30 |
Correct |
131 ms |
16856 KB |
Output is correct |
31 |
Correct |
83 ms |
16844 KB |
Output is correct |
32 |
Correct |
143 ms |
16848 KB |
Output is correct |
33 |
Correct |
144 ms |
16944 KB |
Output is correct |
34 |
Correct |
58 ms |
16768 KB |
Output is correct |
35 |
Correct |
111 ms |
16864 KB |
Output is correct |
36 |
Correct |
116 ms |
16888 KB |
Output is correct |
37 |
Correct |
112 ms |
16840 KB |
Output is correct |
38 |
Correct |
112 ms |
16848 KB |
Output is correct |
39 |
Correct |
111 ms |
16900 KB |
Output is correct |
40 |
Correct |
116 ms |
16912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
637 ms |
4320 KB |
Output is correct |
12 |
Correct |
175 ms |
3928 KB |
Output is correct |
13 |
Correct |
334 ms |
3336 KB |
Output is correct |
14 |
Correct |
312 ms |
3352 KB |
Output is correct |
15 |
Correct |
273 ms |
4364 KB |
Output is correct |
16 |
Correct |
474 ms |
3528 KB |
Output is correct |
17 |
Correct |
459 ms |
3516 KB |
Output is correct |
18 |
Correct |
205 ms |
5292 KB |
Output is correct |
19 |
Correct |
650 ms |
2300 KB |
Output is correct |
20 |
Correct |
619 ms |
4032 KB |
Output is correct |
21 |
Correct |
1306 ms |
4484 KB |
Output is correct |
22 |
Correct |
192 ms |
4676 KB |
Output is correct |
23 |
Correct |
563 ms |
3560 KB |
Output is correct |
24 |
Correct |
541 ms |
3472 KB |
Output is correct |
25 |
Correct |
379 ms |
5452 KB |
Output is correct |
26 |
Correct |
710 ms |
3960 KB |
Output is correct |
27 |
Correct |
677 ms |
4064 KB |
Output is correct |
28 |
Correct |
195 ms |
6476 KB |
Output is correct |
29 |
Correct |
222 ms |
2428 KB |
Output is correct |
30 |
Correct |
969 ms |
4720 KB |
Output is correct |
31 |
Correct |
566 ms |
4400 KB |
Output is correct |
32 |
Correct |
558 ms |
4444 KB |
Output is correct |
33 |
Correct |
395 ms |
3404 KB |
Output is correct |
34 |
Correct |
759 ms |
3508 KB |
Output is correct |
35 |
Correct |
682 ms |
3896 KB |
Output is correct |
36 |
Correct |
175 ms |
2380 KB |
Output is correct |
37 |
Correct |
187 ms |
4428 KB |
Output is correct |
38 |
Correct |
604 ms |
2432 KB |
Output is correct |
39 |
Correct |
1303 ms |
3816 KB |
Output is correct |
40 |
Correct |
806 ms |
3464 KB |
Output is correct |
41 |
Correct |
59 ms |
16960 KB |
Output is correct |
42 |
Correct |
60 ms |
16984 KB |
Output is correct |
43 |
Correct |
101 ms |
16844 KB |
Output is correct |
44 |
Correct |
153 ms |
16848 KB |
Output is correct |
45 |
Correct |
82 ms |
16968 KB |
Output is correct |
46 |
Correct |
156 ms |
16844 KB |
Output is correct |
47 |
Correct |
137 ms |
16844 KB |
Output is correct |
48 |
Correct |
57 ms |
17152 KB |
Output is correct |
49 |
Correct |
59 ms |
16732 KB |
Output is correct |
50 |
Correct |
70 ms |
16884 KB |
Output is correct |
51 |
Correct |
101 ms |
16948 KB |
Output is correct |
52 |
Correct |
193 ms |
16852 KB |
Output is correct |
53 |
Correct |
82 ms |
16796 KB |
Output is correct |
54 |
Correct |
147 ms |
16840 KB |
Output is correct |
55 |
Correct |
136 ms |
16892 KB |
Output is correct |
56 |
Correct |
54 ms |
16720 KB |
Output is correct |
57 |
Correct |
56 ms |
16968 KB |
Output is correct |
58 |
Correct |
63 ms |
16720 KB |
Output is correct |
59 |
Correct |
101 ms |
16856 KB |
Output is correct |
60 |
Correct |
131 ms |
16856 KB |
Output is correct |
61 |
Correct |
83 ms |
16844 KB |
Output is correct |
62 |
Correct |
143 ms |
16848 KB |
Output is correct |
63 |
Correct |
144 ms |
16944 KB |
Output is correct |
64 |
Correct |
58 ms |
16768 KB |
Output is correct |
65 |
Correct |
111 ms |
16864 KB |
Output is correct |
66 |
Correct |
116 ms |
16888 KB |
Output is correct |
67 |
Correct |
112 ms |
16840 KB |
Output is correct |
68 |
Correct |
112 ms |
16848 KB |
Output is correct |
69 |
Correct |
111 ms |
16900 KB |
Output is correct |
70 |
Correct |
116 ms |
16912 KB |
Output is correct |
71 |
Correct |
374 ms |
21684 KB |
Output is correct |
72 |
Correct |
322 ms |
21972 KB |
Output is correct |
73 |
Correct |
1232 ms |
20296 KB |
Output is correct |
74 |
Execution timed out |
2016 ms |
20752 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |