#include<iostream>
#include<string>
using namespace std;
#include<map>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
#define all(x) x.begin(),x.end()
string str;
int n,p;
vector<int> zer, on;//sum of those i contains, sum of those contains me i
void init(int N, int Q, string S) {
n = N; p = 1 << n;
str = S;
zer.resize(p); on.resize(p);
for (int i = 0; i < p; i++) zer[i] = on[i] = S[i] - '0';
for (int i = 0; i < n; i++) {
for (int j = 0; j < p; j++) {
if (j & (1 << i)) zer[j] += zer[j - (1 << i)];
else on[j] += on[j + (1 << i)];
}
}
//for (int i = 0; i < p; i++) cout << i << ' ' << zer[i] << endl;
//for (int i = 0; i < p; i++) cout << i << ' ' << on[i] << endl;
}
int ab(int x) { return x > 0 ? x : -x; }
int res;
void rec1(vector<int>& o, int i, int c, int x) {
if (i == o.size()) {
if (x & 1) res += zer[c];
else res -= zer[c];
return;
}
rec1(o, i + 1, c, x);
rec1(o, i + 1, c + o[i], x+1);
}
void rec0(vector<int>& z, int i, int c, int x) {
if (i == z.size()) {
if (x & 1) res += on[c];
else res -= on[c];
return;
}
rec0(z, i + 1, c, x);
rec0(z, i + 1, c + z[i], x + 1);
}
int query(string A) {
vector<int> ze, o, q;
for (int i = 0; i < n; i++) {
if (A[i] == '0') ze.push_back(1 << (n-1-i));
else if (A[i] == '1') o.push_back(1 << (n-1-i));
else q.push_back(1 << (n-1-i));
}
/*
3 5
12345678
000
0??
1?0
?11
???
*/
if (ze.size() > o.size()) {
int c = 0;
for (int i : q) c += i;
res = 0;
rec1(o, 0, c, 0);
/*for (int i = 0; i < (1 << o.size()); i++) {
int w = c, x=0;
for (int j = 0; j < o.size(); j++) {
if (i & (1 << j)) {
w += o[j];
x++;
}
}
if (x & 1) res += zer[w];
else res -= zer[w];
}*/
return ab(res);
}
else {
int c = 0;
for (int i : o) c += i;
res = 0;
rec0(ze, 0, c, 0);
/*for (int i = 0; i < (1 << ze.size()); i++) {
int w = c, x = 0;
for (int j = 0; j < ze.size(); j++) {
if (i & (1 << j)) {
w += ze[j];
x++;
}
}
if (x & 1) res += on[w];
else res -= on[w];
}*/
return ab(res);
}
return 0;
}
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;
}
}
Compilation message
snake_escaping.cpp: In function 'void rec1(std::vector<int>&, int, int, int)':
snake_escaping.cpp:31:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | if (i == o.size()) {
| ~~^~~~~~~~~~~
snake_escaping.cpp: In function 'void rec0(std::vector<int>&, int, int, int)':
snake_escaping.cpp:41:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | if (i == z.size()) {
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1485 ms |
4312 KB |
Output is correct |
12 |
Correct |
1521 ms |
4044 KB |
Output is correct |
13 |
Correct |
1542 ms |
3256 KB |
Output is correct |
14 |
Correct |
1594 ms |
3512 KB |
Output is correct |
15 |
Correct |
1666 ms |
4412 KB |
Output is correct |
16 |
Correct |
1671 ms |
3736 KB |
Output is correct |
17 |
Correct |
1827 ms |
3684 KB |
Output is correct |
18 |
Correct |
1431 ms |
5432 KB |
Output is correct |
19 |
Correct |
1638 ms |
2336 KB |
Output is correct |
20 |
Correct |
1540 ms |
4072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1485 ms |
4312 KB |
Output is correct |
12 |
Correct |
1521 ms |
4044 KB |
Output is correct |
13 |
Correct |
1542 ms |
3256 KB |
Output is correct |
14 |
Correct |
1594 ms |
3512 KB |
Output is correct |
15 |
Correct |
1666 ms |
4412 KB |
Output is correct |
16 |
Correct |
1671 ms |
3736 KB |
Output is correct |
17 |
Correct |
1827 ms |
3684 KB |
Output is correct |
18 |
Correct |
1431 ms |
5432 KB |
Output is correct |
19 |
Correct |
1638 ms |
2336 KB |
Output is correct |
20 |
Correct |
1540 ms |
4072 KB |
Output is correct |
21 |
Correct |
1607 ms |
4532 KB |
Output is correct |
22 |
Correct |
1581 ms |
4572 KB |
Output is correct |
23 |
Correct |
1778 ms |
3640 KB |
Output is correct |
24 |
Correct |
1755 ms |
3680 KB |
Output is correct |
25 |
Correct |
1625 ms |
5448 KB |
Output is correct |
26 |
Correct |
1657 ms |
3944 KB |
Output is correct |
27 |
Correct |
1722 ms |
3852 KB |
Output is correct |
28 |
Correct |
1392 ms |
6484 KB |
Output is correct |
29 |
Correct |
1783 ms |
2516 KB |
Output is correct |
30 |
Correct |
1638 ms |
18348 KB |
Output is correct |
31 |
Correct |
1671 ms |
18108 KB |
Output is correct |
32 |
Correct |
1692 ms |
18044 KB |
Output is correct |
33 |
Correct |
1655 ms |
17048 KB |
Output is correct |
34 |
Correct |
1677 ms |
17128 KB |
Output is correct |
35 |
Correct |
1779 ms |
17876 KB |
Output is correct |
36 |
Correct |
1424 ms |
16000 KB |
Output is correct |
37 |
Correct |
1839 ms |
17996 KB |
Output is correct |
38 |
Correct |
1738 ms |
16188 KB |
Output is correct |
39 |
Correct |
1758 ms |
17396 KB |
Output is correct |
40 |
Correct |
1778 ms |
17012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
133 ms |
10836 KB |
Output is correct |
12 |
Correct |
113 ms |
10752 KB |
Output is correct |
13 |
Correct |
143 ms |
10752 KB |
Output is correct |
14 |
Correct |
138 ms |
10752 KB |
Output is correct |
15 |
Correct |
106 ms |
10752 KB |
Output is correct |
16 |
Correct |
142 ms |
10752 KB |
Output is correct |
17 |
Correct |
138 ms |
10812 KB |
Output is correct |
18 |
Correct |
104 ms |
10752 KB |
Output is correct |
19 |
Correct |
352 ms |
10876 KB |
Output is correct |
20 |
Correct |
122 ms |
12088 KB |
Output is correct |
21 |
Correct |
115 ms |
12028 KB |
Output is correct |
22 |
Correct |
129 ms |
12032 KB |
Output is correct |
23 |
Correct |
154 ms |
12028 KB |
Output is correct |
24 |
Correct |
136 ms |
12032 KB |
Output is correct |
25 |
Correct |
154 ms |
11984 KB |
Output is correct |
26 |
Correct |
93 ms |
12088 KB |
Output is correct |
27 |
Correct |
107 ms |
12100 KB |
Output is correct |
28 |
Correct |
206 ms |
12144 KB |
Output is correct |
29 |
Correct |
137 ms |
12032 KB |
Output is correct |
30 |
Correct |
182 ms |
12032 KB |
Output is correct |
31 |
Correct |
127 ms |
12116 KB |
Output is correct |
32 |
Correct |
139 ms |
12028 KB |
Output is correct |
33 |
Correct |
133 ms |
12084 KB |
Output is correct |
34 |
Correct |
95 ms |
12184 KB |
Output is correct |
35 |
Correct |
129 ms |
12028 KB |
Output is correct |
36 |
Correct |
152 ms |
12032 KB |
Output is correct |
37 |
Correct |
179 ms |
12124 KB |
Output is correct |
38 |
Correct |
137 ms |
12032 KB |
Output is correct |
39 |
Correct |
145 ms |
12032 KB |
Output is correct |
40 |
Correct |
175 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1485 ms |
4312 KB |
Output is correct |
12 |
Correct |
1521 ms |
4044 KB |
Output is correct |
13 |
Correct |
1542 ms |
3256 KB |
Output is correct |
14 |
Correct |
1594 ms |
3512 KB |
Output is correct |
15 |
Correct |
1666 ms |
4412 KB |
Output is correct |
16 |
Correct |
1671 ms |
3736 KB |
Output is correct |
17 |
Correct |
1827 ms |
3684 KB |
Output is correct |
18 |
Correct |
1431 ms |
5432 KB |
Output is correct |
19 |
Correct |
1638 ms |
2336 KB |
Output is correct |
20 |
Correct |
1540 ms |
4072 KB |
Output is correct |
21 |
Correct |
1607 ms |
4532 KB |
Output is correct |
22 |
Correct |
1581 ms |
4572 KB |
Output is correct |
23 |
Correct |
1778 ms |
3640 KB |
Output is correct |
24 |
Correct |
1755 ms |
3680 KB |
Output is correct |
25 |
Correct |
1625 ms |
5448 KB |
Output is correct |
26 |
Correct |
1657 ms |
3944 KB |
Output is correct |
27 |
Correct |
1722 ms |
3852 KB |
Output is correct |
28 |
Correct |
1392 ms |
6484 KB |
Output is correct |
29 |
Correct |
1783 ms |
2516 KB |
Output is correct |
30 |
Correct |
1638 ms |
18348 KB |
Output is correct |
31 |
Correct |
1671 ms |
18108 KB |
Output is correct |
32 |
Correct |
1692 ms |
18044 KB |
Output is correct |
33 |
Correct |
1655 ms |
17048 KB |
Output is correct |
34 |
Correct |
1677 ms |
17128 KB |
Output is correct |
35 |
Correct |
1779 ms |
17876 KB |
Output is correct |
36 |
Correct |
1424 ms |
16000 KB |
Output is correct |
37 |
Correct |
1839 ms |
17996 KB |
Output is correct |
38 |
Correct |
1738 ms |
16188 KB |
Output is correct |
39 |
Correct |
1758 ms |
17396 KB |
Output is correct |
40 |
Correct |
1778 ms |
17012 KB |
Output is correct |
41 |
Correct |
133 ms |
10836 KB |
Output is correct |
42 |
Correct |
113 ms |
10752 KB |
Output is correct |
43 |
Correct |
143 ms |
10752 KB |
Output is correct |
44 |
Correct |
138 ms |
10752 KB |
Output is correct |
45 |
Correct |
106 ms |
10752 KB |
Output is correct |
46 |
Correct |
142 ms |
10752 KB |
Output is correct |
47 |
Correct |
138 ms |
10812 KB |
Output is correct |
48 |
Correct |
104 ms |
10752 KB |
Output is correct |
49 |
Correct |
352 ms |
10876 KB |
Output is correct |
50 |
Correct |
122 ms |
12088 KB |
Output is correct |
51 |
Correct |
115 ms |
12028 KB |
Output is correct |
52 |
Correct |
129 ms |
12032 KB |
Output is correct |
53 |
Correct |
154 ms |
12028 KB |
Output is correct |
54 |
Correct |
136 ms |
12032 KB |
Output is correct |
55 |
Correct |
154 ms |
11984 KB |
Output is correct |
56 |
Correct |
93 ms |
12088 KB |
Output is correct |
57 |
Correct |
107 ms |
12100 KB |
Output is correct |
58 |
Correct |
206 ms |
12144 KB |
Output is correct |
59 |
Correct |
137 ms |
12032 KB |
Output is correct |
60 |
Correct |
182 ms |
12032 KB |
Output is correct |
61 |
Correct |
127 ms |
12116 KB |
Output is correct |
62 |
Correct |
139 ms |
12028 KB |
Output is correct |
63 |
Correct |
133 ms |
12084 KB |
Output is correct |
64 |
Correct |
95 ms |
12184 KB |
Output is correct |
65 |
Correct |
129 ms |
12028 KB |
Output is correct |
66 |
Correct |
152 ms |
12032 KB |
Output is correct |
67 |
Correct |
179 ms |
12124 KB |
Output is correct |
68 |
Correct |
137 ms |
12032 KB |
Output is correct |
69 |
Correct |
145 ms |
12032 KB |
Output is correct |
70 |
Correct |
175 ms |
12116 KB |
Output is correct |
71 |
Correct |
1849 ms |
36596 KB |
Output is correct |
72 |
Correct |
1933 ms |
36412 KB |
Output is correct |
73 |
Execution timed out |
2059 ms |
33408 KB |
Time limit exceeded |
74 |
Halted |
0 ms |
0 KB |
- |