#include <bits/stdc++.h>
using namespace std;
const int N = 21;
int val[1<<N], ppcnt[1<<N];
int dp[1<<N][2];
//0 je za ?1, 1 je za ?0
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
string s;
cin >> s;
for(int i=0; i<s.size(); i++){
val[i]=dp[i][0]=dp[i][1]=s[i]-'0';
ppcnt[i]=__builtin_popcount(i);
}
for(int i=1; i<=n; i++){
for(int j=0; j<(1<<n); j++){
if(j & (1<<(i-1))){
dp[j][0]+=dp[j^(1<<(i-1))][0];
}
else{
dp[j][1]+=dp[j^(1<<(i-1))][1];
}
}
}
while(q--){
string qry;
cin >> qry;
int cntu=0, cnt1=0, cnt0=0;
for(char c : qry){
if(c=='?')cntu++;
else if(c=='1')cnt1++;
else cnt0++;
}
int mn = min({cntu, cnt1, cnt0});
int ans=0;
int mask = 0, mask1 = 0;
if(cntu == mn){
for(int i=0; i< n; i++){
if(qry[i]=='1')mask+=1<<(n-i-1);
else if(qry[i]=='?')mask1+=1<<(n-i-1);
}
for(int m = mask1; ; m=(m-1)&mask1){
ans+=val[mask|m];
if(!m)break;
}
cout << ans << '\n';
}
else if(cnt0 == mn){
for(int i=0; i< n; i++){
if(qry[i]=='1')mask+=1<<(n-i-1);
else if(qry[i]=='0')mask1+=1<<(n-i-1);
}
int pcnt = ppcnt[mask1|mask]&1;
for(int m = mask1; ; m=(m-1)&mask1){
if((ppcnt[m|mask]&1)^pcnt)ans-=dp[m|mask][1];
else ans+=dp[m|mask][1];
if(!m)break;
}
cout << abs(ans) << '\n';
}
else{
for(int i=0; i< n; i++){
if(qry[i]=='?')mask+=1<<(n-i-1);
else if(qry[i]=='1')mask1+=1<<(n-i-1);
}
int pcnt = ppcnt[mask1|mask]&1;
for(int m = mask1; ; m=(m-1)&mask1){
if((ppcnt[m|mask]&1)^pcnt)ans-=dp[m|mask][0];
else ans+=dp[m|mask][0];
if(!m)break;
}
cout << abs(ans) << '\n';
}
}
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:20:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for(int i=0; i<s.size(); i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4456 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4456 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
153 ms |
8392 KB |
Output is correct |
12 |
Correct |
150 ms |
8264 KB |
Output is correct |
13 |
Correct |
200 ms |
7248 KB |
Output is correct |
14 |
Correct |
162 ms |
7472 KB |
Output is correct |
15 |
Correct |
163 ms |
8500 KB |
Output is correct |
16 |
Correct |
187 ms |
7504 KB |
Output is correct |
17 |
Correct |
220 ms |
7660 KB |
Output is correct |
18 |
Correct |
112 ms |
17608 KB |
Output is correct |
19 |
Correct |
142 ms |
17220 KB |
Output is correct |
20 |
Correct |
159 ms |
18772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4456 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
153 ms |
8392 KB |
Output is correct |
12 |
Correct |
150 ms |
8264 KB |
Output is correct |
13 |
Correct |
200 ms |
7248 KB |
Output is correct |
14 |
Correct |
162 ms |
7472 KB |
Output is correct |
15 |
Correct |
163 ms |
8500 KB |
Output is correct |
16 |
Correct |
187 ms |
7504 KB |
Output is correct |
17 |
Correct |
220 ms |
7660 KB |
Output is correct |
18 |
Correct |
112 ms |
17608 KB |
Output is correct |
19 |
Correct |
142 ms |
17220 KB |
Output is correct |
20 |
Correct |
159 ms |
18772 KB |
Output is correct |
21 |
Correct |
195 ms |
22356 KB |
Output is correct |
22 |
Correct |
219 ms |
22284 KB |
Output is correct |
23 |
Correct |
245 ms |
21332 KB |
Output is correct |
24 |
Correct |
224 ms |
21328 KB |
Output is correct |
25 |
Correct |
221 ms |
23224 KB |
Output is correct |
26 |
Correct |
244 ms |
21840 KB |
Output is correct |
27 |
Correct |
263 ms |
21588 KB |
Output is correct |
28 |
Correct |
121 ms |
24296 KB |
Output is correct |
29 |
Correct |
199 ms |
20340 KB |
Output is correct |
30 |
Correct |
235 ms |
22368 KB |
Output is correct |
31 |
Correct |
250 ms |
22356 KB |
Output is correct |
32 |
Correct |
239 ms |
22340 KB |
Output is correct |
33 |
Correct |
211 ms |
21080 KB |
Output is correct |
34 |
Correct |
238 ms |
21332 KB |
Output is correct |
35 |
Correct |
252 ms |
21576 KB |
Output is correct |
36 |
Correct |
108 ms |
20120 KB |
Output is correct |
37 |
Correct |
181 ms |
22276 KB |
Output is correct |
38 |
Correct |
223 ms |
20304 KB |
Output is correct |
39 |
Correct |
244 ms |
21348 KB |
Output is correct |
40 |
Correct |
228 ms |
21332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4456 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
54 ms |
22520 KB |
Output is correct |
12 |
Correct |
36 ms |
23024 KB |
Output is correct |
13 |
Correct |
38 ms |
24308 KB |
Output is correct |
14 |
Correct |
48 ms |
24304 KB |
Output is correct |
15 |
Correct |
36 ms |
24572 KB |
Output is correct |
16 |
Correct |
44 ms |
24564 KB |
Output is correct |
17 |
Correct |
42 ms |
24556 KB |
Output is correct |
18 |
Correct |
29 ms |
24640 KB |
Output is correct |
19 |
Correct |
37 ms |
24304 KB |
Output is correct |
20 |
Correct |
37 ms |
24560 KB |
Output is correct |
21 |
Correct |
41 ms |
24376 KB |
Output is correct |
22 |
Correct |
47 ms |
24520 KB |
Output is correct |
23 |
Correct |
39 ms |
24308 KB |
Output is correct |
24 |
Correct |
43 ms |
24504 KB |
Output is correct |
25 |
Correct |
47 ms |
24560 KB |
Output is correct |
26 |
Correct |
38 ms |
24332 KB |
Output is correct |
27 |
Correct |
50 ms |
24564 KB |
Output is correct |
28 |
Correct |
37 ms |
24308 KB |
Output is correct |
29 |
Correct |
42 ms |
24868 KB |
Output is correct |
30 |
Correct |
42 ms |
24308 KB |
Output is correct |
31 |
Correct |
34 ms |
24308 KB |
Output is correct |
32 |
Correct |
43 ms |
24564 KB |
Output is correct |
33 |
Correct |
42 ms |
24344 KB |
Output is correct |
34 |
Correct |
28 ms |
24320 KB |
Output is correct |
35 |
Correct |
46 ms |
24568 KB |
Output is correct |
36 |
Correct |
40 ms |
24312 KB |
Output is correct |
37 |
Correct |
40 ms |
24560 KB |
Output is correct |
38 |
Correct |
39 ms |
24304 KB |
Output is correct |
39 |
Correct |
40 ms |
24308 KB |
Output is correct |
40 |
Correct |
40 ms |
24308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4456 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
153 ms |
8392 KB |
Output is correct |
12 |
Correct |
150 ms |
8264 KB |
Output is correct |
13 |
Correct |
200 ms |
7248 KB |
Output is correct |
14 |
Correct |
162 ms |
7472 KB |
Output is correct |
15 |
Correct |
163 ms |
8500 KB |
Output is correct |
16 |
Correct |
187 ms |
7504 KB |
Output is correct |
17 |
Correct |
220 ms |
7660 KB |
Output is correct |
18 |
Correct |
112 ms |
17608 KB |
Output is correct |
19 |
Correct |
142 ms |
17220 KB |
Output is correct |
20 |
Correct |
159 ms |
18772 KB |
Output is correct |
21 |
Correct |
195 ms |
22356 KB |
Output is correct |
22 |
Correct |
219 ms |
22284 KB |
Output is correct |
23 |
Correct |
245 ms |
21332 KB |
Output is correct |
24 |
Correct |
224 ms |
21328 KB |
Output is correct |
25 |
Correct |
221 ms |
23224 KB |
Output is correct |
26 |
Correct |
244 ms |
21840 KB |
Output is correct |
27 |
Correct |
263 ms |
21588 KB |
Output is correct |
28 |
Correct |
121 ms |
24296 KB |
Output is correct |
29 |
Correct |
199 ms |
20340 KB |
Output is correct |
30 |
Correct |
235 ms |
22368 KB |
Output is correct |
31 |
Correct |
250 ms |
22356 KB |
Output is correct |
32 |
Correct |
239 ms |
22340 KB |
Output is correct |
33 |
Correct |
211 ms |
21080 KB |
Output is correct |
34 |
Correct |
238 ms |
21332 KB |
Output is correct |
35 |
Correct |
252 ms |
21576 KB |
Output is correct |
36 |
Correct |
108 ms |
20120 KB |
Output is correct |
37 |
Correct |
181 ms |
22276 KB |
Output is correct |
38 |
Correct |
223 ms |
20304 KB |
Output is correct |
39 |
Correct |
244 ms |
21348 KB |
Output is correct |
40 |
Correct |
228 ms |
21332 KB |
Output is correct |
41 |
Correct |
54 ms |
22520 KB |
Output is correct |
42 |
Correct |
36 ms |
23024 KB |
Output is correct |
43 |
Correct |
38 ms |
24308 KB |
Output is correct |
44 |
Correct |
48 ms |
24304 KB |
Output is correct |
45 |
Correct |
36 ms |
24572 KB |
Output is correct |
46 |
Correct |
44 ms |
24564 KB |
Output is correct |
47 |
Correct |
42 ms |
24556 KB |
Output is correct |
48 |
Correct |
29 ms |
24640 KB |
Output is correct |
49 |
Correct |
37 ms |
24304 KB |
Output is correct |
50 |
Correct |
37 ms |
24560 KB |
Output is correct |
51 |
Correct |
41 ms |
24376 KB |
Output is correct |
52 |
Correct |
47 ms |
24520 KB |
Output is correct |
53 |
Correct |
39 ms |
24308 KB |
Output is correct |
54 |
Correct |
43 ms |
24504 KB |
Output is correct |
55 |
Correct |
47 ms |
24560 KB |
Output is correct |
56 |
Correct |
38 ms |
24332 KB |
Output is correct |
57 |
Correct |
50 ms |
24564 KB |
Output is correct |
58 |
Correct |
37 ms |
24308 KB |
Output is correct |
59 |
Correct |
42 ms |
24868 KB |
Output is correct |
60 |
Correct |
42 ms |
24308 KB |
Output is correct |
61 |
Correct |
34 ms |
24308 KB |
Output is correct |
62 |
Correct |
43 ms |
24564 KB |
Output is correct |
63 |
Correct |
42 ms |
24344 KB |
Output is correct |
64 |
Correct |
28 ms |
24320 KB |
Output is correct |
65 |
Correct |
46 ms |
24568 KB |
Output is correct |
66 |
Correct |
40 ms |
24312 KB |
Output is correct |
67 |
Correct |
40 ms |
24560 KB |
Output is correct |
68 |
Correct |
39 ms |
24304 KB |
Output is correct |
69 |
Correct |
40 ms |
24308 KB |
Output is correct |
70 |
Correct |
40 ms |
24308 KB |
Output is correct |
71 |
Correct |
320 ms |
48864 KB |
Output is correct |
72 |
Correct |
367 ms |
48664 KB |
Output is correct |
73 |
Correct |
366 ms |
47316 KB |
Output is correct |
74 |
Correct |
543 ms |
47860 KB |
Output is correct |
75 |
Correct |
333 ms |
49776 KB |
Output is correct |
76 |
Correct |
482 ms |
48116 KB |
Output is correct |
77 |
Correct |
431 ms |
48116 KB |
Output is correct |
78 |
Correct |
174 ms |
51708 KB |
Output is correct |
79 |
Correct |
320 ms |
45812 KB |
Output is correct |
80 |
Correct |
345 ms |
48892 KB |
Output is correct |
81 |
Correct |
406 ms |
48752 KB |
Output is correct |
82 |
Correct |
541 ms |
47672 KB |
Output is correct |
83 |
Correct |
315 ms |
46876 KB |
Output is correct |
84 |
Correct |
466 ms |
47860 KB |
Output is correct |
85 |
Correct |
444 ms |
48892 KB |
Output is correct |
86 |
Correct |
165 ms |
45812 KB |
Output is correct |
87 |
Correct |
311 ms |
48708 KB |
Output is correct |
88 |
Correct |
362 ms |
46072 KB |
Output is correct |
89 |
Correct |
393 ms |
47344 KB |
Output is correct |
90 |
Correct |
443 ms |
47832 KB |
Output is correct |
91 |
Correct |
292 ms |
46840 KB |
Output is correct |
92 |
Correct |
466 ms |
48112 KB |
Output is correct |
93 |
Correct |
456 ms |
47864 KB |
Output is correct |
94 |
Correct |
165 ms |
45828 KB |
Output is correct |
95 |
Correct |
383 ms |
47864 KB |
Output is correct |
96 |
Correct |
398 ms |
47824 KB |
Output is correct |
97 |
Correct |
385 ms |
48084 KB |
Output is correct |
98 |
Correct |
384 ms |
47856 KB |
Output is correct |
99 |
Correct |
393 ms |
47848 KB |
Output is correct |
100 |
Correct |
384 ms |
47860 KB |
Output is correct |