#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][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][0] = mask[msk];
// if(msk % 2) dp[0][msk][0] += mask[msk - 1];
dp[1][msk][0] = mask[msk];
// if(msk % 2 == 0) dp[1][msk][0] += mask[msk + 1];
continue;
}
dp[0][msk][i] += dp[0][msk][i-1];
dp[1][msk][i] += dp[1][msk][i-1];
if (msk & (1 << (i-1)))
dp[0][msk][i] += dp[0][msk ^ (1 << (i-1))][i-1];
else
dp[1][msk][i] += dp[1][msk ^ (1 << (i-1))][i-1];
sos[0][msk] = dp[0][msk][n];
sos[1][msk] = dp[1][msk][n];
}
}
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 += 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 |
2 ms |
8696 KB |
Output is correct |
3 |
Correct |
2 ms |
8700 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 |
8784 KB |
Output is correct |
9 |
Correct |
2 ms |
8528 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 |
2 ms |
8696 KB |
Output is correct |
3 |
Correct |
2 ms |
8700 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 |
8784 KB |
Output is correct |
9 |
Correct |
2 ms |
8528 KB |
Output is correct |
10 |
Correct |
2 ms |
8528 KB |
Output is correct |
11 |
Correct |
186 ms |
15464 KB |
Output is correct |
12 |
Correct |
204 ms |
12328 KB |
Output is correct |
13 |
Correct |
186 ms |
15432 KB |
Output is correct |
14 |
Correct |
167 ms |
11592 KB |
Output is correct |
15 |
Correct |
232 ms |
21200 KB |
Output is correct |
16 |
Correct |
208 ms |
21472 KB |
Output is correct |
17 |
Correct |
203 ms |
11628 KB |
Output is correct |
18 |
Correct |
149 ms |
19148 KB |
Output is correct |
19 |
Correct |
145 ms |
10612 KB |
Output is correct |
20 |
Correct |
207 ms |
22600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
2 ms |
8696 KB |
Output is correct |
3 |
Correct |
2 ms |
8700 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 |
8784 KB |
Output is correct |
9 |
Correct |
2 ms |
8528 KB |
Output is correct |
10 |
Correct |
2 ms |
8528 KB |
Output is correct |
11 |
Correct |
186 ms |
15464 KB |
Output is correct |
12 |
Correct |
204 ms |
12328 KB |
Output is correct |
13 |
Correct |
186 ms |
15432 KB |
Output is correct |
14 |
Correct |
167 ms |
11592 KB |
Output is correct |
15 |
Correct |
232 ms |
21200 KB |
Output is correct |
16 |
Correct |
208 ms |
21472 KB |
Output is correct |
17 |
Correct |
203 ms |
11628 KB |
Output is correct |
18 |
Correct |
149 ms |
19148 KB |
Output is correct |
19 |
Correct |
145 ms |
10612 KB |
Output is correct |
20 |
Correct |
207 ms |
22600 KB |
Output is correct |
21 |
Correct |
422 ms |
22092 KB |
Output is correct |
22 |
Correct |
222 ms |
16968 KB |
Output is correct |
23 |
Correct |
204 ms |
16456 KB |
Output is correct |
24 |
Correct |
204 ms |
27208 KB |
Output is correct |
25 |
Correct |
272 ms |
30804 KB |
Output is correct |
26 |
Correct |
256 ms |
29256 KB |
Output is correct |
27 |
Correct |
252 ms |
28744 KB |
Output is correct |
28 |
Correct |
149 ms |
29164 KB |
Output is correct |
29 |
Correct |
175 ms |
25672 KB |
Output is correct |
30 |
Correct |
346 ms |
22928 KB |
Output is correct |
31 |
Correct |
264 ms |
28740 KB |
Output is correct |
32 |
Correct |
229 ms |
16712 KB |
Output is correct |
33 |
Correct |
207 ms |
20956 KB |
Output is correct |
34 |
Correct |
238 ms |
26696 KB |
Output is correct |
35 |
Correct |
253 ms |
27976 KB |
Output is correct |
36 |
Correct |
131 ms |
25928 KB |
Output is correct |
37 |
Correct |
232 ms |
26168 KB |
Output is correct |
38 |
Correct |
194 ms |
20104 KB |
Output is correct |
39 |
Correct |
226 ms |
18760 KB |
Output is correct |
40 |
Correct |
225 ms |
24904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
2 ms |
8696 KB |
Output is correct |
3 |
Correct |
2 ms |
8700 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 |
8784 KB |
Output is correct |
9 |
Correct |
2 ms |
8528 KB |
Output is correct |
10 |
Correct |
2 ms |
8528 KB |
Output is correct |
11 |
Runtime error |
29 ms |
65536 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
2 ms |
8696 KB |
Output is correct |
3 |
Correct |
2 ms |
8700 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 |
8784 KB |
Output is correct |
9 |
Correct |
2 ms |
8528 KB |
Output is correct |
10 |
Correct |
2 ms |
8528 KB |
Output is correct |
11 |
Correct |
186 ms |
15464 KB |
Output is correct |
12 |
Correct |
204 ms |
12328 KB |
Output is correct |
13 |
Correct |
186 ms |
15432 KB |
Output is correct |
14 |
Correct |
167 ms |
11592 KB |
Output is correct |
15 |
Correct |
232 ms |
21200 KB |
Output is correct |
16 |
Correct |
208 ms |
21472 KB |
Output is correct |
17 |
Correct |
203 ms |
11628 KB |
Output is correct |
18 |
Correct |
149 ms |
19148 KB |
Output is correct |
19 |
Correct |
145 ms |
10612 KB |
Output is correct |
20 |
Correct |
207 ms |
22600 KB |
Output is correct |
21 |
Correct |
422 ms |
22092 KB |
Output is correct |
22 |
Correct |
222 ms |
16968 KB |
Output is correct |
23 |
Correct |
204 ms |
16456 KB |
Output is correct |
24 |
Correct |
204 ms |
27208 KB |
Output is correct |
25 |
Correct |
272 ms |
30804 KB |
Output is correct |
26 |
Correct |
256 ms |
29256 KB |
Output is correct |
27 |
Correct |
252 ms |
28744 KB |
Output is correct |
28 |
Correct |
149 ms |
29164 KB |
Output is correct |
29 |
Correct |
175 ms |
25672 KB |
Output is correct |
30 |
Correct |
346 ms |
22928 KB |
Output is correct |
31 |
Correct |
264 ms |
28740 KB |
Output is correct |
32 |
Correct |
229 ms |
16712 KB |
Output is correct |
33 |
Correct |
207 ms |
20956 KB |
Output is correct |
34 |
Correct |
238 ms |
26696 KB |
Output is correct |
35 |
Correct |
253 ms |
27976 KB |
Output is correct |
36 |
Correct |
131 ms |
25928 KB |
Output is correct |
37 |
Correct |
232 ms |
26168 KB |
Output is correct |
38 |
Correct |
194 ms |
20104 KB |
Output is correct |
39 |
Correct |
226 ms |
18760 KB |
Output is correct |
40 |
Correct |
225 ms |
24904 KB |
Output is correct |
41 |
Runtime error |
29 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Halted |
0 ms |
0 KB |
- |