#include <bits/stdc++.h>
#pragma GCC target("popcnt")
#define ll long long
#define pii pair<int, int>
using namespace std;
const int N = 20, SPLIT = 6;
int n, q, mask[1<<N];
string vals;
int dp[2][1<<N];
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n >> q >> vals;
for (int i = 0; i < (1<<n); i++)
mask[i] = vals[i] - '0';
for (int i = 0; i <= n; i++) {
for (int msk = 0; msk < (1<<n); msk++) {
if(!i){
dp[0][msk] = mask[msk];
dp[1][msk] = mask[msk];
continue;
}
if (msk & (1 << (i-1)))
dp[0][msk] += dp[0][msk ^ (1 << (i-1))];
else
dp[1][msk] += dp[1][msk ^ (1 << (i-1))];
}
}
while (q--) {
string b;
cin >> b;
int c_marks=0, cc[2]={};
for (char c : b)
if (c == '?')
c_marks++;
else
cc[c-'0']++;
if (c_marks <= SPLIT) {
int init = 0;
int marks = 0;
for (int i = 0; i < n; i++)
if (b[i] == '1')
init |= (1 << (n-i-1));
else if (b[i] == '?')
marks |= (1 << (n-i-1));
int ans = 0;
for (int msk = marks; msk >= 0; msk = (msk-1)&marks) {
ans += mask[init | msk];
if (msk == 0) break;
}
cout << ans << '\n';
continue;
}
int x = (cc[1] <= SPLIT ? 0 : 1);
int init = 0;
int marks = 0;
for (int i = 0; i < n; i++) {
if (b[i] == '?' && x == 0)
init |= (1 << (n-i-1));
if (b[i] == '1' && x == 1)
init |= (1 << (n-i-1));
if (x == 0 && b[i] == '1')
marks |= (1 << (n-i-1));
if (x == 1 && b[i] == '0')
marks |= (1 << (n-i-1));
}
int ans = 0;
for (int msk = marks; msk >= 0; msk = (msk-1)&marks) {
if (__builtin_popcount(msk)%2 == cc[x] % 2)
ans += dp[x][init | msk];
else
ans -= dp[x][init | msk];
if (msk == 0) break;
}
cout << abs(ans) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4648 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4648 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4600 KB |
Output is correct |
11 |
Correct |
184 ms |
8520 KB |
Output is correct |
12 |
Correct |
177 ms |
8008 KB |
Output is correct |
13 |
Correct |
182 ms |
7448 KB |
Output is correct |
14 |
Correct |
165 ms |
7496 KB |
Output is correct |
15 |
Correct |
207 ms |
8556 KB |
Output is correct |
16 |
Correct |
191 ms |
7756 KB |
Output is correct |
17 |
Correct |
207 ms |
7552 KB |
Output is correct |
18 |
Correct |
116 ms |
9544 KB |
Output is correct |
19 |
Correct |
132 ms |
6476 KB |
Output is correct |
20 |
Correct |
186 ms |
8008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4648 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4600 KB |
Output is correct |
11 |
Correct |
184 ms |
8520 KB |
Output is correct |
12 |
Correct |
177 ms |
8008 KB |
Output is correct |
13 |
Correct |
182 ms |
7448 KB |
Output is correct |
14 |
Correct |
165 ms |
7496 KB |
Output is correct |
15 |
Correct |
207 ms |
8556 KB |
Output is correct |
16 |
Correct |
191 ms |
7756 KB |
Output is correct |
17 |
Correct |
207 ms |
7552 KB |
Output is correct |
18 |
Correct |
116 ms |
9544 KB |
Output is correct |
19 |
Correct |
132 ms |
6476 KB |
Output is correct |
20 |
Correct |
186 ms |
8008 KB |
Output is correct |
21 |
Correct |
317 ms |
8520 KB |
Output is correct |
22 |
Correct |
242 ms |
8776 KB |
Output is correct |
23 |
Correct |
237 ms |
7752 KB |
Output is correct |
24 |
Correct |
198 ms |
7496 KB |
Output is correct |
25 |
Correct |
259 ms |
9544 KB |
Output is correct |
26 |
Correct |
236 ms |
8008 KB |
Output is correct |
27 |
Correct |
237 ms |
7944 KB |
Output is correct |
28 |
Correct |
144 ms |
10568 KB |
Output is correct |
29 |
Correct |
164 ms |
6444 KB |
Output is correct |
30 |
Correct |
285 ms |
8776 KB |
Output is correct |
31 |
Correct |
258 ms |
8520 KB |
Output is correct |
32 |
Correct |
225 ms |
8520 KB |
Output is correct |
33 |
Correct |
211 ms |
7404 KB |
Output is correct |
34 |
Correct |
227 ms |
7496 KB |
Output is correct |
35 |
Correct |
270 ms |
8016 KB |
Output is correct |
36 |
Correct |
124 ms |
6376 KB |
Output is correct |
37 |
Correct |
229 ms |
8520 KB |
Output is correct |
38 |
Correct |
184 ms |
6472 KB |
Output is correct |
39 |
Correct |
225 ms |
7752 KB |
Output is correct |
40 |
Correct |
198 ms |
7496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4648 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4600 KB |
Output is correct |
11 |
Correct |
37 ms |
15092 KB |
Output is correct |
12 |
Correct |
40 ms |
15092 KB |
Output is correct |
13 |
Correct |
39 ms |
15084 KB |
Output is correct |
14 |
Correct |
46 ms |
15084 KB |
Output is correct |
15 |
Correct |
40 ms |
15092 KB |
Output is correct |
16 |
Correct |
58 ms |
15088 KB |
Output is correct |
17 |
Correct |
49 ms |
15048 KB |
Output is correct |
18 |
Correct |
34 ms |
15092 KB |
Output is correct |
19 |
Correct |
47 ms |
14836 KB |
Output is correct |
20 |
Correct |
43 ms |
15080 KB |
Output is correct |
21 |
Correct |
45 ms |
15084 KB |
Output is correct |
22 |
Correct |
50 ms |
15092 KB |
Output is correct |
23 |
Correct |
53 ms |
14980 KB |
Output is correct |
24 |
Correct |
45 ms |
15024 KB |
Output is correct |
25 |
Correct |
44 ms |
15084 KB |
Output is correct |
26 |
Correct |
31 ms |
14836 KB |
Output is correct |
27 |
Correct |
39 ms |
15084 KB |
Output is correct |
28 |
Correct |
42 ms |
14968 KB |
Output is correct |
29 |
Correct |
43 ms |
14956 KB |
Output is correct |
30 |
Correct |
41 ms |
15052 KB |
Output is correct |
31 |
Correct |
35 ms |
15092 KB |
Output is correct |
32 |
Correct |
53 ms |
14948 KB |
Output is correct |
33 |
Correct |
43 ms |
15088 KB |
Output is correct |
34 |
Correct |
42 ms |
14944 KB |
Output is correct |
35 |
Correct |
45 ms |
14952 KB |
Output is correct |
36 |
Correct |
47 ms |
14976 KB |
Output is correct |
37 |
Correct |
57 ms |
14944 KB |
Output is correct |
38 |
Correct |
46 ms |
15088 KB |
Output is correct |
39 |
Correct |
41 ms |
14948 KB |
Output is correct |
40 |
Correct |
45 ms |
14940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4648 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4600 KB |
Output is correct |
11 |
Correct |
184 ms |
8520 KB |
Output is correct |
12 |
Correct |
177 ms |
8008 KB |
Output is correct |
13 |
Correct |
182 ms |
7448 KB |
Output is correct |
14 |
Correct |
165 ms |
7496 KB |
Output is correct |
15 |
Correct |
207 ms |
8556 KB |
Output is correct |
16 |
Correct |
191 ms |
7756 KB |
Output is correct |
17 |
Correct |
207 ms |
7552 KB |
Output is correct |
18 |
Correct |
116 ms |
9544 KB |
Output is correct |
19 |
Correct |
132 ms |
6476 KB |
Output is correct |
20 |
Correct |
186 ms |
8008 KB |
Output is correct |
21 |
Correct |
317 ms |
8520 KB |
Output is correct |
22 |
Correct |
242 ms |
8776 KB |
Output is correct |
23 |
Correct |
237 ms |
7752 KB |
Output is correct |
24 |
Correct |
198 ms |
7496 KB |
Output is correct |
25 |
Correct |
259 ms |
9544 KB |
Output is correct |
26 |
Correct |
236 ms |
8008 KB |
Output is correct |
27 |
Correct |
237 ms |
7944 KB |
Output is correct |
28 |
Correct |
144 ms |
10568 KB |
Output is correct |
29 |
Correct |
164 ms |
6444 KB |
Output is correct |
30 |
Correct |
285 ms |
8776 KB |
Output is correct |
31 |
Correct |
258 ms |
8520 KB |
Output is correct |
32 |
Correct |
225 ms |
8520 KB |
Output is correct |
33 |
Correct |
211 ms |
7404 KB |
Output is correct |
34 |
Correct |
227 ms |
7496 KB |
Output is correct |
35 |
Correct |
270 ms |
8016 KB |
Output is correct |
36 |
Correct |
124 ms |
6376 KB |
Output is correct |
37 |
Correct |
229 ms |
8520 KB |
Output is correct |
38 |
Correct |
184 ms |
6472 KB |
Output is correct |
39 |
Correct |
225 ms |
7752 KB |
Output is correct |
40 |
Correct |
198 ms |
7496 KB |
Output is correct |
41 |
Correct |
37 ms |
15092 KB |
Output is correct |
42 |
Correct |
40 ms |
15092 KB |
Output is correct |
43 |
Correct |
39 ms |
15084 KB |
Output is correct |
44 |
Correct |
46 ms |
15084 KB |
Output is correct |
45 |
Correct |
40 ms |
15092 KB |
Output is correct |
46 |
Correct |
58 ms |
15088 KB |
Output is correct |
47 |
Correct |
49 ms |
15048 KB |
Output is correct |
48 |
Correct |
34 ms |
15092 KB |
Output is correct |
49 |
Correct |
47 ms |
14836 KB |
Output is correct |
50 |
Correct |
43 ms |
15080 KB |
Output is correct |
51 |
Correct |
45 ms |
15084 KB |
Output is correct |
52 |
Correct |
50 ms |
15092 KB |
Output is correct |
53 |
Correct |
53 ms |
14980 KB |
Output is correct |
54 |
Correct |
45 ms |
15024 KB |
Output is correct |
55 |
Correct |
44 ms |
15084 KB |
Output is correct |
56 |
Correct |
31 ms |
14836 KB |
Output is correct |
57 |
Correct |
39 ms |
15084 KB |
Output is correct |
58 |
Correct |
42 ms |
14968 KB |
Output is correct |
59 |
Correct |
43 ms |
14956 KB |
Output is correct |
60 |
Correct |
41 ms |
15052 KB |
Output is correct |
61 |
Correct |
35 ms |
15092 KB |
Output is correct |
62 |
Correct |
53 ms |
14948 KB |
Output is correct |
63 |
Correct |
43 ms |
15088 KB |
Output is correct |
64 |
Correct |
42 ms |
14944 KB |
Output is correct |
65 |
Correct |
45 ms |
14952 KB |
Output is correct |
66 |
Correct |
47 ms |
14976 KB |
Output is correct |
67 |
Correct |
57 ms |
14944 KB |
Output is correct |
68 |
Correct |
46 ms |
15088 KB |
Output is correct |
69 |
Correct |
41 ms |
14948 KB |
Output is correct |
70 |
Correct |
45 ms |
14940 KB |
Output is correct |
71 |
Correct |
341 ms |
19804 KB |
Output is correct |
72 |
Correct |
377 ms |
19940 KB |
Output is correct |
73 |
Correct |
349 ms |
18408 KB |
Output is correct |
74 |
Correct |
560 ms |
18760 KB |
Output is correct |
75 |
Correct |
461 ms |
20060 KB |
Output is correct |
76 |
Correct |
558 ms |
18012 KB |
Output is correct |
77 |
Correct |
474 ms |
19092 KB |
Output is correct |
78 |
Correct |
240 ms |
21852 KB |
Output is correct |
79 |
Correct |
323 ms |
19432 KB |
Output is correct |
80 |
Correct |
422 ms |
23544 KB |
Output is correct |
81 |
Correct |
543 ms |
34276 KB |
Output is correct |
82 |
Correct |
590 ms |
17752 KB |
Output is correct |
83 |
Correct |
342 ms |
37796 KB |
Output is correct |
84 |
Correct |
455 ms |
18804 KB |
Output is correct |
85 |
Correct |
454 ms |
19180 KB |
Output is correct |
86 |
Correct |
197 ms |
36588 KB |
Output is correct |
87 |
Correct |
374 ms |
35560 KB |
Output is correct |
88 |
Correct |
367 ms |
27864 KB |
Output is correct |
89 |
Correct |
440 ms |
34092 KB |
Output is correct |
90 |
Correct |
493 ms |
34464 KB |
Output is correct |
91 |
Correct |
331 ms |
29024 KB |
Output is correct |
92 |
Correct |
559 ms |
30184 KB |
Output is correct |
93 |
Correct |
533 ms |
30472 KB |
Output is correct |
94 |
Correct |
216 ms |
36648 KB |
Output is correct |
95 |
Correct |
489 ms |
37352 KB |
Output is correct |
96 |
Correct |
491 ms |
29992 KB |
Output is correct |
97 |
Correct |
456 ms |
33520 KB |
Output is correct |
98 |
Correct |
458 ms |
36580 KB |
Output is correct |
99 |
Correct |
447 ms |
35532 KB |
Output is correct |
100 |
Correct |
484 ms |
36052 KB |
Output is correct |