#include <iostream>
#include <cassert>
#include <algorithm>
#include <string>
#include <bitset>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#ifndef LOCAL
#include <bits/stdc++.h>
#define cerr if(false)cerr
#endif
using namespace std;
using ll = long long;
const int N = 2e6 + 66;
int up[N];
int dw[N];
vector<int>pos;
string vals, qs;
int L;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int q;
cin >> L >> q;
cin >> vals;
for (int i = 0 ; i < (1 << L) ; ++ i) {
up[i] = dw[i] = vals[i] - '0';
}
for (int i = 0 ; i < L ; ++ i) {
for (int m = 0 ; m < (1 << L) ; ++ m) {
if (m >> i & 1) {
dw[m] += dw[m ^ (1 << i)];
}
}
}
for (int i = 0 ; i < L ; ++ i) {
for (int m = (1 << L) - 1 ; m >= 0 ; -- m) {
if (m >> i & 1) {
up[m ^ (1 << i)] += up[m];
}
}
}
for (int m = 0 ; m < (1 << L) ; ++ m) {
cerr << bitset<3>(m) << " " << dw[m] << endl;
}
while (q--) {
int ans = 0;
cin >> qs;
reverse(qs.begin(), qs.end());
int a = count(qs.begin(), qs.end(), '0');
int c = count(qs.begin(), qs.end(), '?');
if (c <= 6) {
pos.clear();
for (int i = 0 ; i < L ; ++ i) {
if (qs[i] == '?') {
pos.emplace_back(i);
}
}
int n = pos.size();
int base = 0;
for (int i = 0 ; i < L ; ++ i) {
if (qs[i] == '1') {
base |= 1 << i;
}
}
for (int m = 0 ; m < (1 << n) ; ++ m) {
int mask = base;
for (int i = 0 ; i < n ; ++ i) {
if (m >> i & 1) mask |= 1 << pos[i];
}
ans += vals[mask] - '0';
}
} else if (a <= 6) {
pos.clear();
for (int i = 0 ; i < L ; ++ i) {
if (qs[i] == '0') {
pos.emplace_back(i);
}
}
int n = pos.size();
int base = 0;
for (int i = 0 ; i < L ; ++ i) {
if (qs[i] == '1') {
base |= 1 << i;
}
}
for (int m = 0 ; m < (1 << n) ; ++ m) {
int mask = base;
for (int i = 0 ; i < n ; ++ i) {
if (m >> i & 1) mask |= 1 << pos[i];
}
int par = __builtin_popcount(m) & 1;
int cur = up[mask] * (par ? -1 : 1);
ans += cur;
}
} else {
pos.clear();
for (int i = 0 ; i < L ; ++ i) {
if (qs[i] == '1') {
pos.emplace_back(i);
}
}
int n = pos.size();
int base = 0;
for (int i = 0 ; i < L ; ++ i) {
if (qs[i] == '?') {
base |= 1 << i;
}
}
for (int m = 0 ; m < (1 << n) ; ++ m) {
int mask = base;
for (int i = 0 ; i < n ; ++ i) {
if (m >> i & 1) mask |= 1 << pos[i];
}
int par = __builtin_popcount(((1 << n) - 1) ^ m) & 1;
int cur = dw[mask] * (par ? -1 : 1);
ans += cur;
}
}
cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
344 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 |
360 KB |
Output is correct |
7 |
Correct |
1 ms |
344 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
344 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 |
360 KB |
Output is correct |
7 |
Correct |
1 ms |
344 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 |
473 ms |
4684 KB |
Output is correct |
12 |
Correct |
545 ms |
4376 KB |
Output is correct |
13 |
Correct |
285 ms |
3700 KB |
Output is correct |
14 |
Correct |
273 ms |
3644 KB |
Output is correct |
15 |
Correct |
981 ms |
6024 KB |
Output is correct |
16 |
Correct |
414 ms |
4248 KB |
Output is correct |
17 |
Correct |
415 ms |
4268 KB |
Output is correct |
18 |
Correct |
177 ms |
6020 KB |
Output is correct |
19 |
Correct |
204 ms |
3100 KB |
Output is correct |
20 |
Correct |
571 ms |
4812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
344 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 |
360 KB |
Output is correct |
7 |
Correct |
1 ms |
344 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 |
473 ms |
4684 KB |
Output is correct |
12 |
Correct |
545 ms |
4376 KB |
Output is correct |
13 |
Correct |
285 ms |
3700 KB |
Output is correct |
14 |
Correct |
273 ms |
3644 KB |
Output is correct |
15 |
Correct |
981 ms |
6024 KB |
Output is correct |
16 |
Correct |
414 ms |
4248 KB |
Output is correct |
17 |
Correct |
415 ms |
4268 KB |
Output is correct |
18 |
Correct |
177 ms |
6020 KB |
Output is correct |
19 |
Correct |
204 ms |
3100 KB |
Output is correct |
20 |
Correct |
571 ms |
4812 KB |
Output is correct |
21 |
Correct |
236 ms |
5260 KB |
Output is correct |
22 |
Correct |
816 ms |
5424 KB |
Output is correct |
23 |
Correct |
441 ms |
4276 KB |
Output is correct |
24 |
Correct |
372 ms |
4172 KB |
Output is correct |
25 |
Correct |
326 ms |
6164 KB |
Output is correct |
26 |
Correct |
599 ms |
4616 KB |
Output is correct |
27 |
Correct |
561 ms |
4532 KB |
Output is correct |
28 |
Correct |
212 ms |
7100 KB |
Output is correct |
29 |
Correct |
228 ms |
3076 KB |
Output is correct |
30 |
Correct |
536 ms |
5332 KB |
Output is correct |
31 |
Correct |
1037 ms |
5356 KB |
Output is correct |
32 |
Correct |
518 ms |
5176 KB |
Output is correct |
33 |
Correct |
319 ms |
3968 KB |
Output is correct |
34 |
Correct |
371 ms |
4144 KB |
Output is correct |
35 |
Correct |
584 ms |
4568 KB |
Output is correct |
36 |
Correct |
187 ms |
3180 KB |
Output is correct |
37 |
Correct |
989 ms |
5196 KB |
Output is correct |
38 |
Correct |
238 ms |
3296 KB |
Output is correct |
39 |
Correct |
455 ms |
4260 KB |
Output is correct |
40 |
Correct |
359 ms |
4116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
344 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 |
360 KB |
Output is correct |
7 |
Correct |
1 ms |
344 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 |
69 ms |
10716 KB |
Output is correct |
12 |
Correct |
70 ms |
10312 KB |
Output is correct |
13 |
Correct |
78 ms |
11916 KB |
Output is correct |
14 |
Correct |
100 ms |
11876 KB |
Output is correct |
15 |
Correct |
69 ms |
12020 KB |
Output is correct |
16 |
Correct |
99 ms |
11848 KB |
Output is correct |
17 |
Correct |
97 ms |
11860 KB |
Output is correct |
18 |
Correct |
57 ms |
12108 KB |
Output is correct |
19 |
Correct |
58 ms |
11744 KB |
Output is correct |
20 |
Correct |
67 ms |
11884 KB |
Output is correct |
21 |
Correct |
83 ms |
11872 KB |
Output is correct |
22 |
Correct |
116 ms |
11852 KB |
Output is correct |
23 |
Correct |
74 ms |
11888 KB |
Output is correct |
24 |
Correct |
101 ms |
11824 KB |
Output is correct |
25 |
Correct |
98 ms |
11852 KB |
Output is correct |
26 |
Correct |
54 ms |
11724 KB |
Output is correct |
27 |
Correct |
61 ms |
11916 KB |
Output is correct |
28 |
Correct |
59 ms |
11724 KB |
Output is correct |
29 |
Correct |
81 ms |
11888 KB |
Output is correct |
30 |
Correct |
101 ms |
11936 KB |
Output is correct |
31 |
Correct |
70 ms |
11852 KB |
Output is correct |
32 |
Correct |
110 ms |
11928 KB |
Output is correct |
33 |
Correct |
95 ms |
11936 KB |
Output is correct |
34 |
Correct |
54 ms |
11724 KB |
Output is correct |
35 |
Correct |
95 ms |
11960 KB |
Output is correct |
36 |
Correct |
88 ms |
11852 KB |
Output is correct |
37 |
Correct |
83 ms |
11864 KB |
Output is correct |
38 |
Correct |
97 ms |
11856 KB |
Output is correct |
39 |
Correct |
89 ms |
11976 KB |
Output is correct |
40 |
Correct |
92 ms |
11980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
344 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 |
360 KB |
Output is correct |
7 |
Correct |
1 ms |
344 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 |
473 ms |
4684 KB |
Output is correct |
12 |
Correct |
545 ms |
4376 KB |
Output is correct |
13 |
Correct |
285 ms |
3700 KB |
Output is correct |
14 |
Correct |
273 ms |
3644 KB |
Output is correct |
15 |
Correct |
981 ms |
6024 KB |
Output is correct |
16 |
Correct |
414 ms |
4248 KB |
Output is correct |
17 |
Correct |
415 ms |
4268 KB |
Output is correct |
18 |
Correct |
177 ms |
6020 KB |
Output is correct |
19 |
Correct |
204 ms |
3100 KB |
Output is correct |
20 |
Correct |
571 ms |
4812 KB |
Output is correct |
21 |
Correct |
236 ms |
5260 KB |
Output is correct |
22 |
Correct |
816 ms |
5424 KB |
Output is correct |
23 |
Correct |
441 ms |
4276 KB |
Output is correct |
24 |
Correct |
372 ms |
4172 KB |
Output is correct |
25 |
Correct |
326 ms |
6164 KB |
Output is correct |
26 |
Correct |
599 ms |
4616 KB |
Output is correct |
27 |
Correct |
561 ms |
4532 KB |
Output is correct |
28 |
Correct |
212 ms |
7100 KB |
Output is correct |
29 |
Correct |
228 ms |
3076 KB |
Output is correct |
30 |
Correct |
536 ms |
5332 KB |
Output is correct |
31 |
Correct |
1037 ms |
5356 KB |
Output is correct |
32 |
Correct |
518 ms |
5176 KB |
Output is correct |
33 |
Correct |
319 ms |
3968 KB |
Output is correct |
34 |
Correct |
371 ms |
4144 KB |
Output is correct |
35 |
Correct |
584 ms |
4568 KB |
Output is correct |
36 |
Correct |
187 ms |
3180 KB |
Output is correct |
37 |
Correct |
989 ms |
5196 KB |
Output is correct |
38 |
Correct |
238 ms |
3296 KB |
Output is correct |
39 |
Correct |
455 ms |
4260 KB |
Output is correct |
40 |
Correct |
359 ms |
4116 KB |
Output is correct |
41 |
Correct |
69 ms |
10716 KB |
Output is correct |
42 |
Correct |
70 ms |
10312 KB |
Output is correct |
43 |
Correct |
78 ms |
11916 KB |
Output is correct |
44 |
Correct |
100 ms |
11876 KB |
Output is correct |
45 |
Correct |
69 ms |
12020 KB |
Output is correct |
46 |
Correct |
99 ms |
11848 KB |
Output is correct |
47 |
Correct |
97 ms |
11860 KB |
Output is correct |
48 |
Correct |
57 ms |
12108 KB |
Output is correct |
49 |
Correct |
58 ms |
11744 KB |
Output is correct |
50 |
Correct |
67 ms |
11884 KB |
Output is correct |
51 |
Correct |
83 ms |
11872 KB |
Output is correct |
52 |
Correct |
116 ms |
11852 KB |
Output is correct |
53 |
Correct |
74 ms |
11888 KB |
Output is correct |
54 |
Correct |
101 ms |
11824 KB |
Output is correct |
55 |
Correct |
98 ms |
11852 KB |
Output is correct |
56 |
Correct |
54 ms |
11724 KB |
Output is correct |
57 |
Correct |
61 ms |
11916 KB |
Output is correct |
58 |
Correct |
59 ms |
11724 KB |
Output is correct |
59 |
Correct |
81 ms |
11888 KB |
Output is correct |
60 |
Correct |
101 ms |
11936 KB |
Output is correct |
61 |
Correct |
70 ms |
11852 KB |
Output is correct |
62 |
Correct |
110 ms |
11928 KB |
Output is correct |
63 |
Correct |
95 ms |
11936 KB |
Output is correct |
64 |
Correct |
54 ms |
11724 KB |
Output is correct |
65 |
Correct |
95 ms |
11960 KB |
Output is correct |
66 |
Correct |
88 ms |
11852 KB |
Output is correct |
67 |
Correct |
83 ms |
11864 KB |
Output is correct |
68 |
Correct |
97 ms |
11856 KB |
Output is correct |
69 |
Correct |
89 ms |
11976 KB |
Output is correct |
70 |
Correct |
92 ms |
11980 KB |
Output is correct |
71 |
Correct |
367 ms |
16080 KB |
Output is correct |
72 |
Correct |
505 ms |
16152 KB |
Output is correct |
73 |
Correct |
749 ms |
14800 KB |
Output is correct |
74 |
Correct |
1176 ms |
15304 KB |
Output is correct |
75 |
Correct |
559 ms |
17200 KB |
Output is correct |
76 |
Correct |
1200 ms |
15276 KB |
Output is correct |
77 |
Correct |
1133 ms |
15300 KB |
Output is correct |
78 |
Correct |
296 ms |
19052 KB |
Output is correct |
79 |
Correct |
354 ms |
13036 KB |
Output is correct |
80 |
Correct |
484 ms |
16188 KB |
Output is correct |
81 |
Correct |
791 ms |
16088 KB |
Output is correct |
82 |
Correct |
1264 ms |
14956 KB |
Output is correct |
83 |
Correct |
546 ms |
14352 KB |
Output is correct |
84 |
Correct |
1179 ms |
15252 KB |
Output is correct |
85 |
Correct |
1068 ms |
15296 KB |
Output is correct |
86 |
Correct |
274 ms |
12940 KB |
Output is correct |
87 |
Correct |
375 ms |
16036 KB |
Output is correct |
88 |
Correct |
356 ms |
13032 KB |
Output is correct |
89 |
Correct |
766 ms |
14828 KB |
Output is correct |
90 |
Correct |
1168 ms |
15224 KB |
Output is correct |
91 |
Correct |
520 ms |
14208 KB |
Output is correct |
92 |
Correct |
1268 ms |
15464 KB |
Output is correct |
93 |
Correct |
1054 ms |
15316 KB |
Output is correct |
94 |
Correct |
293 ms |
12940 KB |
Output is correct |
95 |
Correct |
938 ms |
15076 KB |
Output is correct |
96 |
Correct |
853 ms |
15000 KB |
Output is correct |
97 |
Correct |
870 ms |
14724 KB |
Output is correct |
98 |
Correct |
871 ms |
14624 KB |
Output is correct |
99 |
Correct |
874 ms |
14448 KB |
Output is correct |
100 |
Correct |
871 ms |
14352 KB |
Output is correct |