#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 (false && 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';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 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 |
2 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 |
2 ms |
4432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 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 |
2 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 |
2 ms |
4432 KB |
Output is correct |
11 |
Correct |
209 ms |
8520 KB |
Output is correct |
12 |
Correct |
217 ms |
8008 KB |
Output is correct |
13 |
Correct |
188 ms |
7500 KB |
Output is correct |
14 |
Correct |
184 ms |
7496 KB |
Output is correct |
15 |
Correct |
179 ms |
8444 KB |
Output is correct |
16 |
Correct |
234 ms |
7752 KB |
Output is correct |
17 |
Correct |
235 ms |
7748 KB |
Output is correct |
18 |
Correct |
115 ms |
9544 KB |
Output is correct |
19 |
Correct |
191 ms |
6480 KB |
Output is correct |
20 |
Correct |
200 ms |
8008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 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 |
2 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 |
2 ms |
4432 KB |
Output is correct |
11 |
Correct |
209 ms |
8520 KB |
Output is correct |
12 |
Correct |
217 ms |
8008 KB |
Output is correct |
13 |
Correct |
188 ms |
7500 KB |
Output is correct |
14 |
Correct |
184 ms |
7496 KB |
Output is correct |
15 |
Correct |
179 ms |
8444 KB |
Output is correct |
16 |
Correct |
234 ms |
7752 KB |
Output is correct |
17 |
Correct |
235 ms |
7748 KB |
Output is correct |
18 |
Correct |
115 ms |
9544 KB |
Output is correct |
19 |
Correct |
191 ms |
6480 KB |
Output is correct |
20 |
Correct |
200 ms |
8008 KB |
Output is correct |
21 |
Correct |
300 ms |
8520 KB |
Output is correct |
22 |
Correct |
256 ms |
8784 KB |
Output is correct |
23 |
Correct |
256 ms |
7600 KB |
Output is correct |
24 |
Correct |
243 ms |
7496 KB |
Output is correct |
25 |
Correct |
256 ms |
9412 KB |
Output is correct |
26 |
Correct |
284 ms |
8008 KB |
Output is correct |
27 |
Correct |
286 ms |
8008 KB |
Output is correct |
28 |
Correct |
122 ms |
10580 KB |
Output is correct |
29 |
Correct |
268 ms |
6472 KB |
Output is correct |
30 |
Correct |
267 ms |
8824 KB |
Output is correct |
31 |
Correct |
278 ms |
8528 KB |
Output is correct |
32 |
Correct |
269 ms |
8520 KB |
Output is correct |
33 |
Correct |
206 ms |
7408 KB |
Output is correct |
34 |
Correct |
264 ms |
7608 KB |
Output is correct |
35 |
Correct |
292 ms |
8008 KB |
Output is correct |
36 |
Correct |
120 ms |
6472 KB |
Output is correct |
37 |
Correct |
191 ms |
8520 KB |
Output is correct |
38 |
Correct |
241 ms |
6480 KB |
Output is correct |
39 |
Correct |
327 ms |
7732 KB |
Output is correct |
40 |
Correct |
277 ms |
7496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 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 |
2 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 |
2 ms |
4432 KB |
Output is correct |
11 |
Correct |
40 ms |
15092 KB |
Output is correct |
12 |
Correct |
49 ms |
15104 KB |
Output is correct |
13 |
Correct |
70 ms |
15080 KB |
Output is correct |
14 |
Correct |
54 ms |
15092 KB |
Output is correct |
15 |
Correct |
54 ms |
15080 KB |
Output is correct |
16 |
Correct |
49 ms |
15084 KB |
Output is correct |
17 |
Correct |
49 ms |
15080 KB |
Output is correct |
18 |
Correct |
32 ms |
15084 KB |
Output is correct |
19 |
Correct |
147 ms |
14944 KB |
Output is correct |
20 |
Correct |
45 ms |
14940 KB |
Output is correct |
21 |
Correct |
42 ms |
14960 KB |
Output is correct |
22 |
Correct |
58 ms |
14940 KB |
Output is correct |
23 |
Correct |
46 ms |
15088 KB |
Output is correct |
24 |
Correct |
54 ms |
15096 KB |
Output is correct |
25 |
Correct |
61 ms |
15080 KB |
Output is correct |
26 |
Correct |
38 ms |
14836 KB |
Output is correct |
27 |
Correct |
39 ms |
15112 KB |
Output is correct |
28 |
Correct |
217 ms |
14836 KB |
Output is correct |
29 |
Correct |
46 ms |
15088 KB |
Output is correct |
30 |
Correct |
57 ms |
14960 KB |
Output is correct |
31 |
Correct |
46 ms |
15080 KB |
Output is correct |
32 |
Correct |
49 ms |
15080 KB |
Output is correct |
33 |
Correct |
64 ms |
14944 KB |
Output is correct |
34 |
Correct |
34 ms |
14836 KB |
Output is correct |
35 |
Correct |
56 ms |
15064 KB |
Output is correct |
36 |
Correct |
56 ms |
15084 KB |
Output is correct |
37 |
Correct |
63 ms |
15084 KB |
Output is correct |
38 |
Correct |
65 ms |
15084 KB |
Output is correct |
39 |
Correct |
53 ms |
15084 KB |
Output is correct |
40 |
Correct |
58 ms |
15092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 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 |
2 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 |
2 ms |
4432 KB |
Output is correct |
11 |
Correct |
209 ms |
8520 KB |
Output is correct |
12 |
Correct |
217 ms |
8008 KB |
Output is correct |
13 |
Correct |
188 ms |
7500 KB |
Output is correct |
14 |
Correct |
184 ms |
7496 KB |
Output is correct |
15 |
Correct |
179 ms |
8444 KB |
Output is correct |
16 |
Correct |
234 ms |
7752 KB |
Output is correct |
17 |
Correct |
235 ms |
7748 KB |
Output is correct |
18 |
Correct |
115 ms |
9544 KB |
Output is correct |
19 |
Correct |
191 ms |
6480 KB |
Output is correct |
20 |
Correct |
200 ms |
8008 KB |
Output is correct |
21 |
Correct |
300 ms |
8520 KB |
Output is correct |
22 |
Correct |
256 ms |
8784 KB |
Output is correct |
23 |
Correct |
256 ms |
7600 KB |
Output is correct |
24 |
Correct |
243 ms |
7496 KB |
Output is correct |
25 |
Correct |
256 ms |
9412 KB |
Output is correct |
26 |
Correct |
284 ms |
8008 KB |
Output is correct |
27 |
Correct |
286 ms |
8008 KB |
Output is correct |
28 |
Correct |
122 ms |
10580 KB |
Output is correct |
29 |
Correct |
268 ms |
6472 KB |
Output is correct |
30 |
Correct |
267 ms |
8824 KB |
Output is correct |
31 |
Correct |
278 ms |
8528 KB |
Output is correct |
32 |
Correct |
269 ms |
8520 KB |
Output is correct |
33 |
Correct |
206 ms |
7408 KB |
Output is correct |
34 |
Correct |
264 ms |
7608 KB |
Output is correct |
35 |
Correct |
292 ms |
8008 KB |
Output is correct |
36 |
Correct |
120 ms |
6472 KB |
Output is correct |
37 |
Correct |
191 ms |
8520 KB |
Output is correct |
38 |
Correct |
241 ms |
6480 KB |
Output is correct |
39 |
Correct |
327 ms |
7732 KB |
Output is correct |
40 |
Correct |
277 ms |
7496 KB |
Output is correct |
41 |
Correct |
40 ms |
15092 KB |
Output is correct |
42 |
Correct |
49 ms |
15104 KB |
Output is correct |
43 |
Correct |
70 ms |
15080 KB |
Output is correct |
44 |
Correct |
54 ms |
15092 KB |
Output is correct |
45 |
Correct |
54 ms |
15080 KB |
Output is correct |
46 |
Correct |
49 ms |
15084 KB |
Output is correct |
47 |
Correct |
49 ms |
15080 KB |
Output is correct |
48 |
Correct |
32 ms |
15084 KB |
Output is correct |
49 |
Correct |
147 ms |
14944 KB |
Output is correct |
50 |
Correct |
45 ms |
14940 KB |
Output is correct |
51 |
Correct |
42 ms |
14960 KB |
Output is correct |
52 |
Correct |
58 ms |
14940 KB |
Output is correct |
53 |
Correct |
46 ms |
15088 KB |
Output is correct |
54 |
Correct |
54 ms |
15096 KB |
Output is correct |
55 |
Correct |
61 ms |
15080 KB |
Output is correct |
56 |
Correct |
38 ms |
14836 KB |
Output is correct |
57 |
Correct |
39 ms |
15112 KB |
Output is correct |
58 |
Correct |
217 ms |
14836 KB |
Output is correct |
59 |
Correct |
46 ms |
15088 KB |
Output is correct |
60 |
Correct |
57 ms |
14960 KB |
Output is correct |
61 |
Correct |
46 ms |
15080 KB |
Output is correct |
62 |
Correct |
49 ms |
15080 KB |
Output is correct |
63 |
Correct |
64 ms |
14944 KB |
Output is correct |
64 |
Correct |
34 ms |
14836 KB |
Output is correct |
65 |
Correct |
56 ms |
15064 KB |
Output is correct |
66 |
Correct |
56 ms |
15084 KB |
Output is correct |
67 |
Correct |
63 ms |
15084 KB |
Output is correct |
68 |
Correct |
65 ms |
15084 KB |
Output is correct |
69 |
Correct |
53 ms |
15084 KB |
Output is correct |
70 |
Correct |
58 ms |
15092 KB |
Output is correct |
71 |
Correct |
390 ms |
19744 KB |
Output is correct |
72 |
Correct |
404 ms |
19944 KB |
Output is correct |
73 |
Correct |
822 ms |
18408 KB |
Output is correct |
74 |
Correct |
543 ms |
18920 KB |
Output is correct |
75 |
Correct |
399 ms |
20716 KB |
Output is correct |
76 |
Correct |
567 ms |
19176 KB |
Output is correct |
77 |
Correct |
607 ms |
18920 KB |
Output is correct |
78 |
Correct |
179 ms |
22760 KB |
Output is correct |
79 |
Execution timed out |
2064 ms |
23384 KB |
Time limit exceeded |
80 |
Halted |
0 ms |
0 KB |
- |