#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 20;
int str[1<<N];
int sub[1<<N];
int over[1<<N];
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int n, qrs;
cin >> n >> qrs;
string td;
cin >> td;
for(int i=0; i<(1<<n); i++){
str[i] = td[i] - '0';
sub[i] = over[i] = str[i];
}
for(int i=0; i<n; i++){
for(int mask=0; mask<(1<<n); mask++){
if(mask & (1 << i)) sub[mask] += sub[mask ^ (1 << i)];
}
}
for(int i=0; i<n; i++){
for(int mask=0; mask<(1<<n); mask++){
if(!(mask & (1 << i))) over[mask] += over[mask ^ (1 << i)];
}
}
while(qrs--){
string s;
cin >> s;
reverse(s.begin(), s.end());
int cp = 0, c1 = 0, c0 = 0;
for(auto c : s){
if(c == '?') cp++;
else if(c == '0') c0++;
else c1++;
}
int mn = min(min(c0, c1), cp);
if(cp == mn){
vector <int> pos;
int p = 0;
int res = 0;
for(int i=0; i<n; i++){
char c = s[i];
if(c == '?') pos.push_back(1 << i);
else if(c == '1') p += (1 << i);
}
if(cp == 0){
cout << str[p] << "\n";
continue;
}
for(int mask=0; mask<(1<<pos.size()); mask++){
int g = p;
for(int j=0; j<pos.size(); j++) if((1 << j) & mask) g += pos[j];
res += str[g];
}
cout << res << "\n";
}
else if(c0 == mn){
vector <int> pos;
int p = 0;
for(int i=0; i<n; i++){
char c = s[i];
if(c == '0') pos.push_back(1 << i);
else if(c == '1') p += (1 << i);
}
if(c0 == 0){
cout << over[p] << "\n";
continue;
}
int res = 0;
for(int mask=0; mask<(1<<pos.size()); mask++){
int g = p, pr = 1;
for(int j=0; j<pos.size(); j++){
if((1 << j) & mask){
g += pos[j];
pr = -pr;
}
}
res += pr*over[g];
}
cout << res << "\n";
}
else{
vector <int> pos;
int p = 0;
for(int i=0; i<n; i++){
char c = s[i];
if(c == '1') pos.push_back(1 << i);
else if(c == '?') p += (1 << i);
}
if(c1 == 0){
cout << sub[p] << "\n";
continue;
}
int res = 0;
for(int mask=0; mask<(1<<pos.size()); mask++){
int g = p, pr = 1;
for(int j=0; j<pos.size(); j++){
if(!((1 << j) & mask)){
pr = -pr;
}
else g += pos[j];
}
res += pr*sub[g];
}
cout << res << "\n";
}
}
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:61:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int j=0; j<pos.size(); j++) if((1 << j) & mask) g += pos[j];
| ~^~~~~~~~~~~
snake_escaping.cpp:81:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for(int j=0; j<pos.size(); j++){
| ~^~~~~~~~~~~
snake_escaping.cpp:106:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for(int j=0; j<pos.size(); j++){
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
266 ms |
15032 KB |
Output is correct |
12 |
Correct |
281 ms |
14760 KB |
Output is correct |
13 |
Correct |
371 ms |
14000 KB |
Output is correct |
14 |
Correct |
450 ms |
14064 KB |
Output is correct |
15 |
Correct |
348 ms |
15300 KB |
Output is correct |
16 |
Correct |
442 ms |
14364 KB |
Output is correct |
17 |
Correct |
416 ms |
14208 KB |
Output is correct |
18 |
Correct |
236 ms |
16016 KB |
Output is correct |
19 |
Correct |
264 ms |
13044 KB |
Output is correct |
20 |
Correct |
280 ms |
14660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
266 ms |
15032 KB |
Output is correct |
12 |
Correct |
281 ms |
14760 KB |
Output is correct |
13 |
Correct |
371 ms |
14000 KB |
Output is correct |
14 |
Correct |
450 ms |
14064 KB |
Output is correct |
15 |
Correct |
348 ms |
15300 KB |
Output is correct |
16 |
Correct |
442 ms |
14364 KB |
Output is correct |
17 |
Correct |
416 ms |
14208 KB |
Output is correct |
18 |
Correct |
236 ms |
16016 KB |
Output is correct |
19 |
Correct |
264 ms |
13044 KB |
Output is correct |
20 |
Correct |
280 ms |
14660 KB |
Output is correct |
21 |
Correct |
298 ms |
18128 KB |
Output is correct |
22 |
Correct |
314 ms |
18244 KB |
Output is correct |
23 |
Correct |
480 ms |
17260 KB |
Output is correct |
24 |
Correct |
582 ms |
17152 KB |
Output is correct |
25 |
Correct |
416 ms |
19144 KB |
Output is correct |
26 |
Correct |
558 ms |
17724 KB |
Output is correct |
27 |
Correct |
526 ms |
17584 KB |
Output is correct |
28 |
Correct |
251 ms |
20164 KB |
Output is correct |
29 |
Correct |
299 ms |
16148 KB |
Output is correct |
30 |
Correct |
308 ms |
18268 KB |
Output is correct |
31 |
Correct |
490 ms |
18108 KB |
Output is correct |
32 |
Correct |
573 ms |
18160 KB |
Output is correct |
33 |
Correct |
390 ms |
16968 KB |
Output is correct |
34 |
Correct |
556 ms |
17232 KB |
Output is correct |
35 |
Correct |
527 ms |
17604 KB |
Output is correct |
36 |
Correct |
237 ms |
16112 KB |
Output is correct |
37 |
Correct |
296 ms |
18276 KB |
Output is correct |
38 |
Correct |
306 ms |
16128 KB |
Output is correct |
39 |
Correct |
478 ms |
17344 KB |
Output is correct |
40 |
Correct |
569 ms |
17228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
83 ms |
15968 KB |
Output is correct |
12 |
Correct |
87 ms |
16076 KB |
Output is correct |
13 |
Correct |
106 ms |
15928 KB |
Output is correct |
14 |
Correct |
132 ms |
15976 KB |
Output is correct |
15 |
Correct |
92 ms |
16028 KB |
Output is correct |
16 |
Correct |
148 ms |
15940 KB |
Output is correct |
17 |
Correct |
141 ms |
15936 KB |
Output is correct |
18 |
Correct |
77 ms |
16188 KB |
Output is correct |
19 |
Correct |
83 ms |
15812 KB |
Output is correct |
20 |
Correct |
84 ms |
16068 KB |
Output is correct |
21 |
Correct |
107 ms |
16060 KB |
Output is correct |
22 |
Correct |
134 ms |
16084 KB |
Output is correct |
23 |
Correct |
96 ms |
15972 KB |
Output is correct |
24 |
Correct |
139 ms |
15944 KB |
Output is correct |
25 |
Correct |
137 ms |
15944 KB |
Output is correct |
26 |
Correct |
75 ms |
15844 KB |
Output is correct |
27 |
Correct |
84 ms |
16076 KB |
Output is correct |
28 |
Correct |
88 ms |
15816 KB |
Output is correct |
29 |
Correct |
105 ms |
15948 KB |
Output is correct |
30 |
Correct |
137 ms |
15972 KB |
Output is correct |
31 |
Correct |
91 ms |
15960 KB |
Output is correct |
32 |
Correct |
155 ms |
15948 KB |
Output is correct |
33 |
Correct |
135 ms |
15948 KB |
Output is correct |
34 |
Correct |
77 ms |
15848 KB |
Output is correct |
35 |
Correct |
120 ms |
16032 KB |
Output is correct |
36 |
Correct |
123 ms |
15944 KB |
Output is correct |
37 |
Correct |
120 ms |
15964 KB |
Output is correct |
38 |
Correct |
123 ms |
15932 KB |
Output is correct |
39 |
Correct |
123 ms |
15964 KB |
Output is correct |
40 |
Correct |
127 ms |
15932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
266 ms |
15032 KB |
Output is correct |
12 |
Correct |
281 ms |
14760 KB |
Output is correct |
13 |
Correct |
371 ms |
14000 KB |
Output is correct |
14 |
Correct |
450 ms |
14064 KB |
Output is correct |
15 |
Correct |
348 ms |
15300 KB |
Output is correct |
16 |
Correct |
442 ms |
14364 KB |
Output is correct |
17 |
Correct |
416 ms |
14208 KB |
Output is correct |
18 |
Correct |
236 ms |
16016 KB |
Output is correct |
19 |
Correct |
264 ms |
13044 KB |
Output is correct |
20 |
Correct |
280 ms |
14660 KB |
Output is correct |
21 |
Correct |
298 ms |
18128 KB |
Output is correct |
22 |
Correct |
314 ms |
18244 KB |
Output is correct |
23 |
Correct |
480 ms |
17260 KB |
Output is correct |
24 |
Correct |
582 ms |
17152 KB |
Output is correct |
25 |
Correct |
416 ms |
19144 KB |
Output is correct |
26 |
Correct |
558 ms |
17724 KB |
Output is correct |
27 |
Correct |
526 ms |
17584 KB |
Output is correct |
28 |
Correct |
251 ms |
20164 KB |
Output is correct |
29 |
Correct |
299 ms |
16148 KB |
Output is correct |
30 |
Correct |
308 ms |
18268 KB |
Output is correct |
31 |
Correct |
490 ms |
18108 KB |
Output is correct |
32 |
Correct |
573 ms |
18160 KB |
Output is correct |
33 |
Correct |
390 ms |
16968 KB |
Output is correct |
34 |
Correct |
556 ms |
17232 KB |
Output is correct |
35 |
Correct |
527 ms |
17604 KB |
Output is correct |
36 |
Correct |
237 ms |
16112 KB |
Output is correct |
37 |
Correct |
296 ms |
18276 KB |
Output is correct |
38 |
Correct |
306 ms |
16128 KB |
Output is correct |
39 |
Correct |
478 ms |
17344 KB |
Output is correct |
40 |
Correct |
569 ms |
17228 KB |
Output is correct |
41 |
Correct |
83 ms |
15968 KB |
Output is correct |
42 |
Correct |
87 ms |
16076 KB |
Output is correct |
43 |
Correct |
106 ms |
15928 KB |
Output is correct |
44 |
Correct |
132 ms |
15976 KB |
Output is correct |
45 |
Correct |
92 ms |
16028 KB |
Output is correct |
46 |
Correct |
148 ms |
15940 KB |
Output is correct |
47 |
Correct |
141 ms |
15936 KB |
Output is correct |
48 |
Correct |
77 ms |
16188 KB |
Output is correct |
49 |
Correct |
83 ms |
15812 KB |
Output is correct |
50 |
Correct |
84 ms |
16068 KB |
Output is correct |
51 |
Correct |
107 ms |
16060 KB |
Output is correct |
52 |
Correct |
134 ms |
16084 KB |
Output is correct |
53 |
Correct |
96 ms |
15972 KB |
Output is correct |
54 |
Correct |
139 ms |
15944 KB |
Output is correct |
55 |
Correct |
137 ms |
15944 KB |
Output is correct |
56 |
Correct |
75 ms |
15844 KB |
Output is correct |
57 |
Correct |
84 ms |
16076 KB |
Output is correct |
58 |
Correct |
88 ms |
15816 KB |
Output is correct |
59 |
Correct |
105 ms |
15948 KB |
Output is correct |
60 |
Correct |
137 ms |
15972 KB |
Output is correct |
61 |
Correct |
91 ms |
15960 KB |
Output is correct |
62 |
Correct |
155 ms |
15948 KB |
Output is correct |
63 |
Correct |
135 ms |
15948 KB |
Output is correct |
64 |
Correct |
77 ms |
15848 KB |
Output is correct |
65 |
Correct |
120 ms |
16032 KB |
Output is correct |
66 |
Correct |
123 ms |
15944 KB |
Output is correct |
67 |
Correct |
120 ms |
15964 KB |
Output is correct |
68 |
Correct |
123 ms |
15932 KB |
Output is correct |
69 |
Correct |
123 ms |
15964 KB |
Output is correct |
70 |
Correct |
127 ms |
15932 KB |
Output is correct |
71 |
Correct |
494 ms |
40316 KB |
Output is correct |
72 |
Correct |
537 ms |
40420 KB |
Output is correct |
73 |
Correct |
1002 ms |
39044 KB |
Output is correct |
74 |
Correct |
1542 ms |
39288 KB |
Output is correct |
75 |
Correct |
725 ms |
41284 KB |
Output is correct |
76 |
Correct |
1993 ms |
39620 KB |
Output is correct |
77 |
Correct |
1575 ms |
39616 KB |
Output is correct |
78 |
Correct |
387 ms |
43288 KB |
Output is correct |
79 |
Correct |
504 ms |
37264 KB |
Output is correct |
80 |
Correct |
507 ms |
40496 KB |
Output is correct |
81 |
Correct |
950 ms |
40476 KB |
Output is correct |
82 |
Correct |
1482 ms |
39336 KB |
Output is correct |
83 |
Correct |
650 ms |
38672 KB |
Output is correct |
84 |
Correct |
1625 ms |
39388 KB |
Output is correct |
85 |
Correct |
1551 ms |
39596 KB |
Output is correct |
86 |
Correct |
367 ms |
37348 KB |
Output is correct |
87 |
Correct |
516 ms |
40372 KB |
Output is correct |
88 |
Correct |
523 ms |
37288 KB |
Output is correct |
89 |
Correct |
985 ms |
39136 KB |
Output is correct |
90 |
Correct |
1511 ms |
39532 KB |
Output is correct |
91 |
Correct |
664 ms |
38496 KB |
Output is correct |
92 |
Correct |
1962 ms |
39864 KB |
Output is correct |
93 |
Correct |
1582 ms |
39520 KB |
Output is correct |
94 |
Correct |
388 ms |
37260 KB |
Output is correct |
95 |
Correct |
1248 ms |
39416 KB |
Output is correct |
96 |
Correct |
1231 ms |
39444 KB |
Output is correct |
97 |
Correct |
1247 ms |
39320 KB |
Output is correct |
98 |
Correct |
1272 ms |
39436 KB |
Output is correct |
99 |
Correct |
1279 ms |
39276 KB |
Output is correct |
100 |
Correct |
1268 ms |
39288 KB |
Output is correct |