#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include<bits/stdc++.h>
#define quick ios::sync_with_stdio(0);cin.tie(0);
#define rep(x,a,b) for(int x=a;x<=b;x++)
#define repd(x,a,b) for(int x=a;x>=b;x--)
#define lowbit(x) (x&-x)
#define sz(x) (int)(x.size())
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define mp make_pair
#define eb emplace_back
using namespace std;
typedef complex<int> P;
#define X real()
#define Y imag()
typedef pair<int,int> pii;
void debug(){
cout<<"\n";
}
template <class T,class ... U >
void debug(T a, U ... b){
cout<<a<<" ",debug(b...);
}
const int M=20;
const int N=(1<<M)+7;
const int INF=1e18;
int dp1[N];
int dp2[N];
int a[N];
int now=0;
int ans=0;
string s;
void dfs(int x,int n){
if(x>=n){
ans+=a[now];
return ;
}
if(s[x]=='0'||s[x]=='?'){
dfs(x+1,n);
}
if(s[x]=='1'||s[x]=='?'){
now+=(1<<x);
dfs(x+1,n);
now-=(1<<x);
}
}
int change=0;
void dfs2(int x,int n){
if(x>=n){
if(change) ans-=dp1[now];
else ans+=dp1[now];
return ;
}
if(s[x]=='1'){
now+=(1<<x);
dfs2(x+1,n);
now-=(1<<x);
change^=1;
dfs2(x+1,n);
change^=1;
}
else if(s[x]=='?'){
now+=(1<<x);
dfs2(x+1,n);
now-=(1<<x);
}
else{
dfs2(x+1,n);
}
}
void dfs3(int x,int n){
if(x>=n){
if(change) ans-=dp2[now];
else ans+=dp2[now];
return ;
}
if(s[x]=='0'){
change^=1;
now+=(1<<x);
dfs3(x+1,n);
change^=1;
now-=(1<<x);
dfs3(x+1,n);
}
else if(s[x]=='?'){
dfs3(x+1,n);
}
else{
now+=(1<<x);
dfs3(x+1,n);
now-=(1<<x);
}
}
signed main(){
quick
int n,q;
cin>>n>>q;
string S;
cin>>S;
rep(i,0,(1<<n)-1){
dp1[i]=dp2[i]=a[i]=S[i]-'0';
}
rep(i,0,n-1){
rep(mask,0,(1<<n)-1){
if(mask&(1<<i)){
dp1[mask]+=dp1[mask^(1<<i)];
}
else dp2[mask]+=dp2[mask^(1<<i)];
}
}
while(q--){
cin>>s;
reverse(all(s));
int a,b,c;
for(char _c:s){
if(_c=='?') a++;
else if(_c=='1') b++;
else c++;
}
ans=change=now=0;
if(a<=min(b,c)){
dfs(0,n);
}
else if(b<=min(a,c)){
dfs2(0,n);
}
else{
dfs3(0,n);
}
cout<<ans<<"\n";
}
return 0;
}
Compilation message
snake_escaping.cpp:28:15: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
28 | const int INF=1e18;
| ^~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:118:11: warning: 'c' may be used uninitialized in this function [-Wmaybe-uninitialized]
118 | int a,b,c;
| ^
snake_escaping.cpp:118:9: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
118 | int a,b,c;
| ^
snake_escaping.cpp:118:7: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]
118 | int a,b,c;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
252 ms |
15032 KB |
Output is correct |
12 |
Correct |
303 ms |
14668 KB |
Output is correct |
13 |
Correct |
581 ms |
13908 KB |
Output is correct |
14 |
Correct |
604 ms |
14048 KB |
Output is correct |
15 |
Correct |
370 ms |
15136 KB |
Output is correct |
16 |
Correct |
518 ms |
14272 KB |
Output is correct |
17 |
Correct |
642 ms |
14284 KB |
Output is correct |
18 |
Correct |
186 ms |
16028 KB |
Output is correct |
19 |
Correct |
295 ms |
13028 KB |
Output is correct |
20 |
Correct |
273 ms |
14652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
252 ms |
15032 KB |
Output is correct |
12 |
Correct |
303 ms |
14668 KB |
Output is correct |
13 |
Correct |
581 ms |
13908 KB |
Output is correct |
14 |
Correct |
604 ms |
14048 KB |
Output is correct |
15 |
Correct |
370 ms |
15136 KB |
Output is correct |
16 |
Correct |
518 ms |
14272 KB |
Output is correct |
17 |
Correct |
642 ms |
14284 KB |
Output is correct |
18 |
Correct |
186 ms |
16028 KB |
Output is correct |
19 |
Correct |
295 ms |
13028 KB |
Output is correct |
20 |
Correct |
273 ms |
14652 KB |
Output is correct |
21 |
Correct |
291 ms |
18300 KB |
Output is correct |
22 |
Correct |
479 ms |
18336 KB |
Output is correct |
23 |
Correct |
974 ms |
17272 KB |
Output is correct |
24 |
Correct |
1032 ms |
17256 KB |
Output is correct |
25 |
Correct |
551 ms |
19224 KB |
Output is correct |
26 |
Correct |
856 ms |
17528 KB |
Output is correct |
27 |
Correct |
1135 ms |
17548 KB |
Output is correct |
28 |
Correct |
183 ms |
20000 KB |
Output is correct |
29 |
Correct |
429 ms |
16076 KB |
Output is correct |
30 |
Correct |
320 ms |
18404 KB |
Output is correct |
31 |
Correct |
764 ms |
18104 KB |
Output is correct |
32 |
Correct |
825 ms |
18028 KB |
Output is correct |
33 |
Correct |
589 ms |
16972 KB |
Output is correct |
34 |
Correct |
1158 ms |
17028 KB |
Output is correct |
35 |
Correct |
1173 ms |
17536 KB |
Output is correct |
36 |
Correct |
242 ms |
16024 KB |
Output is correct |
37 |
Correct |
399 ms |
18004 KB |
Output is correct |
38 |
Correct |
545 ms |
16168 KB |
Output is correct |
39 |
Correct |
789 ms |
17268 KB |
Output is correct |
40 |
Correct |
877 ms |
17180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
43 ms |
15980 KB |
Output is correct |
12 |
Correct |
1348 ms |
16264 KB |
Output is correct |
13 |
Correct |
821 ms |
16124 KB |
Output is correct |
14 |
Correct |
191 ms |
16000 KB |
Output is correct |
15 |
Correct |
69 ms |
16064 KB |
Output is correct |
16 |
Correct |
143 ms |
15976 KB |
Output is correct |
17 |
Correct |
279 ms |
15980 KB |
Output is correct |
18 |
Correct |
46 ms |
16160 KB |
Output is correct |
19 |
Correct |
899 ms |
15928 KB |
Output is correct |
20 |
Correct |
48 ms |
16032 KB |
Output is correct |
21 |
Correct |
117 ms |
16112 KB |
Output is correct |
22 |
Correct |
155 ms |
15908 KB |
Output is correct |
23 |
Correct |
96 ms |
15976 KB |
Output is correct |
24 |
Correct |
282 ms |
15944 KB |
Output is correct |
25 |
Correct |
325 ms |
15980 KB |
Output is correct |
26 |
Correct |
37 ms |
15856 KB |
Output is correct |
27 |
Correct |
470 ms |
16112 KB |
Output is correct |
28 |
Execution timed out |
2011 ms |
16140 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
252 ms |
15032 KB |
Output is correct |
12 |
Correct |
303 ms |
14668 KB |
Output is correct |
13 |
Correct |
581 ms |
13908 KB |
Output is correct |
14 |
Correct |
604 ms |
14048 KB |
Output is correct |
15 |
Correct |
370 ms |
15136 KB |
Output is correct |
16 |
Correct |
518 ms |
14272 KB |
Output is correct |
17 |
Correct |
642 ms |
14284 KB |
Output is correct |
18 |
Correct |
186 ms |
16028 KB |
Output is correct |
19 |
Correct |
295 ms |
13028 KB |
Output is correct |
20 |
Correct |
273 ms |
14652 KB |
Output is correct |
21 |
Correct |
291 ms |
18300 KB |
Output is correct |
22 |
Correct |
479 ms |
18336 KB |
Output is correct |
23 |
Correct |
974 ms |
17272 KB |
Output is correct |
24 |
Correct |
1032 ms |
17256 KB |
Output is correct |
25 |
Correct |
551 ms |
19224 KB |
Output is correct |
26 |
Correct |
856 ms |
17528 KB |
Output is correct |
27 |
Correct |
1135 ms |
17548 KB |
Output is correct |
28 |
Correct |
183 ms |
20000 KB |
Output is correct |
29 |
Correct |
429 ms |
16076 KB |
Output is correct |
30 |
Correct |
320 ms |
18404 KB |
Output is correct |
31 |
Correct |
764 ms |
18104 KB |
Output is correct |
32 |
Correct |
825 ms |
18028 KB |
Output is correct |
33 |
Correct |
589 ms |
16972 KB |
Output is correct |
34 |
Correct |
1158 ms |
17028 KB |
Output is correct |
35 |
Correct |
1173 ms |
17536 KB |
Output is correct |
36 |
Correct |
242 ms |
16024 KB |
Output is correct |
37 |
Correct |
399 ms |
18004 KB |
Output is correct |
38 |
Correct |
545 ms |
16168 KB |
Output is correct |
39 |
Correct |
789 ms |
17268 KB |
Output is correct |
40 |
Correct |
877 ms |
17180 KB |
Output is correct |
41 |
Correct |
43 ms |
15980 KB |
Output is correct |
42 |
Correct |
1348 ms |
16264 KB |
Output is correct |
43 |
Correct |
821 ms |
16124 KB |
Output is correct |
44 |
Correct |
191 ms |
16000 KB |
Output is correct |
45 |
Correct |
69 ms |
16064 KB |
Output is correct |
46 |
Correct |
143 ms |
15976 KB |
Output is correct |
47 |
Correct |
279 ms |
15980 KB |
Output is correct |
48 |
Correct |
46 ms |
16160 KB |
Output is correct |
49 |
Correct |
899 ms |
15928 KB |
Output is correct |
50 |
Correct |
48 ms |
16032 KB |
Output is correct |
51 |
Correct |
117 ms |
16112 KB |
Output is correct |
52 |
Correct |
155 ms |
15908 KB |
Output is correct |
53 |
Correct |
96 ms |
15976 KB |
Output is correct |
54 |
Correct |
282 ms |
15944 KB |
Output is correct |
55 |
Correct |
325 ms |
15980 KB |
Output is correct |
56 |
Correct |
37 ms |
15856 KB |
Output is correct |
57 |
Correct |
470 ms |
16112 KB |
Output is correct |
58 |
Execution timed out |
2011 ms |
16140 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |