#include <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fr first
#define sc second
//#define mp make_pair
#define ll long long
//#define int ll
//#define double long double
//const ll INF=1e18,N=1e6+5;
int dp[(1<<20)+5][2];
void solve(){
int n,q;cin>>n>>q;
string s;cin>>s;
vector<int> a((1<<n)+5);
for(int i=0;i<(1<<n);i++) a[i]=(s[i]-'0');
for(int i=0;i<(1<<n);i++) dp[i][0]=dp[i][1]=a[i];
for(int i=0;i<n;i++){
for(int mask=0;mask<(1<<n);mask++){
if(mask&(1<<i)) dp[mask][0]+=dp[mask^(1<<i)][0];
else dp[mask][1]+=dp[mask^(1<<i)][1];
}
}
while(q--){
string st;cin>>st;
int z=0,o=0,u=0;
for(int i=0;i<st.size();i++){
if(st[i]=='?') u++;
else if(st[i]=='0') z++;
else o++;
}
int sum=0,ans=0;
vector<int> tmp;
if(u<=z && u<=o){
reverse(all(st));
for(int i=0;i<n;i++){
if(st[i]=='1') sum+=(1<<i);
if(st[i]=='?') tmp.pb(1<<i);
}
for(int i=0;i<(1<<tmp.size());i++){
int tmpsum=sum;
for(int j=0;j<tmp.size();j++){
if(i&(1<<j)) tmpsum+=tmp[j];
}
ans+=a[tmpsum];
}
}
else if(o<=z && o<=u){
reverse(all(st));
for(int i=0;i<n;i++){
if(st[i]=='1' || st[i]=='?') sum+=(1<<i);
if(st[i]=='1') tmp.pb(1<<i);
}
for(int i=0;i<(1<<tmp.size());i++){
int tmpsum=sum,c=0;
for(int j=0;j<tmp.size();j++){
if(i&(1<<j)){
tmpsum^=(tmp[j]);
c++;
}
}
if(c%2) c=-1;
else c=1;
ans+=c*dp[tmpsum][0];
}
}
else{
reverse(all(st));
for(int i=0;i<n;i++){
if(st[i]=='1') sum+=(1<<i);
if(st[i]=='0') tmp.pb(1<<i);
}
for(int i=0;i<(1<<tmp.size());i++){
int tmpsum=sum,c=0;
for(int j=0;j<tmp.size();j++){
if(!(i&(1<<j))){
tmpsum^=(tmp[j]);
c++;
}
}
if(c%2) c=-1;
else c=1;
ans+=c*dp[tmpsum][1];
}
}
cout<<ans<<endl;
}
}
signed main(){
ios;
int t=1;//cin>>t;
while(t--)
solve();
}
Compilation message
snake_escaping.cpp: In function 'void solve()':
snake_escaping.cpp:30:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i=0;i<st.size();i++){
| ~^~~~~~~~~~
snake_escaping.cpp:45:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int j=0;j<tmp.size();j++){
| ~^~~~~~~~~~~
snake_escaping.cpp:59:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int j=0;j<tmp.size();j++){
| ~^~~~~~~~~~~
snake_escaping.cpp:78:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int j=0;j<tmp.size();j++){
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1391 ms |
14420 KB |
Output is correct |
12 |
Correct |
1319 ms |
14060 KB |
Output is correct |
13 |
Correct |
1449 ms |
13116 KB |
Output is correct |
14 |
Correct |
1524 ms |
13400 KB |
Output is correct |
15 |
Correct |
1470 ms |
14632 KB |
Output is correct |
16 |
Correct |
1519 ms |
13340 KB |
Output is correct |
17 |
Correct |
1501 ms |
13088 KB |
Output is correct |
18 |
Correct |
1232 ms |
15216 KB |
Output is correct |
19 |
Correct |
1353 ms |
11996 KB |
Output is correct |
20 |
Correct |
1428 ms |
13708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1391 ms |
14420 KB |
Output is correct |
12 |
Correct |
1319 ms |
14060 KB |
Output is correct |
13 |
Correct |
1449 ms |
13116 KB |
Output is correct |
14 |
Correct |
1524 ms |
13400 KB |
Output is correct |
15 |
Correct |
1470 ms |
14632 KB |
Output is correct |
16 |
Correct |
1519 ms |
13340 KB |
Output is correct |
17 |
Correct |
1501 ms |
13088 KB |
Output is correct |
18 |
Correct |
1232 ms |
15216 KB |
Output is correct |
19 |
Correct |
1353 ms |
11996 KB |
Output is correct |
20 |
Correct |
1428 ms |
13708 KB |
Output is correct |
21 |
Correct |
1373 ms |
17200 KB |
Output is correct |
22 |
Correct |
1407 ms |
17060 KB |
Output is correct |
23 |
Correct |
1572 ms |
16340 KB |
Output is correct |
24 |
Correct |
1639 ms |
16016 KB |
Output is correct |
25 |
Correct |
1463 ms |
17992 KB |
Output is correct |
26 |
Correct |
1656 ms |
16704 KB |
Output is correct |
27 |
Correct |
1553 ms |
16332 KB |
Output is correct |
28 |
Correct |
1291 ms |
19812 KB |
Output is correct |
29 |
Correct |
1429 ms |
15868 KB |
Output is correct |
30 |
Correct |
1379 ms |
17060 KB |
Output is correct |
31 |
Correct |
1510 ms |
17060 KB |
Output is correct |
32 |
Correct |
1649 ms |
17084 KB |
Output is correct |
33 |
Correct |
1531 ms |
15888 KB |
Output is correct |
34 |
Correct |
1615 ms |
16084 KB |
Output is correct |
35 |
Correct |
1563 ms |
16544 KB |
Output is correct |
36 |
Correct |
1305 ms |
15080 KB |
Output is correct |
37 |
Correct |
1352 ms |
17468 KB |
Output is correct |
38 |
Correct |
1400 ms |
14904 KB |
Output is correct |
39 |
Correct |
1564 ms |
16280 KB |
Output is correct |
40 |
Correct |
1598 ms |
15916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
102 ms |
15976 KB |
Output is correct |
12 |
Correct |
98 ms |
15972 KB |
Output is correct |
13 |
Correct |
115 ms |
15996 KB |
Output is correct |
14 |
Correct |
153 ms |
16368 KB |
Output is correct |
15 |
Correct |
116 ms |
16092 KB |
Output is correct |
16 |
Correct |
147 ms |
15964 KB |
Output is correct |
17 |
Correct |
154 ms |
16112 KB |
Output is correct |
18 |
Correct |
98 ms |
16212 KB |
Output is correct |
19 |
Correct |
95 ms |
15864 KB |
Output is correct |
20 |
Correct |
115 ms |
16116 KB |
Output is correct |
21 |
Correct |
121 ms |
16116 KB |
Output is correct |
22 |
Correct |
151 ms |
16160 KB |
Output is correct |
23 |
Correct |
100 ms |
16116 KB |
Output is correct |
24 |
Correct |
137 ms |
16116 KB |
Output is correct |
25 |
Correct |
125 ms |
15948 KB |
Output is correct |
26 |
Correct |
101 ms |
15892 KB |
Output is correct |
27 |
Correct |
115 ms |
16216 KB |
Output is correct |
28 |
Correct |
106 ms |
15860 KB |
Output is correct |
29 |
Correct |
119 ms |
16096 KB |
Output is correct |
30 |
Correct |
129 ms |
16012 KB |
Output is correct |
31 |
Correct |
106 ms |
16108 KB |
Output is correct |
32 |
Correct |
149 ms |
16304 KB |
Output is correct |
33 |
Correct |
144 ms |
16564 KB |
Output is correct |
34 |
Correct |
97 ms |
16064 KB |
Output is correct |
35 |
Correct |
114 ms |
15980 KB |
Output is correct |
36 |
Correct |
124 ms |
15960 KB |
Output is correct |
37 |
Correct |
152 ms |
16112 KB |
Output is correct |
38 |
Correct |
131 ms |
15916 KB |
Output is correct |
39 |
Correct |
128 ms |
16008 KB |
Output is correct |
40 |
Correct |
148 ms |
15948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1391 ms |
14420 KB |
Output is correct |
12 |
Correct |
1319 ms |
14060 KB |
Output is correct |
13 |
Correct |
1449 ms |
13116 KB |
Output is correct |
14 |
Correct |
1524 ms |
13400 KB |
Output is correct |
15 |
Correct |
1470 ms |
14632 KB |
Output is correct |
16 |
Correct |
1519 ms |
13340 KB |
Output is correct |
17 |
Correct |
1501 ms |
13088 KB |
Output is correct |
18 |
Correct |
1232 ms |
15216 KB |
Output is correct |
19 |
Correct |
1353 ms |
11996 KB |
Output is correct |
20 |
Correct |
1428 ms |
13708 KB |
Output is correct |
21 |
Correct |
1373 ms |
17200 KB |
Output is correct |
22 |
Correct |
1407 ms |
17060 KB |
Output is correct |
23 |
Correct |
1572 ms |
16340 KB |
Output is correct |
24 |
Correct |
1639 ms |
16016 KB |
Output is correct |
25 |
Correct |
1463 ms |
17992 KB |
Output is correct |
26 |
Correct |
1656 ms |
16704 KB |
Output is correct |
27 |
Correct |
1553 ms |
16332 KB |
Output is correct |
28 |
Correct |
1291 ms |
19812 KB |
Output is correct |
29 |
Correct |
1429 ms |
15868 KB |
Output is correct |
30 |
Correct |
1379 ms |
17060 KB |
Output is correct |
31 |
Correct |
1510 ms |
17060 KB |
Output is correct |
32 |
Correct |
1649 ms |
17084 KB |
Output is correct |
33 |
Correct |
1531 ms |
15888 KB |
Output is correct |
34 |
Correct |
1615 ms |
16084 KB |
Output is correct |
35 |
Correct |
1563 ms |
16544 KB |
Output is correct |
36 |
Correct |
1305 ms |
15080 KB |
Output is correct |
37 |
Correct |
1352 ms |
17468 KB |
Output is correct |
38 |
Correct |
1400 ms |
14904 KB |
Output is correct |
39 |
Correct |
1564 ms |
16280 KB |
Output is correct |
40 |
Correct |
1598 ms |
15916 KB |
Output is correct |
41 |
Correct |
102 ms |
15976 KB |
Output is correct |
42 |
Correct |
98 ms |
15972 KB |
Output is correct |
43 |
Correct |
115 ms |
15996 KB |
Output is correct |
44 |
Correct |
153 ms |
16368 KB |
Output is correct |
45 |
Correct |
116 ms |
16092 KB |
Output is correct |
46 |
Correct |
147 ms |
15964 KB |
Output is correct |
47 |
Correct |
154 ms |
16112 KB |
Output is correct |
48 |
Correct |
98 ms |
16212 KB |
Output is correct |
49 |
Correct |
95 ms |
15864 KB |
Output is correct |
50 |
Correct |
115 ms |
16116 KB |
Output is correct |
51 |
Correct |
121 ms |
16116 KB |
Output is correct |
52 |
Correct |
151 ms |
16160 KB |
Output is correct |
53 |
Correct |
100 ms |
16116 KB |
Output is correct |
54 |
Correct |
137 ms |
16116 KB |
Output is correct |
55 |
Correct |
125 ms |
15948 KB |
Output is correct |
56 |
Correct |
101 ms |
15892 KB |
Output is correct |
57 |
Correct |
115 ms |
16216 KB |
Output is correct |
58 |
Correct |
106 ms |
15860 KB |
Output is correct |
59 |
Correct |
119 ms |
16096 KB |
Output is correct |
60 |
Correct |
129 ms |
16012 KB |
Output is correct |
61 |
Correct |
106 ms |
16108 KB |
Output is correct |
62 |
Correct |
149 ms |
16304 KB |
Output is correct |
63 |
Correct |
144 ms |
16564 KB |
Output is correct |
64 |
Correct |
97 ms |
16064 KB |
Output is correct |
65 |
Correct |
114 ms |
15980 KB |
Output is correct |
66 |
Correct |
124 ms |
15960 KB |
Output is correct |
67 |
Correct |
152 ms |
16112 KB |
Output is correct |
68 |
Correct |
131 ms |
15916 KB |
Output is correct |
69 |
Correct |
128 ms |
16008 KB |
Output is correct |
70 |
Correct |
148 ms |
15948 KB |
Output is correct |
71 |
Correct |
1607 ms |
38328 KB |
Output is correct |
72 |
Correct |
1646 ms |
38292 KB |
Output is correct |
73 |
Correct |
1800 ms |
36932 KB |
Output is correct |
74 |
Execution timed out |
2071 ms |
35684 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |