#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){
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(){
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: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:44:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<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: '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 |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
468 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 |
352 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
468 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 |
352 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 |
1334 ms |
15300 KB |
Output is correct |
12 |
Correct |
1464 ms |
14380 KB |
Output is correct |
13 |
Correct |
1502 ms |
13768 KB |
Output is correct |
14 |
Correct |
1491 ms |
13928 KB |
Output is correct |
15 |
Correct |
1491 ms |
15148 KB |
Output is correct |
16 |
Correct |
1592 ms |
14072 KB |
Output is correct |
17 |
Correct |
1549 ms |
14060 KB |
Output is correct |
18 |
Correct |
1442 ms |
15872 KB |
Output is correct |
19 |
Correct |
1531 ms |
12868 KB |
Output is correct |
20 |
Correct |
1663 ms |
14576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
468 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 |
352 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 |
1334 ms |
15300 KB |
Output is correct |
12 |
Correct |
1464 ms |
14380 KB |
Output is correct |
13 |
Correct |
1502 ms |
13768 KB |
Output is correct |
14 |
Correct |
1491 ms |
13928 KB |
Output is correct |
15 |
Correct |
1491 ms |
15148 KB |
Output is correct |
16 |
Correct |
1592 ms |
14072 KB |
Output is correct |
17 |
Correct |
1549 ms |
14060 KB |
Output is correct |
18 |
Correct |
1442 ms |
15872 KB |
Output is correct |
19 |
Correct |
1531 ms |
12868 KB |
Output is correct |
20 |
Correct |
1663 ms |
14576 KB |
Output is correct |
21 |
Correct |
1667 ms |
17864 KB |
Output is correct |
22 |
Correct |
1638 ms |
18128 KB |
Output is correct |
23 |
Correct |
1762 ms |
16952 KB |
Output is correct |
24 |
Correct |
1825 ms |
16868 KB |
Output is correct |
25 |
Correct |
1617 ms |
18860 KB |
Output is correct |
26 |
Correct |
1768 ms |
17396 KB |
Output is correct |
27 |
Correct |
1579 ms |
17288 KB |
Output is correct |
28 |
Correct |
1262 ms |
20088 KB |
Output is correct |
29 |
Correct |
1333 ms |
15936 KB |
Output is correct |
30 |
Correct |
1403 ms |
17952 KB |
Output is correct |
31 |
Correct |
1492 ms |
17900 KB |
Output is correct |
32 |
Correct |
1665 ms |
17768 KB |
Output is correct |
33 |
Correct |
1453 ms |
16888 KB |
Output is correct |
34 |
Correct |
1607 ms |
16952 KB |
Output is correct |
35 |
Correct |
1556 ms |
17640 KB |
Output is correct |
36 |
Correct |
1259 ms |
16236 KB |
Output is correct |
37 |
Correct |
1476 ms |
18156 KB |
Output is correct |
38 |
Correct |
1427 ms |
16028 KB |
Output is correct |
39 |
Correct |
1599 ms |
17148 KB |
Output is correct |
40 |
Correct |
1625 ms |
16856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
468 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 |
352 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 |
108 ms |
16012 KB |
Output is correct |
12 |
Correct |
103 ms |
16116 KB |
Output is correct |
13 |
Correct |
115 ms |
16116 KB |
Output is correct |
14 |
Correct |
156 ms |
16100 KB |
Output is correct |
15 |
Correct |
99 ms |
16188 KB |
Output is correct |
16 |
Correct |
141 ms |
15956 KB |
Output is correct |
17 |
Correct |
136 ms |
16152 KB |
Output is correct |
18 |
Correct |
87 ms |
16112 KB |
Output is correct |
19 |
Correct |
96 ms |
15836 KB |
Output is correct |
20 |
Correct |
106 ms |
16216 KB |
Output is correct |
21 |
Correct |
114 ms |
16004 KB |
Output is correct |
22 |
Correct |
140 ms |
16116 KB |
Output is correct |
23 |
Correct |
100 ms |
16116 KB |
Output is correct |
24 |
Correct |
143 ms |
16504 KB |
Output is correct |
25 |
Correct |
140 ms |
16056 KB |
Output is correct |
26 |
Correct |
106 ms |
15836 KB |
Output is correct |
27 |
Correct |
99 ms |
15988 KB |
Output is correct |
28 |
Correct |
97 ms |
15860 KB |
Output is correct |
29 |
Correct |
110 ms |
16140 KB |
Output is correct |
30 |
Correct |
141 ms |
16100 KB |
Output is correct |
31 |
Correct |
108 ms |
16120 KB |
Output is correct |
32 |
Correct |
132 ms |
15940 KB |
Output is correct |
33 |
Correct |
140 ms |
16048 KB |
Output is correct |
34 |
Correct |
95 ms |
15864 KB |
Output is correct |
35 |
Correct |
122 ms |
16156 KB |
Output is correct |
36 |
Correct |
132 ms |
16116 KB |
Output is correct |
37 |
Correct |
118 ms |
16116 KB |
Output is correct |
38 |
Correct |
127 ms |
16020 KB |
Output is correct |
39 |
Correct |
132 ms |
16116 KB |
Output is correct |
40 |
Correct |
139 ms |
16068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
468 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 |
352 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 |
1334 ms |
15300 KB |
Output is correct |
12 |
Correct |
1464 ms |
14380 KB |
Output is correct |
13 |
Correct |
1502 ms |
13768 KB |
Output is correct |
14 |
Correct |
1491 ms |
13928 KB |
Output is correct |
15 |
Correct |
1491 ms |
15148 KB |
Output is correct |
16 |
Correct |
1592 ms |
14072 KB |
Output is correct |
17 |
Correct |
1549 ms |
14060 KB |
Output is correct |
18 |
Correct |
1442 ms |
15872 KB |
Output is correct |
19 |
Correct |
1531 ms |
12868 KB |
Output is correct |
20 |
Correct |
1663 ms |
14576 KB |
Output is correct |
21 |
Correct |
1667 ms |
17864 KB |
Output is correct |
22 |
Correct |
1638 ms |
18128 KB |
Output is correct |
23 |
Correct |
1762 ms |
16952 KB |
Output is correct |
24 |
Correct |
1825 ms |
16868 KB |
Output is correct |
25 |
Correct |
1617 ms |
18860 KB |
Output is correct |
26 |
Correct |
1768 ms |
17396 KB |
Output is correct |
27 |
Correct |
1579 ms |
17288 KB |
Output is correct |
28 |
Correct |
1262 ms |
20088 KB |
Output is correct |
29 |
Correct |
1333 ms |
15936 KB |
Output is correct |
30 |
Correct |
1403 ms |
17952 KB |
Output is correct |
31 |
Correct |
1492 ms |
17900 KB |
Output is correct |
32 |
Correct |
1665 ms |
17768 KB |
Output is correct |
33 |
Correct |
1453 ms |
16888 KB |
Output is correct |
34 |
Correct |
1607 ms |
16952 KB |
Output is correct |
35 |
Correct |
1556 ms |
17640 KB |
Output is correct |
36 |
Correct |
1259 ms |
16236 KB |
Output is correct |
37 |
Correct |
1476 ms |
18156 KB |
Output is correct |
38 |
Correct |
1427 ms |
16028 KB |
Output is correct |
39 |
Correct |
1599 ms |
17148 KB |
Output is correct |
40 |
Correct |
1625 ms |
16856 KB |
Output is correct |
41 |
Correct |
108 ms |
16012 KB |
Output is correct |
42 |
Correct |
103 ms |
16116 KB |
Output is correct |
43 |
Correct |
115 ms |
16116 KB |
Output is correct |
44 |
Correct |
156 ms |
16100 KB |
Output is correct |
45 |
Correct |
99 ms |
16188 KB |
Output is correct |
46 |
Correct |
141 ms |
15956 KB |
Output is correct |
47 |
Correct |
136 ms |
16152 KB |
Output is correct |
48 |
Correct |
87 ms |
16112 KB |
Output is correct |
49 |
Correct |
96 ms |
15836 KB |
Output is correct |
50 |
Correct |
106 ms |
16216 KB |
Output is correct |
51 |
Correct |
114 ms |
16004 KB |
Output is correct |
52 |
Correct |
140 ms |
16116 KB |
Output is correct |
53 |
Correct |
100 ms |
16116 KB |
Output is correct |
54 |
Correct |
143 ms |
16504 KB |
Output is correct |
55 |
Correct |
140 ms |
16056 KB |
Output is correct |
56 |
Correct |
106 ms |
15836 KB |
Output is correct |
57 |
Correct |
99 ms |
15988 KB |
Output is correct |
58 |
Correct |
97 ms |
15860 KB |
Output is correct |
59 |
Correct |
110 ms |
16140 KB |
Output is correct |
60 |
Correct |
141 ms |
16100 KB |
Output is correct |
61 |
Correct |
108 ms |
16120 KB |
Output is correct |
62 |
Correct |
132 ms |
15940 KB |
Output is correct |
63 |
Correct |
140 ms |
16048 KB |
Output is correct |
64 |
Correct |
95 ms |
15864 KB |
Output is correct |
65 |
Correct |
122 ms |
16156 KB |
Output is correct |
66 |
Correct |
132 ms |
16116 KB |
Output is correct |
67 |
Correct |
118 ms |
16116 KB |
Output is correct |
68 |
Correct |
127 ms |
16020 KB |
Output is correct |
69 |
Correct |
132 ms |
16116 KB |
Output is correct |
70 |
Correct |
139 ms |
16068 KB |
Output is correct |
71 |
Correct |
1597 ms |
39800 KB |
Output is correct |
72 |
Correct |
1667 ms |
39756 KB |
Output is correct |
73 |
Correct |
1930 ms |
38248 KB |
Output is correct |
74 |
Execution timed out |
2073 ms |
34552 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |