#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=(1<<20)+5;
int dp[N],dp2[N],a[N];
void solve(){
int n,q;cin>>n>>q;
string s;cin>>s;
for(int i=0;i<(1<<n);i++) a[i]=(s[i]-'0');
for(int i=0;i<(1<<n);i++) dp[i]=dp2[i]=a[i];
for(int i=0;i<n;i++){
for(int mask=0;mask<(1<<n);mask++){
if(mask&(1<<i)) dp[mask]+=dp[mask^(1<<i)];
else dp2[mask]+=dp2[mask^(1<<i)];
}
}
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<=6){
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];
}
cout<<ans<<endl;
}
else if(o<=6){
for(int i=0;i<n;i++){
if(st[i]=='1' || st[i]=='?') sum+=(1<<i);
if(st[i]=='1') tmp.pb(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^=(1<<tmp[j]);
c++;
}
}
if(c%2) ans-=dp[tmpsum];
else ans+=dp[tmpsum];
}
cout<<ans<<endl;
}
else{
for(int i=0;i<n;i++){
if(st[i]=='1') sum+=(1<<i);
if(st[i]=='0') tmp.pb(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^=(1<<tmp[j]);
c++;
}
}
if(c%2) ans-=dp2[tmpsum];
else ans+=dp2[tmpsum];
}
cout<<ans<<endl;
}
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;//cin>>t;
while(t--)
solve();
}
Compilation message
snake_escaping.cpp: In function 'void solve()':
snake_escaping.cpp:29:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for(int i=0;i<st.size();i++){
| ~^~~~~~~~~~
snake_escaping.cpp:43:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int j=0;j<tmp.size();j++){
| ~^~~~~~~~~~~
snake_escaping.cpp:57:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<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: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(int j=0;j<tmp.size();j++){
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4564 KB |
Output is correct |
2 |
Correct |
3 ms |
4444 KB |
Output is correct |
3 |
Correct |
3 ms |
4440 KB |
Output is correct |
4 |
Correct |
3 ms |
4460 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 |
4696 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4444 KB |
Output is correct |
10 |
Correct |
3 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4564 KB |
Output is correct |
2 |
Correct |
3 ms |
4444 KB |
Output is correct |
3 |
Correct |
3 ms |
4440 KB |
Output is correct |
4 |
Correct |
3 ms |
4460 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 |
4696 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4444 KB |
Output is correct |
10 |
Correct |
3 ms |
4444 KB |
Output is correct |
11 |
Correct |
1662 ms |
17052 KB |
Output is correct |
12 |
Correct |
1641 ms |
16760 KB |
Output is correct |
13 |
Correct |
1501 ms |
16408 KB |
Output is correct |
14 |
Correct |
1499 ms |
16064 KB |
Output is correct |
15 |
Execution timed out |
2021 ms |
16996 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4564 KB |
Output is correct |
2 |
Correct |
3 ms |
4444 KB |
Output is correct |
3 |
Correct |
3 ms |
4440 KB |
Output is correct |
4 |
Correct |
3 ms |
4460 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 |
4696 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4444 KB |
Output is correct |
10 |
Correct |
3 ms |
4444 KB |
Output is correct |
11 |
Correct |
1662 ms |
17052 KB |
Output is correct |
12 |
Correct |
1641 ms |
16760 KB |
Output is correct |
13 |
Correct |
1501 ms |
16408 KB |
Output is correct |
14 |
Correct |
1499 ms |
16064 KB |
Output is correct |
15 |
Execution timed out |
2021 ms |
16996 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4564 KB |
Output is correct |
2 |
Correct |
3 ms |
4444 KB |
Output is correct |
3 |
Correct |
3 ms |
4440 KB |
Output is correct |
4 |
Correct |
3 ms |
4460 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 |
4696 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4444 KB |
Output is correct |
10 |
Correct |
3 ms |
4444 KB |
Output is correct |
11 |
Correct |
96 ms |
15980 KB |
Output is correct |
12 |
Correct |
107 ms |
16212 KB |
Output is correct |
13 |
Correct |
112 ms |
15996 KB |
Output is correct |
14 |
Correct |
132 ms |
15960 KB |
Output is correct |
15 |
Correct |
107 ms |
16096 KB |
Output is correct |
16 |
Correct |
128 ms |
15956 KB |
Output is correct |
17 |
Correct |
147 ms |
16388 KB |
Output is correct |
18 |
Correct |
93 ms |
16216 KB |
Output is correct |
19 |
Correct |
106 ms |
15880 KB |
Output is correct |
20 |
Correct |
101 ms |
16116 KB |
Output is correct |
21 |
Correct |
118 ms |
15956 KB |
Output is correct |
22 |
Correct |
135 ms |
15936 KB |
Output is correct |
23 |
Correct |
107 ms |
15964 KB |
Output is correct |
24 |
Correct |
136 ms |
16564 KB |
Output is correct |
25 |
Correct |
140 ms |
16112 KB |
Output is correct |
26 |
Correct |
95 ms |
15940 KB |
Output is correct |
27 |
Correct |
97 ms |
16068 KB |
Output is correct |
28 |
Correct |
95 ms |
15952 KB |
Output is correct |
29 |
Correct |
117 ms |
16000 KB |
Output is correct |
30 |
Correct |
136 ms |
15960 KB |
Output is correct |
31 |
Correct |
110 ms |
16260 KB |
Output is correct |
32 |
Correct |
138 ms |
16112 KB |
Output is correct |
33 |
Correct |
128 ms |
15944 KB |
Output is correct |
34 |
Correct |
86 ms |
15968 KB |
Output is correct |
35 |
Correct |
130 ms |
16116 KB |
Output is correct |
36 |
Correct |
124 ms |
16048 KB |
Output is correct |
37 |
Correct |
144 ms |
16008 KB |
Output is correct |
38 |
Correct |
126 ms |
16112 KB |
Output is correct |
39 |
Correct |
122 ms |
16116 KB |
Output is correct |
40 |
Correct |
123 ms |
15940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4564 KB |
Output is correct |
2 |
Correct |
3 ms |
4444 KB |
Output is correct |
3 |
Correct |
3 ms |
4440 KB |
Output is correct |
4 |
Correct |
3 ms |
4460 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 |
4696 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4444 KB |
Output is correct |
10 |
Correct |
3 ms |
4444 KB |
Output is correct |
11 |
Correct |
1662 ms |
17052 KB |
Output is correct |
12 |
Correct |
1641 ms |
16760 KB |
Output is correct |
13 |
Correct |
1501 ms |
16408 KB |
Output is correct |
14 |
Correct |
1499 ms |
16064 KB |
Output is correct |
15 |
Execution timed out |
2021 ms |
16996 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |