#include<iostream>
#include<string>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#define maxp 1048580
using namespace std;
int st[maxp],n,p,zer[maxp], on[maxp], cnt[maxp];
int ab(int x) { return x > 0 ? x : -x; }
void init(int N, int Q, string S) {
n = N; p = 1 << n;
for (int i = 0; i < p; i++) zer[i] = on[i] = st[i] = S[i] - '0';
for (int i = 1; i < p; i<<=1) {
for (int j = 0; j < p; j++) {
if (j & i) zer[j] += zer[j ^ i];
else on[j] += on[j ^ i];
}
}
cnt[0] = 0;
for (int i = 0; i < p/2; i++) {
cnt[i << 1] = cnt[i];
cnt[(i << 1) + 1] = cnt[i] + 1;
}
}
int query(string A) {
int a = 0, b = 0, q = 0, res=0;
for (int i = 0; i < n; i++) {
if (A[i] == '0') a|=1<<(n-1-i);
else if (A[i] == '1') b |= 1 << (n - 1 - i);
else q |= 1 << (n - 1 - i);
}
int oc = cnt[b], zc = cnt[a], qc = cnt[q];
if (oc < zc && oc < qc) {
res = 0;
for (int i = b; i >= 0; i--) {
i &= b;
if (cnt[i | q] & 1) res += zer[i | q];
else res -= zer[i | q];
}
res= ab(res);
}
else if (qc > zc) {
res = 0;
for (int i = a; i >= 0; i--) {
i &= a;
if (cnt[i | b] & 1) res += on[i | b];
else res -= on[i | b];
}
res= ab(res);
}
else {
res = 0;
for (int i = q; i >= 0; i--) {
i &= q;
res += st[i | b];
}
}
return res;
}
/*
4 8
3141592653589793
0101
?01?
??1?
?0??
1?00
01?1
??10
????
3 5
12345678
000
0??
1?0
?11
???
*/
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int N, Q; cin >> N >> Q;
string S; cin >> S;
init(N, Q, move(S));
for (int i = 0; i < Q; i++) {
string A;
cin >> A;
cout << query(move(A)) << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
3 ms |
6492 KB |
Output is correct |
3 |
Correct |
3 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
3 ms |
6616 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6628 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
3 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
3 ms |
6492 KB |
Output is correct |
3 |
Correct |
3 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
3 ms |
6616 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6628 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
3 ms |
6492 KB |
Output is correct |
11 |
Correct |
1245 ms |
16468 KB |
Output is correct |
12 |
Correct |
1246 ms |
16068 KB |
Output is correct |
13 |
Correct |
1268 ms |
15156 KB |
Output is correct |
14 |
Correct |
1269 ms |
15364 KB |
Output is correct |
15 |
Correct |
1252 ms |
16332 KB |
Output is correct |
16 |
Correct |
1278 ms |
15460 KB |
Output is correct |
17 |
Correct |
1275 ms |
15228 KB |
Output is correct |
18 |
Correct |
1216 ms |
17084 KB |
Output is correct |
19 |
Correct |
1247 ms |
14316 KB |
Output is correct |
20 |
Correct |
1247 ms |
15912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
3 ms |
6492 KB |
Output is correct |
3 |
Correct |
3 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
3 ms |
6616 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6628 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
3 ms |
6492 KB |
Output is correct |
11 |
Correct |
1245 ms |
16468 KB |
Output is correct |
12 |
Correct |
1246 ms |
16068 KB |
Output is correct |
13 |
Correct |
1268 ms |
15156 KB |
Output is correct |
14 |
Correct |
1269 ms |
15364 KB |
Output is correct |
15 |
Correct |
1252 ms |
16332 KB |
Output is correct |
16 |
Correct |
1278 ms |
15460 KB |
Output is correct |
17 |
Correct |
1275 ms |
15228 KB |
Output is correct |
18 |
Correct |
1216 ms |
17084 KB |
Output is correct |
19 |
Correct |
1247 ms |
14316 KB |
Output is correct |
20 |
Correct |
1247 ms |
15912 KB |
Output is correct |
21 |
Correct |
1262 ms |
18004 KB |
Output is correct |
22 |
Correct |
1277 ms |
18544 KB |
Output is correct |
23 |
Correct |
1322 ms |
17232 KB |
Output is correct |
24 |
Correct |
1303 ms |
17056 KB |
Output is correct |
25 |
Correct |
1293 ms |
18880 KB |
Output is correct |
26 |
Correct |
1310 ms |
17920 KB |
Output is correct |
27 |
Correct |
1320 ms |
17520 KB |
Output is correct |
28 |
Correct |
1229 ms |
20292 KB |
Output is correct |
29 |
Correct |
1267 ms |
16024 KB |
Output is correct |
30 |
Correct |
1287 ms |
18216 KB |
Output is correct |
31 |
Correct |
1287 ms |
18360 KB |
Output is correct |
32 |
Correct |
1304 ms |
18280 KB |
Output is correct |
33 |
Correct |
1280 ms |
16928 KB |
Output is correct |
34 |
Correct |
1294 ms |
16880 KB |
Output is correct |
35 |
Correct |
1342 ms |
17600 KB |
Output is correct |
36 |
Correct |
1223 ms |
16180 KB |
Output is correct |
37 |
Correct |
1259 ms |
18212 KB |
Output is correct |
38 |
Correct |
1280 ms |
16268 KB |
Output is correct |
39 |
Correct |
1297 ms |
17212 KB |
Output is correct |
40 |
Correct |
1316 ms |
17100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
3 ms |
6492 KB |
Output is correct |
3 |
Correct |
3 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
3 ms |
6616 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6628 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
3 ms |
6492 KB |
Output is correct |
11 |
Correct |
93 ms |
19188 KB |
Output is correct |
12 |
Correct |
90 ms |
19256 KB |
Output is correct |
13 |
Correct |
95 ms |
19076 KB |
Output is correct |
14 |
Correct |
111 ms |
19196 KB |
Output is correct |
15 |
Correct |
91 ms |
19040 KB |
Output is correct |
16 |
Correct |
105 ms |
19200 KB |
Output is correct |
17 |
Correct |
104 ms |
19188 KB |
Output is correct |
18 |
Correct |
86 ms |
19200 KB |
Output is correct |
19 |
Correct |
91 ms |
19184 KB |
Output is correct |
20 |
Correct |
109 ms |
19192 KB |
Output is correct |
21 |
Correct |
93 ms |
19188 KB |
Output is correct |
22 |
Correct |
100 ms |
19200 KB |
Output is correct |
23 |
Correct |
92 ms |
18928 KB |
Output is correct |
24 |
Correct |
101 ms |
18932 KB |
Output is correct |
25 |
Correct |
103 ms |
19200 KB |
Output is correct |
26 |
Correct |
89 ms |
19192 KB |
Output is correct |
27 |
Correct |
90 ms |
19188 KB |
Output is correct |
28 |
Correct |
95 ms |
19200 KB |
Output is correct |
29 |
Correct |
97 ms |
19192 KB |
Output is correct |
30 |
Correct |
97 ms |
19192 KB |
Output is correct |
31 |
Correct |
91 ms |
19188 KB |
Output is correct |
32 |
Correct |
110 ms |
18936 KB |
Output is correct |
33 |
Correct |
104 ms |
19032 KB |
Output is correct |
34 |
Correct |
86 ms |
19188 KB |
Output is correct |
35 |
Correct |
99 ms |
19452 KB |
Output is correct |
36 |
Correct |
101 ms |
19108 KB |
Output is correct |
37 |
Correct |
105 ms |
19192 KB |
Output is correct |
38 |
Correct |
99 ms |
19188 KB |
Output is correct |
39 |
Correct |
100 ms |
19192 KB |
Output is correct |
40 |
Correct |
102 ms |
19188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
3 ms |
6492 KB |
Output is correct |
3 |
Correct |
3 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
3 ms |
6616 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6628 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
3 ms |
6492 KB |
Output is correct |
11 |
Correct |
1245 ms |
16468 KB |
Output is correct |
12 |
Correct |
1246 ms |
16068 KB |
Output is correct |
13 |
Correct |
1268 ms |
15156 KB |
Output is correct |
14 |
Correct |
1269 ms |
15364 KB |
Output is correct |
15 |
Correct |
1252 ms |
16332 KB |
Output is correct |
16 |
Correct |
1278 ms |
15460 KB |
Output is correct |
17 |
Correct |
1275 ms |
15228 KB |
Output is correct |
18 |
Correct |
1216 ms |
17084 KB |
Output is correct |
19 |
Correct |
1247 ms |
14316 KB |
Output is correct |
20 |
Correct |
1247 ms |
15912 KB |
Output is correct |
21 |
Correct |
1262 ms |
18004 KB |
Output is correct |
22 |
Correct |
1277 ms |
18544 KB |
Output is correct |
23 |
Correct |
1322 ms |
17232 KB |
Output is correct |
24 |
Correct |
1303 ms |
17056 KB |
Output is correct |
25 |
Correct |
1293 ms |
18880 KB |
Output is correct |
26 |
Correct |
1310 ms |
17920 KB |
Output is correct |
27 |
Correct |
1320 ms |
17520 KB |
Output is correct |
28 |
Correct |
1229 ms |
20292 KB |
Output is correct |
29 |
Correct |
1267 ms |
16024 KB |
Output is correct |
30 |
Correct |
1287 ms |
18216 KB |
Output is correct |
31 |
Correct |
1287 ms |
18360 KB |
Output is correct |
32 |
Correct |
1304 ms |
18280 KB |
Output is correct |
33 |
Correct |
1280 ms |
16928 KB |
Output is correct |
34 |
Correct |
1294 ms |
16880 KB |
Output is correct |
35 |
Correct |
1342 ms |
17600 KB |
Output is correct |
36 |
Correct |
1223 ms |
16180 KB |
Output is correct |
37 |
Correct |
1259 ms |
18212 KB |
Output is correct |
38 |
Correct |
1280 ms |
16268 KB |
Output is correct |
39 |
Correct |
1297 ms |
17212 KB |
Output is correct |
40 |
Correct |
1316 ms |
17100 KB |
Output is correct |
41 |
Correct |
93 ms |
19188 KB |
Output is correct |
42 |
Correct |
90 ms |
19256 KB |
Output is correct |
43 |
Correct |
95 ms |
19076 KB |
Output is correct |
44 |
Correct |
111 ms |
19196 KB |
Output is correct |
45 |
Correct |
91 ms |
19040 KB |
Output is correct |
46 |
Correct |
105 ms |
19200 KB |
Output is correct |
47 |
Correct |
104 ms |
19188 KB |
Output is correct |
48 |
Correct |
86 ms |
19200 KB |
Output is correct |
49 |
Correct |
91 ms |
19184 KB |
Output is correct |
50 |
Correct |
109 ms |
19192 KB |
Output is correct |
51 |
Correct |
93 ms |
19188 KB |
Output is correct |
52 |
Correct |
100 ms |
19200 KB |
Output is correct |
53 |
Correct |
92 ms |
18928 KB |
Output is correct |
54 |
Correct |
101 ms |
18932 KB |
Output is correct |
55 |
Correct |
103 ms |
19200 KB |
Output is correct |
56 |
Correct |
89 ms |
19192 KB |
Output is correct |
57 |
Correct |
90 ms |
19188 KB |
Output is correct |
58 |
Correct |
95 ms |
19200 KB |
Output is correct |
59 |
Correct |
97 ms |
19192 KB |
Output is correct |
60 |
Correct |
97 ms |
19192 KB |
Output is correct |
61 |
Correct |
91 ms |
19188 KB |
Output is correct |
62 |
Correct |
110 ms |
18936 KB |
Output is correct |
63 |
Correct |
104 ms |
19032 KB |
Output is correct |
64 |
Correct |
86 ms |
19188 KB |
Output is correct |
65 |
Correct |
99 ms |
19452 KB |
Output is correct |
66 |
Correct |
101 ms |
19108 KB |
Output is correct |
67 |
Correct |
105 ms |
19192 KB |
Output is correct |
68 |
Correct |
99 ms |
19188 KB |
Output is correct |
69 |
Correct |
100 ms |
19192 KB |
Output is correct |
70 |
Correct |
102 ms |
19188 KB |
Output is correct |
71 |
Correct |
1407 ms |
33168 KB |
Output is correct |
72 |
Correct |
1410 ms |
33556 KB |
Output is correct |
73 |
Correct |
1481 ms |
31756 KB |
Output is correct |
74 |
Correct |
1626 ms |
32224 KB |
Output is correct |
75 |
Correct |
1390 ms |
34180 KB |
Output is correct |
76 |
Correct |
1728 ms |
32560 KB |
Output is correct |
77 |
Correct |
1724 ms |
32196 KB |
Output is correct |
78 |
Correct |
1305 ms |
35952 KB |
Output is correct |
79 |
Correct |
1410 ms |
29936 KB |
Output is correct |
80 |
Correct |
1441 ms |
32928 KB |
Output is correct |
81 |
Correct |
1528 ms |
22428 KB |
Output is correct |
82 |
Correct |
1549 ms |
20860 KB |
Output is correct |
83 |
Correct |
1410 ms |
20120 KB |
Output is correct |
84 |
Correct |
1614 ms |
20916 KB |
Output is correct |
85 |
Correct |
1686 ms |
20968 KB |
Output is correct |
86 |
Correct |
1315 ms |
19192 KB |
Output is correct |
87 |
Correct |
1373 ms |
22104 KB |
Output is correct |
88 |
Correct |
1401 ms |
18752 KB |
Output is correct |
89 |
Correct |
1449 ms |
21048 KB |
Output is correct |
90 |
Correct |
1509 ms |
21188 KB |
Output is correct |
91 |
Correct |
1419 ms |
20156 KB |
Output is correct |
92 |
Correct |
1746 ms |
21116 KB |
Output is correct |
93 |
Correct |
1652 ms |
20952 KB |
Output is correct |
94 |
Correct |
1323 ms |
19120 KB |
Output is correct |
95 |
Correct |
1652 ms |
32312 KB |
Output is correct |
96 |
Correct |
1742 ms |
31984 KB |
Output is correct |
97 |
Correct |
1717 ms |
32028 KB |
Output is correct |
98 |
Correct |
1669 ms |
32088 KB |
Output is correct |
99 |
Correct |
1733 ms |
32048 KB |
Output is correct |
100 |
Correct |
1638 ms |
31940 KB |
Output is correct |