#include <bits/stdc++.h>
#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], sos[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))];
sos[0][msk] = dp[0][msk];
sos[1][msk] = dp[1][msk];
}
}
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 += sos[x][init | msk];
else
ans -= sos[x][init | msk];
if (msk == 0) break;
}
cout << abs(ans) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
3 ms |
8528 KB |
Output is correct |
3 |
Correct |
2 ms |
8528 KB |
Output is correct |
4 |
Correct |
2 ms |
8528 KB |
Output is correct |
5 |
Correct |
2 ms |
8528 KB |
Output is correct |
6 |
Correct |
2 ms |
8528 KB |
Output is correct |
7 |
Correct |
2 ms |
8528 KB |
Output is correct |
8 |
Correct |
2 ms |
8528 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
3 ms |
8528 KB |
Output is correct |
3 |
Correct |
2 ms |
8528 KB |
Output is correct |
4 |
Correct |
2 ms |
8528 KB |
Output is correct |
5 |
Correct |
2 ms |
8528 KB |
Output is correct |
6 |
Correct |
2 ms |
8528 KB |
Output is correct |
7 |
Correct |
2 ms |
8528 KB |
Output is correct |
8 |
Correct |
2 ms |
8528 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8528 KB |
Output is correct |
11 |
Correct |
256 ms |
12636 KB |
Output is correct |
12 |
Correct |
184 ms |
12108 KB |
Output is correct |
13 |
Correct |
212 ms |
11592 KB |
Output is correct |
14 |
Correct |
213 ms |
11592 KB |
Output is correct |
15 |
Correct |
197 ms |
12872 KB |
Output is correct |
16 |
Correct |
283 ms |
19904 KB |
Output is correct |
17 |
Correct |
258 ms |
11592 KB |
Output is correct |
18 |
Correct |
135 ms |
18256 KB |
Output is correct |
19 |
Correct |
235 ms |
10448 KB |
Output is correct |
20 |
Correct |
261 ms |
17480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
3 ms |
8528 KB |
Output is correct |
3 |
Correct |
2 ms |
8528 KB |
Output is correct |
4 |
Correct |
2 ms |
8528 KB |
Output is correct |
5 |
Correct |
2 ms |
8528 KB |
Output is correct |
6 |
Correct |
2 ms |
8528 KB |
Output is correct |
7 |
Correct |
2 ms |
8528 KB |
Output is correct |
8 |
Correct |
2 ms |
8528 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8528 KB |
Output is correct |
11 |
Correct |
256 ms |
12636 KB |
Output is correct |
12 |
Correct |
184 ms |
12108 KB |
Output is correct |
13 |
Correct |
212 ms |
11592 KB |
Output is correct |
14 |
Correct |
213 ms |
11592 KB |
Output is correct |
15 |
Correct |
197 ms |
12872 KB |
Output is correct |
16 |
Correct |
283 ms |
19904 KB |
Output is correct |
17 |
Correct |
258 ms |
11592 KB |
Output is correct |
18 |
Correct |
135 ms |
18256 KB |
Output is correct |
19 |
Correct |
235 ms |
10448 KB |
Output is correct |
20 |
Correct |
261 ms |
17480 KB |
Output is correct |
21 |
Correct |
392 ms |
19676 KB |
Output is correct |
22 |
Correct |
247 ms |
12872 KB |
Output is correct |
23 |
Correct |
270 ms |
12616 KB |
Output is correct |
24 |
Correct |
296 ms |
23112 KB |
Output is correct |
25 |
Correct |
263 ms |
24324 KB |
Output is correct |
26 |
Correct |
341 ms |
12088 KB |
Output is correct |
27 |
Correct |
361 ms |
18596 KB |
Output is correct |
28 |
Correct |
157 ms |
24972 KB |
Output is correct |
29 |
Correct |
370 ms |
20552 KB |
Output is correct |
30 |
Correct |
308 ms |
18760 KB |
Output is correct |
31 |
Correct |
320 ms |
23372 KB |
Output is correct |
32 |
Correct |
307 ms |
12616 KB |
Output is correct |
33 |
Correct |
247 ms |
18260 KB |
Output is correct |
34 |
Correct |
349 ms |
20932 KB |
Output is correct |
35 |
Correct |
348 ms |
23880 KB |
Output is correct |
36 |
Correct |
142 ms |
23880 KB |
Output is correct |
37 |
Correct |
253 ms |
23112 KB |
Output is correct |
38 |
Correct |
314 ms |
17224 KB |
Output is correct |
39 |
Correct |
436 ms |
14080 KB |
Output is correct |
40 |
Correct |
334 ms |
15616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
3 ms |
8528 KB |
Output is correct |
3 |
Correct |
2 ms |
8528 KB |
Output is correct |
4 |
Correct |
2 ms |
8528 KB |
Output is correct |
5 |
Correct |
2 ms |
8528 KB |
Output is correct |
6 |
Correct |
2 ms |
8528 KB |
Output is correct |
7 |
Correct |
2 ms |
8528 KB |
Output is correct |
8 |
Correct |
2 ms |
8528 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8528 KB |
Output is correct |
11 |
Correct |
56 ms |
23272 KB |
Output is correct |
12 |
Correct |
53 ms |
23160 KB |
Output is correct |
13 |
Correct |
94 ms |
23284 KB |
Output is correct |
14 |
Correct |
69 ms |
23276 KB |
Output is correct |
15 |
Correct |
52 ms |
23272 KB |
Output is correct |
16 |
Correct |
73 ms |
23396 KB |
Output is correct |
17 |
Correct |
73 ms |
23180 KB |
Output is correct |
18 |
Correct |
45 ms |
23528 KB |
Output is correct |
19 |
Correct |
250 ms |
23028 KB |
Output is correct |
20 |
Correct |
48 ms |
23148 KB |
Output is correct |
21 |
Correct |
52 ms |
23184 KB |
Output is correct |
22 |
Correct |
63 ms |
23276 KB |
Output is correct |
23 |
Correct |
48 ms |
23156 KB |
Output is correct |
24 |
Correct |
60 ms |
23276 KB |
Output is correct |
25 |
Correct |
66 ms |
23272 KB |
Output is correct |
26 |
Correct |
38 ms |
23028 KB |
Output is correct |
27 |
Correct |
47 ms |
23372 KB |
Output is correct |
28 |
Correct |
348 ms |
23028 KB |
Output is correct |
29 |
Correct |
63 ms |
23144 KB |
Output is correct |
30 |
Correct |
76 ms |
23172 KB |
Output is correct |
31 |
Correct |
64 ms |
23276 KB |
Output is correct |
32 |
Correct |
66 ms |
23272 KB |
Output is correct |
33 |
Correct |
78 ms |
23272 KB |
Output is correct |
34 |
Correct |
44 ms |
23196 KB |
Output is correct |
35 |
Correct |
85 ms |
23144 KB |
Output is correct |
36 |
Correct |
86 ms |
23272 KB |
Output is correct |
37 |
Correct |
80 ms |
23272 KB |
Output is correct |
38 |
Correct |
86 ms |
23188 KB |
Output is correct |
39 |
Correct |
83 ms |
23240 KB |
Output is correct |
40 |
Correct |
82 ms |
23276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
3 ms |
8528 KB |
Output is correct |
3 |
Correct |
2 ms |
8528 KB |
Output is correct |
4 |
Correct |
2 ms |
8528 KB |
Output is correct |
5 |
Correct |
2 ms |
8528 KB |
Output is correct |
6 |
Correct |
2 ms |
8528 KB |
Output is correct |
7 |
Correct |
2 ms |
8528 KB |
Output is correct |
8 |
Correct |
2 ms |
8528 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8528 KB |
Output is correct |
11 |
Correct |
256 ms |
12636 KB |
Output is correct |
12 |
Correct |
184 ms |
12108 KB |
Output is correct |
13 |
Correct |
212 ms |
11592 KB |
Output is correct |
14 |
Correct |
213 ms |
11592 KB |
Output is correct |
15 |
Correct |
197 ms |
12872 KB |
Output is correct |
16 |
Correct |
283 ms |
19904 KB |
Output is correct |
17 |
Correct |
258 ms |
11592 KB |
Output is correct |
18 |
Correct |
135 ms |
18256 KB |
Output is correct |
19 |
Correct |
235 ms |
10448 KB |
Output is correct |
20 |
Correct |
261 ms |
17480 KB |
Output is correct |
21 |
Correct |
392 ms |
19676 KB |
Output is correct |
22 |
Correct |
247 ms |
12872 KB |
Output is correct |
23 |
Correct |
270 ms |
12616 KB |
Output is correct |
24 |
Correct |
296 ms |
23112 KB |
Output is correct |
25 |
Correct |
263 ms |
24324 KB |
Output is correct |
26 |
Correct |
341 ms |
12088 KB |
Output is correct |
27 |
Correct |
361 ms |
18596 KB |
Output is correct |
28 |
Correct |
157 ms |
24972 KB |
Output is correct |
29 |
Correct |
370 ms |
20552 KB |
Output is correct |
30 |
Correct |
308 ms |
18760 KB |
Output is correct |
31 |
Correct |
320 ms |
23372 KB |
Output is correct |
32 |
Correct |
307 ms |
12616 KB |
Output is correct |
33 |
Correct |
247 ms |
18260 KB |
Output is correct |
34 |
Correct |
349 ms |
20932 KB |
Output is correct |
35 |
Correct |
348 ms |
23880 KB |
Output is correct |
36 |
Correct |
142 ms |
23880 KB |
Output is correct |
37 |
Correct |
253 ms |
23112 KB |
Output is correct |
38 |
Correct |
314 ms |
17224 KB |
Output is correct |
39 |
Correct |
436 ms |
14080 KB |
Output is correct |
40 |
Correct |
334 ms |
15616 KB |
Output is correct |
41 |
Correct |
56 ms |
23272 KB |
Output is correct |
42 |
Correct |
53 ms |
23160 KB |
Output is correct |
43 |
Correct |
94 ms |
23284 KB |
Output is correct |
44 |
Correct |
69 ms |
23276 KB |
Output is correct |
45 |
Correct |
52 ms |
23272 KB |
Output is correct |
46 |
Correct |
73 ms |
23396 KB |
Output is correct |
47 |
Correct |
73 ms |
23180 KB |
Output is correct |
48 |
Correct |
45 ms |
23528 KB |
Output is correct |
49 |
Correct |
250 ms |
23028 KB |
Output is correct |
50 |
Correct |
48 ms |
23148 KB |
Output is correct |
51 |
Correct |
52 ms |
23184 KB |
Output is correct |
52 |
Correct |
63 ms |
23276 KB |
Output is correct |
53 |
Correct |
48 ms |
23156 KB |
Output is correct |
54 |
Correct |
60 ms |
23276 KB |
Output is correct |
55 |
Correct |
66 ms |
23272 KB |
Output is correct |
56 |
Correct |
38 ms |
23028 KB |
Output is correct |
57 |
Correct |
47 ms |
23372 KB |
Output is correct |
58 |
Correct |
348 ms |
23028 KB |
Output is correct |
59 |
Correct |
63 ms |
23144 KB |
Output is correct |
60 |
Correct |
76 ms |
23172 KB |
Output is correct |
61 |
Correct |
64 ms |
23276 KB |
Output is correct |
62 |
Correct |
66 ms |
23272 KB |
Output is correct |
63 |
Correct |
78 ms |
23272 KB |
Output is correct |
64 |
Correct |
44 ms |
23196 KB |
Output is correct |
65 |
Correct |
85 ms |
23144 KB |
Output is correct |
66 |
Correct |
86 ms |
23272 KB |
Output is correct |
67 |
Correct |
80 ms |
23272 KB |
Output is correct |
68 |
Correct |
86 ms |
23188 KB |
Output is correct |
69 |
Correct |
83 ms |
23240 KB |
Output is correct |
70 |
Correct |
82 ms |
23276 KB |
Output is correct |
71 |
Correct |
420 ms |
42532 KB |
Output is correct |
72 |
Correct |
482 ms |
34292 KB |
Output is correct |
73 |
Correct |
1231 ms |
43188 KB |
Output is correct |
74 |
Correct |
672 ms |
45544 KB |
Output is correct |
75 |
Correct |
477 ms |
50664 KB |
Output is correct |
76 |
Correct |
653 ms |
38296 KB |
Output is correct |
77 |
Correct |
737 ms |
48872 KB |
Output is correct |
78 |
Correct |
206 ms |
31076 KB |
Output is correct |
79 |
Execution timed out |
2063 ms |
35560 KB |
Time limit exceeded |
80 |
Halted |
0 ms |
0 KB |
- |