#include<bits/stdc++.h>
using namespace std;
const int MAX_L = 20;
int L, q, sum[2][1 << MAX_L];
string s;
bool checkBit (int _a, int _bit) { return (_a >> _bit) & 1; }
int main () {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("test.INP", "r", stdin);
cin >> L >> q >> s;
for (int i = 0; i < (1 << L); ++i) sum[0][i] = sum[1][i] = s[i] - '0';
for (int i = 0; i < L; ++i) {
for (int j = 0; j < (1 << L); ++j) {
if (checkBit(j, i) ) sum[0][j] += sum[0][j ^ (1 << i)];
else sum[1][j] += sum[1][j ^ (1 << i)];
}
}
while (q --) {
string t; cin >> t; reverse(t.begin(), t.end() );
int maskQ = 0, maskO1 = 0, maskO0 = 0;
for (int i = 0; i < L; ++i) {
if (t[i] == '?') maskQ |= (1 << i);
else if (t[i] == '1') maskO1 |= (1 << i);
else maskO0 |= (1 << i);
}
int ans = 0;
if (__builtin_popcount(maskQ) <= L / 3) {
for (int subMask = maskQ; ; subMask = (subMask - 1) & maskQ) {
ans += s[subMask | maskO1] - '0';
if (!subMask) break ;
}
}
else if (__builtin_popcount(maskO1) <= L / 3) {
for (int subMask = maskO1; ; subMask = (subMask - 1) & maskO1) {
if ( (__builtin_popcount(maskO1) - __builtin_popcount(subMask & maskO1) ) & 1) ans -= sum[0][maskQ | subMask];
else ans += sum[0][maskQ | subMask];
if (!subMask) break ;
}
}
else {
for (int subMask = maskO0; ; subMask = (subMask - 1) & maskO0) {
if (__builtin_popcount(subMask) & 1) ans -= sum[1][maskO1 | subMask];
else ans += sum[1][maskO1 | subMask];
if (!subMask) break ;
}
}
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
258 ms |
6904 KB |
Output is correct |
12 |
Correct |
257 ms |
6500 KB |
Output is correct |
13 |
Correct |
281 ms |
5752 KB |
Output is correct |
14 |
Correct |
275 ms |
5880 KB |
Output is correct |
15 |
Correct |
289 ms |
6840 KB |
Output is correct |
16 |
Correct |
303 ms |
5932 KB |
Output is correct |
17 |
Correct |
311 ms |
6008 KB |
Output is correct |
18 |
Correct |
200 ms |
7800 KB |
Output is correct |
19 |
Correct |
237 ms |
4728 KB |
Output is correct |
20 |
Correct |
269 ms |
6528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
258 ms |
6904 KB |
Output is correct |
12 |
Correct |
257 ms |
6500 KB |
Output is correct |
13 |
Correct |
281 ms |
5752 KB |
Output is correct |
14 |
Correct |
275 ms |
5880 KB |
Output is correct |
15 |
Correct |
289 ms |
6840 KB |
Output is correct |
16 |
Correct |
303 ms |
5932 KB |
Output is correct |
17 |
Correct |
311 ms |
6008 KB |
Output is correct |
18 |
Correct |
200 ms |
7800 KB |
Output is correct |
19 |
Correct |
237 ms |
4728 KB |
Output is correct |
20 |
Correct |
269 ms |
6528 KB |
Output is correct |
21 |
Correct |
284 ms |
6892 KB |
Output is correct |
22 |
Correct |
286 ms |
7032 KB |
Output is correct |
23 |
Correct |
317 ms |
6108 KB |
Output is correct |
24 |
Correct |
334 ms |
6040 KB |
Output is correct |
25 |
Correct |
331 ms |
7800 KB |
Output is correct |
26 |
Correct |
348 ms |
6136 KB |
Output is correct |
27 |
Correct |
348 ms |
5908 KB |
Output is correct |
28 |
Correct |
210 ms |
8312 KB |
Output is correct |
29 |
Correct |
273 ms |
4348 KB |
Output is correct |
30 |
Correct |
308 ms |
6380 KB |
Output is correct |
31 |
Correct |
359 ms |
6356 KB |
Output is correct |
32 |
Correct |
393 ms |
6392 KB |
Output is correct |
33 |
Correct |
296 ms |
5200 KB |
Output is correct |
34 |
Correct |
329 ms |
5112 KB |
Output is correct |
35 |
Correct |
349 ms |
5748 KB |
Output is correct |
36 |
Correct |
215 ms |
3832 KB |
Output is correct |
37 |
Correct |
272 ms |
5880 KB |
Output is correct |
38 |
Correct |
283 ms |
3576 KB |
Output is correct |
39 |
Correct |
317 ms |
4796 KB |
Output is correct |
40 |
Correct |
323 ms |
4472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
64 ms |
10876 KB |
Output is correct |
12 |
Correct |
64 ms |
10776 KB |
Output is correct |
13 |
Correct |
67 ms |
10648 KB |
Output is correct |
14 |
Correct |
96 ms |
10812 KB |
Output is correct |
15 |
Correct |
69 ms |
10776 KB |
Output is correct |
16 |
Correct |
91 ms |
10648 KB |
Output is correct |
17 |
Correct |
88 ms |
10672 KB |
Output is correct |
18 |
Correct |
59 ms |
10904 KB |
Output is correct |
19 |
Correct |
63 ms |
10648 KB |
Output is correct |
20 |
Correct |
68 ms |
10904 KB |
Output is correct |
21 |
Correct |
77 ms |
10680 KB |
Output is correct |
22 |
Correct |
86 ms |
10652 KB |
Output is correct |
23 |
Correct |
64 ms |
10776 KB |
Output is correct |
24 |
Correct |
70 ms |
10648 KB |
Output is correct |
25 |
Correct |
80 ms |
10648 KB |
Output is correct |
26 |
Correct |
58 ms |
10280 KB |
Output is correct |
27 |
Correct |
65 ms |
10644 KB |
Output is correct |
28 |
Correct |
64 ms |
10520 KB |
Output is correct |
29 |
Correct |
68 ms |
10652 KB |
Output is correct |
30 |
Correct |
71 ms |
10648 KB |
Output is correct |
31 |
Correct |
64 ms |
10516 KB |
Output is correct |
32 |
Correct |
80 ms |
10520 KB |
Output is correct |
33 |
Correct |
82 ms |
10624 KB |
Output is correct |
34 |
Correct |
58 ms |
10520 KB |
Output is correct |
35 |
Correct |
74 ms |
10500 KB |
Output is correct |
36 |
Correct |
74 ms |
10692 KB |
Output is correct |
37 |
Correct |
73 ms |
10520 KB |
Output is correct |
38 |
Correct |
73 ms |
10552 KB |
Output is correct |
39 |
Correct |
74 ms |
10520 KB |
Output is correct |
40 |
Correct |
75 ms |
10520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
258 ms |
6904 KB |
Output is correct |
12 |
Correct |
257 ms |
6500 KB |
Output is correct |
13 |
Correct |
281 ms |
5752 KB |
Output is correct |
14 |
Correct |
275 ms |
5880 KB |
Output is correct |
15 |
Correct |
289 ms |
6840 KB |
Output is correct |
16 |
Correct |
303 ms |
5932 KB |
Output is correct |
17 |
Correct |
311 ms |
6008 KB |
Output is correct |
18 |
Correct |
200 ms |
7800 KB |
Output is correct |
19 |
Correct |
237 ms |
4728 KB |
Output is correct |
20 |
Correct |
269 ms |
6528 KB |
Output is correct |
21 |
Correct |
284 ms |
6892 KB |
Output is correct |
22 |
Correct |
286 ms |
7032 KB |
Output is correct |
23 |
Correct |
317 ms |
6108 KB |
Output is correct |
24 |
Correct |
334 ms |
6040 KB |
Output is correct |
25 |
Correct |
331 ms |
7800 KB |
Output is correct |
26 |
Correct |
348 ms |
6136 KB |
Output is correct |
27 |
Correct |
348 ms |
5908 KB |
Output is correct |
28 |
Correct |
210 ms |
8312 KB |
Output is correct |
29 |
Correct |
273 ms |
4348 KB |
Output is correct |
30 |
Correct |
308 ms |
6380 KB |
Output is correct |
31 |
Correct |
359 ms |
6356 KB |
Output is correct |
32 |
Correct |
393 ms |
6392 KB |
Output is correct |
33 |
Correct |
296 ms |
5200 KB |
Output is correct |
34 |
Correct |
329 ms |
5112 KB |
Output is correct |
35 |
Correct |
349 ms |
5748 KB |
Output is correct |
36 |
Correct |
215 ms |
3832 KB |
Output is correct |
37 |
Correct |
272 ms |
5880 KB |
Output is correct |
38 |
Correct |
283 ms |
3576 KB |
Output is correct |
39 |
Correct |
317 ms |
4796 KB |
Output is correct |
40 |
Correct |
323 ms |
4472 KB |
Output is correct |
41 |
Correct |
64 ms |
10876 KB |
Output is correct |
42 |
Correct |
64 ms |
10776 KB |
Output is correct |
43 |
Correct |
67 ms |
10648 KB |
Output is correct |
44 |
Correct |
96 ms |
10812 KB |
Output is correct |
45 |
Correct |
69 ms |
10776 KB |
Output is correct |
46 |
Correct |
91 ms |
10648 KB |
Output is correct |
47 |
Correct |
88 ms |
10672 KB |
Output is correct |
48 |
Correct |
59 ms |
10904 KB |
Output is correct |
49 |
Correct |
63 ms |
10648 KB |
Output is correct |
50 |
Correct |
68 ms |
10904 KB |
Output is correct |
51 |
Correct |
77 ms |
10680 KB |
Output is correct |
52 |
Correct |
86 ms |
10652 KB |
Output is correct |
53 |
Correct |
64 ms |
10776 KB |
Output is correct |
54 |
Correct |
70 ms |
10648 KB |
Output is correct |
55 |
Correct |
80 ms |
10648 KB |
Output is correct |
56 |
Correct |
58 ms |
10280 KB |
Output is correct |
57 |
Correct |
65 ms |
10644 KB |
Output is correct |
58 |
Correct |
64 ms |
10520 KB |
Output is correct |
59 |
Correct |
68 ms |
10652 KB |
Output is correct |
60 |
Correct |
71 ms |
10648 KB |
Output is correct |
61 |
Correct |
64 ms |
10516 KB |
Output is correct |
62 |
Correct |
80 ms |
10520 KB |
Output is correct |
63 |
Correct |
82 ms |
10624 KB |
Output is correct |
64 |
Correct |
58 ms |
10520 KB |
Output is correct |
65 |
Correct |
74 ms |
10500 KB |
Output is correct |
66 |
Correct |
74 ms |
10692 KB |
Output is correct |
67 |
Correct |
73 ms |
10520 KB |
Output is correct |
68 |
Correct |
73 ms |
10552 KB |
Output is correct |
69 |
Correct |
74 ms |
10520 KB |
Output is correct |
70 |
Correct |
75 ms |
10520 KB |
Output is correct |
71 |
Correct |
529 ms |
15428 KB |
Output is correct |
72 |
Correct |
534 ms |
36508 KB |
Output is correct |
73 |
Correct |
571 ms |
35096 KB |
Output is correct |
74 |
Correct |
974 ms |
35308 KB |
Output is correct |
75 |
Correct |
600 ms |
37400 KB |
Output is correct |
76 |
Correct |
1070 ms |
35608 KB |
Output is correct |
77 |
Correct |
859 ms |
35540 KB |
Output is correct |
78 |
Correct |
347 ms |
39284 KB |
Output is correct |
79 |
Correct |
464 ms |
33396 KB |
Output is correct |
80 |
Correct |
574 ms |
36632 KB |
Output is correct |
81 |
Correct |
749 ms |
36420 KB |
Output is correct |
82 |
Correct |
929 ms |
35412 KB |
Output is correct |
83 |
Correct |
488 ms |
34424 KB |
Output is correct |
84 |
Correct |
638 ms |
35408 KB |
Output is correct |
85 |
Correct |
906 ms |
35536 KB |
Output is correct |
86 |
Correct |
370 ms |
33340 KB |
Output is correct |
87 |
Correct |
504 ms |
36176 KB |
Output is correct |
88 |
Correct |
499 ms |
33188 KB |
Output is correct |
89 |
Correct |
573 ms |
34932 KB |
Output is correct |
90 |
Correct |
621 ms |
35300 KB |
Output is correct |
91 |
Correct |
462 ms |
34500 KB |
Output is correct |
92 |
Correct |
863 ms |
35580 KB |
Output is correct |
93 |
Correct |
862 ms |
35416 KB |
Output is correct |
94 |
Correct |
326 ms |
33304 KB |
Output is correct |
95 |
Correct |
738 ms |
35480 KB |
Output is correct |
96 |
Correct |
704 ms |
35348 KB |
Output is correct |
97 |
Correct |
753 ms |
35336 KB |
Output is correct |
98 |
Correct |
730 ms |
35372 KB |
Output is correct |
99 |
Correct |
763 ms |
35372 KB |
Output is correct |
100 |
Correct |
758 ms |
35560 KB |
Output is correct |