#include<iostream>
#include<string>
using namespace std;
#include<map>
#include<string>
#include<vector>
#define maxp 1048580
using namespace std;
string str;
int st[maxp],n,p,res,zer[maxp], on[maxp];
int ab(int x) { return x > 0 ? x : -x; }
void init(int N, int Q, string S) {
n = N; p = 1 << n; str = S;
for (int i = 0; i < p; i++) zer[i] = on[i] = st[i] = str[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];
}
}
}
void rec1(vector<int>& o, int i, int c, int a, int b) {
if (i == o.size()) {res += (a^zer[c]) + b;return;}
rec1(o, i + 1, c, a, b);
rec1(o, i + 1, c ^ o[i], ~a, 1-b);
}
void rec0(vector<int>& z, int i, int c, int a, int b) {
if (i == z.size()) {res += (a^on[c]) + b;return;}
rec0(z, i + 1, c, a, b);
rec0(z, i + 1, c ^ z[i], ~a, 1-b);
}
void recq(vector<int>& q, int i, int c) {
if (i == q.size()) { res += st[c]; return; }
recq(q, i + 1, c);
recq(q, i + 1, c ^ q[i]);
}
int query(string A) {
int zc = 0, oc = 0, qc = 0;
for (int i = 0; i < n; i++) {
if (A[i] == '0') zc++;
else if (A[i] == '1') oc++;
else qc++;
}
if (oc < zc && oc < qc) {
int c = 0;
vector<int> o;
for (int i = 0; i < n; i++) {
if (A[i] == '1') o.push_back(1 << (n - 1 - i));
else if(A[i]=='?') c += (1 << (n - 1 - i));
}
res = 0;
rec1(o, 0, c, 0, 0);
return ab(res);
}
else if (qc > zc) {
int c = 0;
vector<int> z;
for (int i = 0; i < n; i++) {
if (A[i] == '1') c += (1 << (n - 1 - i));
else if (A[i] == '0') z.push_back(1 << (n - 1 - i));
}
res = 0;
rec0(z, 0, c, 0, 0);
return ab(res);
}
else {
res = 0;
int c = 0;
vector<int> q;
for (int i = 0; i < n; i++) {
if (A[i] == '1') c += (1 << (n - 1 - i));
else if (A[i] == '?') q.push_back(1 << (n - 1 - i));
}
recq(q, 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;
}
}
Compilation message
snake_escaping.cpp: In function 'void rec1(std::vector<int>&, int, int, int, int)':
snake_escaping.cpp:24:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | if (i == o.size()) {res += (a^zer[c]) + b;return;}
| ~~^~~~~~~~~~~
snake_escaping.cpp: In function 'void rec0(std::vector<int>&, int, int, int, int)':
snake_escaping.cpp:29:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | if (i == z.size()) {res += (a^on[c]) + b;return;}
| ~~^~~~~~~~~~~
snake_escaping.cpp: In function 'void recq(std::vector<int>&, int, int)':
snake_escaping.cpp:34:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | if (i == q.size()) { res += st[c]; return; }
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
4 ms |
4564 KB |
Output is correct |
3 |
Correct |
3 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
3 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
3 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
4 ms |
4564 KB |
Output is correct |
3 |
Correct |
3 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
3 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
3 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
1236 ms |
8584 KB |
Output is correct |
12 |
Correct |
1331 ms |
8236 KB |
Output is correct |
13 |
Correct |
1391 ms |
7296 KB |
Output is correct |
14 |
Correct |
1445 ms |
7496 KB |
Output is correct |
15 |
Correct |
1370 ms |
8368 KB |
Output is correct |
16 |
Correct |
1453 ms |
8016 KB |
Output is correct |
17 |
Correct |
1505 ms |
7668 KB |
Output is correct |
18 |
Correct |
1240 ms |
9544 KB |
Output is correct |
19 |
Correct |
1247 ms |
6420 KB |
Output is correct |
20 |
Correct |
1289 ms |
8192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
4 ms |
4564 KB |
Output is correct |
3 |
Correct |
3 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
3 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
3 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
1236 ms |
8584 KB |
Output is correct |
12 |
Correct |
1331 ms |
8236 KB |
Output is correct |
13 |
Correct |
1391 ms |
7296 KB |
Output is correct |
14 |
Correct |
1445 ms |
7496 KB |
Output is correct |
15 |
Correct |
1370 ms |
8368 KB |
Output is correct |
16 |
Correct |
1453 ms |
8016 KB |
Output is correct |
17 |
Correct |
1505 ms |
7668 KB |
Output is correct |
18 |
Correct |
1240 ms |
9544 KB |
Output is correct |
19 |
Correct |
1247 ms |
6420 KB |
Output is correct |
20 |
Correct |
1289 ms |
8192 KB |
Output is correct |
21 |
Correct |
1327 ms |
8592 KB |
Output is correct |
22 |
Correct |
1336 ms |
8784 KB |
Output is correct |
23 |
Correct |
1517 ms |
7732 KB |
Output is correct |
24 |
Correct |
1532 ms |
7824 KB |
Output is correct |
25 |
Correct |
1426 ms |
9816 KB |
Output is correct |
26 |
Correct |
1507 ms |
8044 KB |
Output is correct |
27 |
Correct |
1554 ms |
8312 KB |
Output is correct |
28 |
Correct |
1216 ms |
10768 KB |
Output is correct |
29 |
Correct |
1330 ms |
6760 KB |
Output is correct |
30 |
Correct |
1327 ms |
8576 KB |
Output is correct |
31 |
Correct |
1461 ms |
8600 KB |
Output is correct |
32 |
Correct |
1570 ms |
8552 KB |
Output is correct |
33 |
Correct |
1424 ms |
7504 KB |
Output is correct |
34 |
Correct |
1548 ms |
7588 KB |
Output is correct |
35 |
Correct |
1493 ms |
8068 KB |
Output is correct |
36 |
Correct |
1300 ms |
6496 KB |
Output is correct |
37 |
Correct |
1297 ms |
8788 KB |
Output is correct |
38 |
Correct |
1316 ms |
6392 KB |
Output is correct |
39 |
Correct |
1506 ms |
8048 KB |
Output is correct |
40 |
Correct |
1528 ms |
7972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
4 ms |
4564 KB |
Output is correct |
3 |
Correct |
3 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
3 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
3 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
95 ms |
14848 KB |
Output is correct |
12 |
Correct |
97 ms |
14844 KB |
Output is correct |
13 |
Correct |
105 ms |
14844 KB |
Output is correct |
14 |
Correct |
121 ms |
14848 KB |
Output is correct |
15 |
Correct |
100 ms |
14844 KB |
Output is correct |
16 |
Correct |
120 ms |
14844 KB |
Output is correct |
17 |
Correct |
120 ms |
14844 KB |
Output is correct |
18 |
Correct |
87 ms |
15048 KB |
Output is correct |
19 |
Correct |
96 ms |
14848 KB |
Output is correct |
20 |
Correct |
97 ms |
14880 KB |
Output is correct |
21 |
Correct |
110 ms |
14848 KB |
Output is correct |
22 |
Correct |
155 ms |
14848 KB |
Output is correct |
23 |
Correct |
102 ms |
14848 KB |
Output is correct |
24 |
Correct |
126 ms |
14848 KB |
Output is correct |
25 |
Correct |
117 ms |
14848 KB |
Output is correct |
26 |
Correct |
87 ms |
14844 KB |
Output is correct |
27 |
Correct |
118 ms |
14848 KB |
Output is correct |
28 |
Correct |
99 ms |
14844 KB |
Output is correct |
29 |
Correct |
107 ms |
14848 KB |
Output is correct |
30 |
Correct |
116 ms |
14848 KB |
Output is correct |
31 |
Correct |
104 ms |
14848 KB |
Output is correct |
32 |
Correct |
124 ms |
14848 KB |
Output is correct |
33 |
Correct |
128 ms |
14844 KB |
Output is correct |
34 |
Correct |
86 ms |
14848 KB |
Output is correct |
35 |
Correct |
108 ms |
14844 KB |
Output is correct |
36 |
Correct |
109 ms |
14844 KB |
Output is correct |
37 |
Correct |
116 ms |
14844 KB |
Output is correct |
38 |
Correct |
108 ms |
15044 KB |
Output is correct |
39 |
Correct |
112 ms |
14844 KB |
Output is correct |
40 |
Correct |
112 ms |
14844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
4 ms |
4564 KB |
Output is correct |
3 |
Correct |
3 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
3 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
3 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
1236 ms |
8584 KB |
Output is correct |
12 |
Correct |
1331 ms |
8236 KB |
Output is correct |
13 |
Correct |
1391 ms |
7296 KB |
Output is correct |
14 |
Correct |
1445 ms |
7496 KB |
Output is correct |
15 |
Correct |
1370 ms |
8368 KB |
Output is correct |
16 |
Correct |
1453 ms |
8016 KB |
Output is correct |
17 |
Correct |
1505 ms |
7668 KB |
Output is correct |
18 |
Correct |
1240 ms |
9544 KB |
Output is correct |
19 |
Correct |
1247 ms |
6420 KB |
Output is correct |
20 |
Correct |
1289 ms |
8192 KB |
Output is correct |
21 |
Correct |
1327 ms |
8592 KB |
Output is correct |
22 |
Correct |
1336 ms |
8784 KB |
Output is correct |
23 |
Correct |
1517 ms |
7732 KB |
Output is correct |
24 |
Correct |
1532 ms |
7824 KB |
Output is correct |
25 |
Correct |
1426 ms |
9816 KB |
Output is correct |
26 |
Correct |
1507 ms |
8044 KB |
Output is correct |
27 |
Correct |
1554 ms |
8312 KB |
Output is correct |
28 |
Correct |
1216 ms |
10768 KB |
Output is correct |
29 |
Correct |
1330 ms |
6760 KB |
Output is correct |
30 |
Correct |
1327 ms |
8576 KB |
Output is correct |
31 |
Correct |
1461 ms |
8600 KB |
Output is correct |
32 |
Correct |
1570 ms |
8552 KB |
Output is correct |
33 |
Correct |
1424 ms |
7504 KB |
Output is correct |
34 |
Correct |
1548 ms |
7588 KB |
Output is correct |
35 |
Correct |
1493 ms |
8068 KB |
Output is correct |
36 |
Correct |
1300 ms |
6496 KB |
Output is correct |
37 |
Correct |
1297 ms |
8788 KB |
Output is correct |
38 |
Correct |
1316 ms |
6392 KB |
Output is correct |
39 |
Correct |
1506 ms |
8048 KB |
Output is correct |
40 |
Correct |
1528 ms |
7972 KB |
Output is correct |
41 |
Correct |
95 ms |
14848 KB |
Output is correct |
42 |
Correct |
97 ms |
14844 KB |
Output is correct |
43 |
Correct |
105 ms |
14844 KB |
Output is correct |
44 |
Correct |
121 ms |
14848 KB |
Output is correct |
45 |
Correct |
100 ms |
14844 KB |
Output is correct |
46 |
Correct |
120 ms |
14844 KB |
Output is correct |
47 |
Correct |
120 ms |
14844 KB |
Output is correct |
48 |
Correct |
87 ms |
15048 KB |
Output is correct |
49 |
Correct |
96 ms |
14848 KB |
Output is correct |
50 |
Correct |
97 ms |
14880 KB |
Output is correct |
51 |
Correct |
110 ms |
14848 KB |
Output is correct |
52 |
Correct |
155 ms |
14848 KB |
Output is correct |
53 |
Correct |
102 ms |
14848 KB |
Output is correct |
54 |
Correct |
126 ms |
14848 KB |
Output is correct |
55 |
Correct |
117 ms |
14848 KB |
Output is correct |
56 |
Correct |
87 ms |
14844 KB |
Output is correct |
57 |
Correct |
118 ms |
14848 KB |
Output is correct |
58 |
Correct |
99 ms |
14844 KB |
Output is correct |
59 |
Correct |
107 ms |
14848 KB |
Output is correct |
60 |
Correct |
116 ms |
14848 KB |
Output is correct |
61 |
Correct |
104 ms |
14848 KB |
Output is correct |
62 |
Correct |
124 ms |
14848 KB |
Output is correct |
63 |
Correct |
128 ms |
14844 KB |
Output is correct |
64 |
Correct |
86 ms |
14848 KB |
Output is correct |
65 |
Correct |
108 ms |
14844 KB |
Output is correct |
66 |
Correct |
109 ms |
14844 KB |
Output is correct |
67 |
Correct |
116 ms |
14844 KB |
Output is correct |
68 |
Correct |
108 ms |
15044 KB |
Output is correct |
69 |
Correct |
112 ms |
14844 KB |
Output is correct |
70 |
Correct |
112 ms |
14844 KB |
Output is correct |
71 |
Correct |
1494 ms |
18988 KB |
Output is correct |
72 |
Correct |
1505 ms |
19220 KB |
Output is correct |
73 |
Correct |
1817 ms |
17624 KB |
Output is correct |
74 |
Execution timed out |
2020 ms |
18096 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |