#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;reverse(all(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){
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){
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{
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(){
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: 'long long 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:44:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int j=0;j<tmp.size();j++){
| ~^~~~~~~~~~~
snake_escaping.cpp:57:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int j=0;j<tmp.size();j++){
| ~^~~~~~~~~~~
snake_escaping.cpp:75:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(int j=0;j<tmp.size();j++){
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 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 |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
348 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 |
348 KB |
Output is correct |
2 |
Correct |
2 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 |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1541 ms |
5496 KB |
Output is correct |
12 |
Correct |
1602 ms |
14816 KB |
Output is correct |
13 |
Correct |
1753 ms |
13928 KB |
Output is correct |
14 |
Correct |
1785 ms |
14056 KB |
Output is correct |
15 |
Correct |
1679 ms |
15180 KB |
Output is correct |
16 |
Correct |
1733 ms |
14324 KB |
Output is correct |
17 |
Correct |
1746 ms |
14288 KB |
Output is correct |
18 |
Correct |
1568 ms |
16172 KB |
Output is correct |
19 |
Correct |
1635 ms |
12956 KB |
Output is correct |
20 |
Correct |
1532 ms |
14644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 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 |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1541 ms |
5496 KB |
Output is correct |
12 |
Correct |
1602 ms |
14816 KB |
Output is correct |
13 |
Correct |
1753 ms |
13928 KB |
Output is correct |
14 |
Correct |
1785 ms |
14056 KB |
Output is correct |
15 |
Correct |
1679 ms |
15180 KB |
Output is correct |
16 |
Correct |
1733 ms |
14324 KB |
Output is correct |
17 |
Correct |
1746 ms |
14288 KB |
Output is correct |
18 |
Correct |
1568 ms |
16172 KB |
Output is correct |
19 |
Correct |
1635 ms |
12956 KB |
Output is correct |
20 |
Correct |
1532 ms |
14644 KB |
Output is correct |
21 |
Correct |
1694 ms |
18148 KB |
Output is correct |
22 |
Correct |
1676 ms |
18456 KB |
Output is correct |
23 |
Correct |
1864 ms |
17756 KB |
Output is correct |
24 |
Correct |
1986 ms |
17200 KB |
Output is correct |
25 |
Correct |
1773 ms |
19180 KB |
Output is correct |
26 |
Correct |
1897 ms |
17676 KB |
Output is correct |
27 |
Correct |
1863 ms |
17572 KB |
Output is correct |
28 |
Correct |
1565 ms |
20304 KB |
Output is correct |
29 |
Correct |
1695 ms |
16096 KB |
Output is correct |
30 |
Correct |
1686 ms |
18260 KB |
Output is correct |
31 |
Correct |
1844 ms |
18168 KB |
Output is correct |
32 |
Correct |
1922 ms |
18284 KB |
Output is correct |
33 |
Correct |
1742 ms |
17048 KB |
Output is correct |
34 |
Correct |
1973 ms |
17164 KB |
Output is correct |
35 |
Correct |
1871 ms |
17560 KB |
Output is correct |
36 |
Correct |
1532 ms |
16104 KB |
Output is correct |
37 |
Correct |
1701 ms |
18192 KB |
Output is correct |
38 |
Correct |
1642 ms |
16288 KB |
Output is correct |
39 |
Correct |
1837 ms |
17268 KB |
Output is correct |
40 |
Correct |
1895 ms |
17208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 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 |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
152 ms |
28060 KB |
Output is correct |
12 |
Correct |
160 ms |
28484 KB |
Output is correct |
13 |
Correct |
161 ms |
28256 KB |
Output is correct |
14 |
Correct |
206 ms |
28256 KB |
Output is correct |
15 |
Correct |
149 ms |
28476 KB |
Output is correct |
16 |
Correct |
206 ms |
28228 KB |
Output is correct |
17 |
Correct |
199 ms |
28716 KB |
Output is correct |
18 |
Correct |
130 ms |
28496 KB |
Output is correct |
19 |
Correct |
145 ms |
28228 KB |
Output is correct |
20 |
Correct |
145 ms |
28480 KB |
Output is correct |
21 |
Correct |
176 ms |
28324 KB |
Output is correct |
22 |
Correct |
215 ms |
28456 KB |
Output is correct |
23 |
Correct |
163 ms |
28228 KB |
Output is correct |
24 |
Correct |
215 ms |
28488 KB |
Output is correct |
25 |
Correct |
194 ms |
28224 KB |
Output is correct |
26 |
Correct |
141 ms |
28268 KB |
Output is correct |
27 |
Correct |
156 ms |
28496 KB |
Output is correct |
28 |
Correct |
144 ms |
28244 KB |
Output is correct |
29 |
Correct |
173 ms |
28376 KB |
Output is correct |
30 |
Correct |
198 ms |
28492 KB |
Output is correct |
31 |
Correct |
142 ms |
28224 KB |
Output is correct |
32 |
Correct |
247 ms |
28252 KB |
Output is correct |
33 |
Correct |
194 ms |
28424 KB |
Output is correct |
34 |
Correct |
135 ms |
28260 KB |
Output is correct |
35 |
Correct |
176 ms |
28280 KB |
Output is correct |
36 |
Correct |
183 ms |
28232 KB |
Output is correct |
37 |
Correct |
184 ms |
28448 KB |
Output is correct |
38 |
Correct |
177 ms |
28224 KB |
Output is correct |
39 |
Correct |
189 ms |
28256 KB |
Output is correct |
40 |
Correct |
191 ms |
28416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 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 |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1541 ms |
5496 KB |
Output is correct |
12 |
Correct |
1602 ms |
14816 KB |
Output is correct |
13 |
Correct |
1753 ms |
13928 KB |
Output is correct |
14 |
Correct |
1785 ms |
14056 KB |
Output is correct |
15 |
Correct |
1679 ms |
15180 KB |
Output is correct |
16 |
Correct |
1733 ms |
14324 KB |
Output is correct |
17 |
Correct |
1746 ms |
14288 KB |
Output is correct |
18 |
Correct |
1568 ms |
16172 KB |
Output is correct |
19 |
Correct |
1635 ms |
12956 KB |
Output is correct |
20 |
Correct |
1532 ms |
14644 KB |
Output is correct |
21 |
Correct |
1694 ms |
18148 KB |
Output is correct |
22 |
Correct |
1676 ms |
18456 KB |
Output is correct |
23 |
Correct |
1864 ms |
17756 KB |
Output is correct |
24 |
Correct |
1986 ms |
17200 KB |
Output is correct |
25 |
Correct |
1773 ms |
19180 KB |
Output is correct |
26 |
Correct |
1897 ms |
17676 KB |
Output is correct |
27 |
Correct |
1863 ms |
17572 KB |
Output is correct |
28 |
Correct |
1565 ms |
20304 KB |
Output is correct |
29 |
Correct |
1695 ms |
16096 KB |
Output is correct |
30 |
Correct |
1686 ms |
18260 KB |
Output is correct |
31 |
Correct |
1844 ms |
18168 KB |
Output is correct |
32 |
Correct |
1922 ms |
18284 KB |
Output is correct |
33 |
Correct |
1742 ms |
17048 KB |
Output is correct |
34 |
Correct |
1973 ms |
17164 KB |
Output is correct |
35 |
Correct |
1871 ms |
17560 KB |
Output is correct |
36 |
Correct |
1532 ms |
16104 KB |
Output is correct |
37 |
Correct |
1701 ms |
18192 KB |
Output is correct |
38 |
Correct |
1642 ms |
16288 KB |
Output is correct |
39 |
Correct |
1837 ms |
17268 KB |
Output is correct |
40 |
Correct |
1895 ms |
17208 KB |
Output is correct |
41 |
Correct |
152 ms |
28060 KB |
Output is correct |
42 |
Correct |
160 ms |
28484 KB |
Output is correct |
43 |
Correct |
161 ms |
28256 KB |
Output is correct |
44 |
Correct |
206 ms |
28256 KB |
Output is correct |
45 |
Correct |
149 ms |
28476 KB |
Output is correct |
46 |
Correct |
206 ms |
28228 KB |
Output is correct |
47 |
Correct |
199 ms |
28716 KB |
Output is correct |
48 |
Correct |
130 ms |
28496 KB |
Output is correct |
49 |
Correct |
145 ms |
28228 KB |
Output is correct |
50 |
Correct |
145 ms |
28480 KB |
Output is correct |
51 |
Correct |
176 ms |
28324 KB |
Output is correct |
52 |
Correct |
215 ms |
28456 KB |
Output is correct |
53 |
Correct |
163 ms |
28228 KB |
Output is correct |
54 |
Correct |
215 ms |
28488 KB |
Output is correct |
55 |
Correct |
194 ms |
28224 KB |
Output is correct |
56 |
Correct |
141 ms |
28268 KB |
Output is correct |
57 |
Correct |
156 ms |
28496 KB |
Output is correct |
58 |
Correct |
144 ms |
28244 KB |
Output is correct |
59 |
Correct |
173 ms |
28376 KB |
Output is correct |
60 |
Correct |
198 ms |
28492 KB |
Output is correct |
61 |
Correct |
142 ms |
28224 KB |
Output is correct |
62 |
Correct |
247 ms |
28252 KB |
Output is correct |
63 |
Correct |
194 ms |
28424 KB |
Output is correct |
64 |
Correct |
135 ms |
28260 KB |
Output is correct |
65 |
Correct |
176 ms |
28280 KB |
Output is correct |
66 |
Correct |
183 ms |
28232 KB |
Output is correct |
67 |
Correct |
184 ms |
28448 KB |
Output is correct |
68 |
Correct |
177 ms |
28224 KB |
Output is correct |
69 |
Correct |
189 ms |
28256 KB |
Output is correct |
70 |
Correct |
191 ms |
28416 KB |
Output is correct |
71 |
Execution timed out |
2019 ms |
52420 KB |
Time limit exceeded |
72 |
Halted |
0 ms |
0 KB |
- |