#include<iostream>
#include<string>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
#define maxp 1048580
using namespace std;
int st[maxp],n,p,res,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 o[20], z[20], q[20];
int oc, zc, qc;
void rec1(int i, int c, int a) {
if (i == oc) {res += (a^zer[c]) + (a & 1);return;}
rec1(i + 1, c, a);
rec1(i + 1, c ^ o[i], ~a);
}
void rec0(int i, int c, int a) {
if (i == zc) {res += (a^on[c]) + (a&1); return; }
rec0(i + 1, c, a);
rec0(i + 1, c ^ z[i], ~a);
}
void recq(int i, int c) {
if (i == qc) { res += st[c]; return; }
recq(i + 1, c);
recq(i + 1, c ^ q[i]);
}
int query(string A) {
oc = zc = qc = 0;
int a = 0, b = 0, q = 0;
for (int i = 0; i < n; i++) {
if (A[i] == '0') zc++, a+=1<<(n-1-i);
else if (A[i] == '1') oc++, b += 1 << (n - 1 - i);
else qc++, q += 1 << (n - 1 - i);
}
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];
}
/*int c = 0;
int po = 0;
for (int i = 0; i < n; i++) {
if (A[i] == '1') o[po++] = (1 << (n - 1 - i));
else if(A[i]=='?') c += (1 << (n - 1 - i));
}
rec1(0, c, 0);*/
return 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];
}
/*int c = 0;
int pz = 0;
for (int i = 0; i < n; i++) {
if (A[i] == '1') c += (1 << (n - 1 - i));
else if (A[i] == '0') z[pz++]=(1 << (n - 1 - i));
}
res = 0;
rec0(0, c, 0);*/
return ab(res);
}
else {
res = 0;
for (int i = q; i >= 0; i--) {
i &= q;
res += st[i | b];
}
/*res = 0;
int c = 0;
int pq = 0;
for (int i = 0; i < n; i++) {
if (A[i] == '1') c += (1 << (n - 1 - i));
else if (A[i] == '?') q[pq++]=(1 << (n - 1 - i));
}
recq(0, c);*/
return res;
}
return 0;
}
/*
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 |
3 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 |
6488 KB |
Output is correct |
5 |
Correct |
3 ms |
6744 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
3 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6612 KB |
Output is correct |
9 |
Correct |
3 ms |
6488 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
6488 KB |
Output is correct |
5 |
Correct |
3 ms |
6744 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
3 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6612 KB |
Output is correct |
9 |
Correct |
3 ms |
6488 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
1260 ms |
18860 KB |
Output is correct |
12 |
Correct |
1258 ms |
18676 KB |
Output is correct |
13 |
Correct |
1284 ms |
17904 KB |
Output is correct |
14 |
Correct |
1296 ms |
18260 KB |
Output is correct |
15 |
Correct |
1283 ms |
19168 KB |
Output is correct |
16 |
Correct |
1276 ms |
18116 KB |
Output is correct |
17 |
Correct |
1278 ms |
18160 KB |
Output is correct |
18 |
Correct |
1220 ms |
19956 KB |
Output is correct |
19 |
Correct |
1267 ms |
16976 KB |
Output is correct |
20 |
Correct |
1252 ms |
18948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
6488 KB |
Output is correct |
5 |
Correct |
3 ms |
6744 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
3 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6612 KB |
Output is correct |
9 |
Correct |
3 ms |
6488 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
1260 ms |
18860 KB |
Output is correct |
12 |
Correct |
1258 ms |
18676 KB |
Output is correct |
13 |
Correct |
1284 ms |
17904 KB |
Output is correct |
14 |
Correct |
1296 ms |
18260 KB |
Output is correct |
15 |
Correct |
1283 ms |
19168 KB |
Output is correct |
16 |
Correct |
1276 ms |
18116 KB |
Output is correct |
17 |
Correct |
1278 ms |
18160 KB |
Output is correct |
18 |
Correct |
1220 ms |
19956 KB |
Output is correct |
19 |
Correct |
1267 ms |
16976 KB |
Output is correct |
20 |
Correct |
1252 ms |
18948 KB |
Output is correct |
21 |
Correct |
1293 ms |
21236 KB |
Output is correct |
22 |
Correct |
1300 ms |
21440 KB |
Output is correct |
23 |
Correct |
1323 ms |
20612 KB |
Output is correct |
24 |
Correct |
1334 ms |
20248 KB |
Output is correct |
25 |
Correct |
1262 ms |
22580 KB |
Output is correct |
26 |
Correct |
1349 ms |
20772 KB |
Output is correct |
27 |
Correct |
1308 ms |
20764 KB |
Output is correct |
28 |
Correct |
1236 ms |
23432 KB |
Output is correct |
29 |
Correct |
1273 ms |
19584 KB |
Output is correct |
30 |
Correct |
1378 ms |
21456 KB |
Output is correct |
31 |
Correct |
1357 ms |
21776 KB |
Output is correct |
32 |
Correct |
1316 ms |
21224 KB |
Output is correct |
33 |
Correct |
1264 ms |
20636 KB |
Output is correct |
34 |
Correct |
1307 ms |
20480 KB |
Output is correct |
35 |
Correct |
1324 ms |
20796 KB |
Output is correct |
36 |
Correct |
1237 ms |
19336 KB |
Output is correct |
37 |
Correct |
1261 ms |
21408 KB |
Output is correct |
38 |
Correct |
1292 ms |
19160 KB |
Output is correct |
39 |
Correct |
1296 ms |
20504 KB |
Output is correct |
40 |
Correct |
1333 ms |
20424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
6488 KB |
Output is correct |
5 |
Correct |
3 ms |
6744 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
3 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6612 KB |
Output is correct |
9 |
Correct |
3 ms |
6488 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
92 ms |
19112 KB |
Output is correct |
12 |
Correct |
94 ms |
19256 KB |
Output is correct |
13 |
Correct |
116 ms |
19188 KB |
Output is correct |
14 |
Correct |
98 ms |
19192 KB |
Output is correct |
15 |
Correct |
97 ms |
19272 KB |
Output is correct |
16 |
Correct |
107 ms |
19192 KB |
Output is correct |
17 |
Correct |
134 ms |
19184 KB |
Output is correct |
18 |
Correct |
88 ms |
19192 KB |
Output is correct |
19 |
Correct |
90 ms |
19124 KB |
Output is correct |
20 |
Correct |
92 ms |
19372 KB |
Output is correct |
21 |
Correct |
108 ms |
19100 KB |
Output is correct |
22 |
Correct |
100 ms |
19168 KB |
Output is correct |
23 |
Correct |
99 ms |
19184 KB |
Output is correct |
24 |
Correct |
110 ms |
19192 KB |
Output is correct |
25 |
Correct |
110 ms |
19188 KB |
Output is correct |
26 |
Correct |
85 ms |
19188 KB |
Output is correct |
27 |
Correct |
93 ms |
19260 KB |
Output is correct |
28 |
Correct |
96 ms |
19184 KB |
Output is correct |
29 |
Correct |
96 ms |
19200 KB |
Output is correct |
30 |
Correct |
97 ms |
19124 KB |
Output is correct |
31 |
Correct |
95 ms |
19116 KB |
Output is correct |
32 |
Correct |
118 ms |
19344 KB |
Output is correct |
33 |
Correct |
108 ms |
19228 KB |
Output is correct |
34 |
Correct |
91 ms |
19184 KB |
Output is correct |
35 |
Correct |
99 ms |
19188 KB |
Output is correct |
36 |
Correct |
102 ms |
19424 KB |
Output is correct |
37 |
Correct |
98 ms |
19200 KB |
Output is correct |
38 |
Correct |
98 ms |
19184 KB |
Output is correct |
39 |
Correct |
98 ms |
19192 KB |
Output is correct |
40 |
Correct |
100 ms |
19188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
6488 KB |
Output is correct |
5 |
Correct |
3 ms |
6744 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
3 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6612 KB |
Output is correct |
9 |
Correct |
3 ms |
6488 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
1260 ms |
18860 KB |
Output is correct |
12 |
Correct |
1258 ms |
18676 KB |
Output is correct |
13 |
Correct |
1284 ms |
17904 KB |
Output is correct |
14 |
Correct |
1296 ms |
18260 KB |
Output is correct |
15 |
Correct |
1283 ms |
19168 KB |
Output is correct |
16 |
Correct |
1276 ms |
18116 KB |
Output is correct |
17 |
Correct |
1278 ms |
18160 KB |
Output is correct |
18 |
Correct |
1220 ms |
19956 KB |
Output is correct |
19 |
Correct |
1267 ms |
16976 KB |
Output is correct |
20 |
Correct |
1252 ms |
18948 KB |
Output is correct |
21 |
Correct |
1293 ms |
21236 KB |
Output is correct |
22 |
Correct |
1300 ms |
21440 KB |
Output is correct |
23 |
Correct |
1323 ms |
20612 KB |
Output is correct |
24 |
Correct |
1334 ms |
20248 KB |
Output is correct |
25 |
Correct |
1262 ms |
22580 KB |
Output is correct |
26 |
Correct |
1349 ms |
20772 KB |
Output is correct |
27 |
Correct |
1308 ms |
20764 KB |
Output is correct |
28 |
Correct |
1236 ms |
23432 KB |
Output is correct |
29 |
Correct |
1273 ms |
19584 KB |
Output is correct |
30 |
Correct |
1378 ms |
21456 KB |
Output is correct |
31 |
Correct |
1357 ms |
21776 KB |
Output is correct |
32 |
Correct |
1316 ms |
21224 KB |
Output is correct |
33 |
Correct |
1264 ms |
20636 KB |
Output is correct |
34 |
Correct |
1307 ms |
20480 KB |
Output is correct |
35 |
Correct |
1324 ms |
20796 KB |
Output is correct |
36 |
Correct |
1237 ms |
19336 KB |
Output is correct |
37 |
Correct |
1261 ms |
21408 KB |
Output is correct |
38 |
Correct |
1292 ms |
19160 KB |
Output is correct |
39 |
Correct |
1296 ms |
20504 KB |
Output is correct |
40 |
Correct |
1333 ms |
20424 KB |
Output is correct |
41 |
Correct |
92 ms |
19112 KB |
Output is correct |
42 |
Correct |
94 ms |
19256 KB |
Output is correct |
43 |
Correct |
116 ms |
19188 KB |
Output is correct |
44 |
Correct |
98 ms |
19192 KB |
Output is correct |
45 |
Correct |
97 ms |
19272 KB |
Output is correct |
46 |
Correct |
107 ms |
19192 KB |
Output is correct |
47 |
Correct |
134 ms |
19184 KB |
Output is correct |
48 |
Correct |
88 ms |
19192 KB |
Output is correct |
49 |
Correct |
90 ms |
19124 KB |
Output is correct |
50 |
Correct |
92 ms |
19372 KB |
Output is correct |
51 |
Correct |
108 ms |
19100 KB |
Output is correct |
52 |
Correct |
100 ms |
19168 KB |
Output is correct |
53 |
Correct |
99 ms |
19184 KB |
Output is correct |
54 |
Correct |
110 ms |
19192 KB |
Output is correct |
55 |
Correct |
110 ms |
19188 KB |
Output is correct |
56 |
Correct |
85 ms |
19188 KB |
Output is correct |
57 |
Correct |
93 ms |
19260 KB |
Output is correct |
58 |
Correct |
96 ms |
19184 KB |
Output is correct |
59 |
Correct |
96 ms |
19200 KB |
Output is correct |
60 |
Correct |
97 ms |
19124 KB |
Output is correct |
61 |
Correct |
95 ms |
19116 KB |
Output is correct |
62 |
Correct |
118 ms |
19344 KB |
Output is correct |
63 |
Correct |
108 ms |
19228 KB |
Output is correct |
64 |
Correct |
91 ms |
19184 KB |
Output is correct |
65 |
Correct |
99 ms |
19188 KB |
Output is correct |
66 |
Correct |
102 ms |
19424 KB |
Output is correct |
67 |
Correct |
98 ms |
19200 KB |
Output is correct |
68 |
Correct |
98 ms |
19184 KB |
Output is correct |
69 |
Correct |
98 ms |
19192 KB |
Output is correct |
70 |
Correct |
100 ms |
19188 KB |
Output is correct |
71 |
Correct |
1453 ms |
38368 KB |
Output is correct |
72 |
Correct |
1535 ms |
38500 KB |
Output is correct |
73 |
Correct |
1496 ms |
37248 KB |
Output is correct |
74 |
Correct |
1798 ms |
37376 KB |
Output is correct |
75 |
Correct |
1447 ms |
40088 KB |
Output is correct |
76 |
Correct |
1781 ms |
32216 KB |
Output is correct |
77 |
Correct |
1676 ms |
42684 KB |
Output is correct |
78 |
Correct |
1342 ms |
46504 KB |
Output is correct |
79 |
Correct |
1391 ms |
40284 KB |
Output is correct |
80 |
Correct |
1462 ms |
43512 KB |
Output is correct |
81 |
Correct |
1516 ms |
43404 KB |
Output is correct |
82 |
Correct |
1573 ms |
42488 KB |
Output is correct |
83 |
Correct |
1443 ms |
41436 KB |
Output is correct |
84 |
Correct |
1667 ms |
42668 KB |
Output is correct |
85 |
Correct |
1663 ms |
42800 KB |
Output is correct |
86 |
Correct |
1318 ms |
40504 KB |
Output is correct |
87 |
Correct |
1403 ms |
43740 KB |
Output is correct |
88 |
Correct |
1421 ms |
40300 KB |
Output is correct |
89 |
Correct |
1478 ms |
42280 KB |
Output is correct |
90 |
Correct |
1556 ms |
42476 KB |
Output is correct |
91 |
Correct |
1434 ms |
41520 KB |
Output is correct |
92 |
Correct |
1774 ms |
42588 KB |
Output is correct |
93 |
Correct |
1649 ms |
42672 KB |
Output is correct |
94 |
Correct |
1284 ms |
40448 KB |
Output is correct |
95 |
Correct |
1661 ms |
42572 KB |
Output is correct |
96 |
Correct |
1627 ms |
42472 KB |
Output is correct |
97 |
Correct |
1641 ms |
42380 KB |
Output is correct |
98 |
Correct |
1636 ms |
42736 KB |
Output is correct |
99 |
Correct |
1607 ms |
42724 KB |
Output is correct |
100 |
Correct |
1545 ms |
42408 KB |
Output is correct |