#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 cnt0[N];
int cnt1[N];
int cnt2[N];
int ans=0;
string s;
void dfs(int x,int now,int n){
if(x>=n){
ans+=a[now];
return ;
}
dfs(x+1,now|(1<<cnt0[x]),n);
dfs(x+1,now,n);
}
void dfs2(int x,int flg,int now,int n){
if(x>=n) {
ans+=flg*dp1[now];
return ;
}
dfs2(x+1,flg,now|(1<<cnt1[x]),n);
dfs2(x+1,-flg,now,n);
}
void dfs3(int x,int flg,int now,int n){
if(x>=n){
ans+=flg*dp2[now];
return ;
}
dfs3(x+1,flg,now,n);
dfs3(x+1,-flg,now|(1<<cnt2[x]),n);
}
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;
int now1,now2,now3;
a=b=c=now1=now2=now3=0;
rep(i,0,n-1){
if(s[i]=='?') cnt0[a++]=i,now2|=(1<<i);
else if(s[i]=='1') cnt1[b++]=i,now1|=(1<<i),now3|=(1<<i);
else cnt2[c++]=i;
}
ans=0;
if(a<=min(b,c)){
dfs(0,now1,a);
}
else if(b<=min(a,c)){
dfs2(0,1,now2,b);
}
else{
dfs3(0,1,now3,c);
}
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;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 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 |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 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 |
209 ms |
10620 KB |
Output is correct |
12 |
Correct |
220 ms |
10284 KB |
Output is correct |
13 |
Correct |
249 ms |
9616 KB |
Output is correct |
14 |
Correct |
249 ms |
10068 KB |
Output is correct |
15 |
Correct |
237 ms |
11192 KB |
Output is correct |
16 |
Correct |
250 ms |
10312 KB |
Output is correct |
17 |
Correct |
267 ms |
10340 KB |
Output is correct |
18 |
Correct |
181 ms |
12248 KB |
Output is correct |
19 |
Correct |
200 ms |
9148 KB |
Output is correct |
20 |
Correct |
237 ms |
10820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 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 |
209 ms |
10620 KB |
Output is correct |
12 |
Correct |
220 ms |
10284 KB |
Output is correct |
13 |
Correct |
249 ms |
9616 KB |
Output is correct |
14 |
Correct |
249 ms |
10068 KB |
Output is correct |
15 |
Correct |
237 ms |
11192 KB |
Output is correct |
16 |
Correct |
250 ms |
10312 KB |
Output is correct |
17 |
Correct |
267 ms |
10340 KB |
Output is correct |
18 |
Correct |
181 ms |
12248 KB |
Output is correct |
19 |
Correct |
200 ms |
9148 KB |
Output is correct |
20 |
Correct |
237 ms |
10820 KB |
Output is correct |
21 |
Correct |
218 ms |
9656 KB |
Output is correct |
22 |
Correct |
219 ms |
9664 KB |
Output is correct |
23 |
Correct |
264 ms |
8800 KB |
Output is correct |
24 |
Correct |
265 ms |
8492 KB |
Output is correct |
25 |
Correct |
243 ms |
10524 KB |
Output is correct |
26 |
Correct |
275 ms |
9128 KB |
Output is correct |
27 |
Correct |
287 ms |
8732 KB |
Output is correct |
28 |
Correct |
175 ms |
11148 KB |
Output is correct |
29 |
Correct |
205 ms |
7008 KB |
Output is correct |
30 |
Correct |
235 ms |
9136 KB |
Output is correct |
31 |
Correct |
258 ms |
8908 KB |
Output is correct |
32 |
Correct |
295 ms |
8720 KB |
Output is correct |
33 |
Correct |
220 ms |
7548 KB |
Output is correct |
34 |
Correct |
292 ms |
7788 KB |
Output is correct |
35 |
Correct |
275 ms |
7724 KB |
Output is correct |
36 |
Correct |
166 ms |
5928 KB |
Output is correct |
37 |
Correct |
217 ms |
7968 KB |
Output is correct |
38 |
Correct |
216 ms |
5480 KB |
Output is correct |
39 |
Correct |
261 ms |
6688 KB |
Output is correct |
40 |
Correct |
256 ms |
5892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 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 |
39 ms |
16088 KB |
Output is correct |
12 |
Correct |
43 ms |
16084 KB |
Output is correct |
13 |
Correct |
53 ms |
15952 KB |
Output is correct |
14 |
Correct |
46 ms |
15948 KB |
Output is correct |
15 |
Correct |
39 ms |
16080 KB |
Output is correct |
16 |
Correct |
58 ms |
16068 KB |
Output is correct |
17 |
Correct |
55 ms |
16248 KB |
Output is correct |
18 |
Correct |
35 ms |
16236 KB |
Output is correct |
19 |
Correct |
40 ms |
15828 KB |
Output is correct |
20 |
Correct |
38 ms |
16088 KB |
Output is correct |
21 |
Correct |
44 ms |
16072 KB |
Output is correct |
22 |
Correct |
49 ms |
16052 KB |
Output is correct |
23 |
Correct |
51 ms |
15980 KB |
Output is correct |
24 |
Correct |
60 ms |
16036 KB |
Output is correct |
25 |
Correct |
53 ms |
15964 KB |
Output is correct |
26 |
Correct |
31 ms |
15852 KB |
Output is correct |
27 |
Correct |
36 ms |
15984 KB |
Output is correct |
28 |
Correct |
45 ms |
15832 KB |
Output is correct |
29 |
Correct |
45 ms |
15912 KB |
Output is correct |
30 |
Correct |
50 ms |
15980 KB |
Output is correct |
31 |
Correct |
40 ms |
15944 KB |
Output is correct |
32 |
Correct |
55 ms |
15980 KB |
Output is correct |
33 |
Correct |
53 ms |
15944 KB |
Output is correct |
34 |
Correct |
33 ms |
15828 KB |
Output is correct |
35 |
Correct |
48 ms |
16036 KB |
Output is correct |
36 |
Correct |
47 ms |
16020 KB |
Output is correct |
37 |
Correct |
48 ms |
15956 KB |
Output is correct |
38 |
Correct |
58 ms |
15956 KB |
Output is correct |
39 |
Correct |
64 ms |
16012 KB |
Output is correct |
40 |
Correct |
51 ms |
16012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 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 |
209 ms |
10620 KB |
Output is correct |
12 |
Correct |
220 ms |
10284 KB |
Output is correct |
13 |
Correct |
249 ms |
9616 KB |
Output is correct |
14 |
Correct |
249 ms |
10068 KB |
Output is correct |
15 |
Correct |
237 ms |
11192 KB |
Output is correct |
16 |
Correct |
250 ms |
10312 KB |
Output is correct |
17 |
Correct |
267 ms |
10340 KB |
Output is correct |
18 |
Correct |
181 ms |
12248 KB |
Output is correct |
19 |
Correct |
200 ms |
9148 KB |
Output is correct |
20 |
Correct |
237 ms |
10820 KB |
Output is correct |
21 |
Correct |
218 ms |
9656 KB |
Output is correct |
22 |
Correct |
219 ms |
9664 KB |
Output is correct |
23 |
Correct |
264 ms |
8800 KB |
Output is correct |
24 |
Correct |
265 ms |
8492 KB |
Output is correct |
25 |
Correct |
243 ms |
10524 KB |
Output is correct |
26 |
Correct |
275 ms |
9128 KB |
Output is correct |
27 |
Correct |
287 ms |
8732 KB |
Output is correct |
28 |
Correct |
175 ms |
11148 KB |
Output is correct |
29 |
Correct |
205 ms |
7008 KB |
Output is correct |
30 |
Correct |
235 ms |
9136 KB |
Output is correct |
31 |
Correct |
258 ms |
8908 KB |
Output is correct |
32 |
Correct |
295 ms |
8720 KB |
Output is correct |
33 |
Correct |
220 ms |
7548 KB |
Output is correct |
34 |
Correct |
292 ms |
7788 KB |
Output is correct |
35 |
Correct |
275 ms |
7724 KB |
Output is correct |
36 |
Correct |
166 ms |
5928 KB |
Output is correct |
37 |
Correct |
217 ms |
7968 KB |
Output is correct |
38 |
Correct |
216 ms |
5480 KB |
Output is correct |
39 |
Correct |
261 ms |
6688 KB |
Output is correct |
40 |
Correct |
256 ms |
5892 KB |
Output is correct |
41 |
Correct |
39 ms |
16088 KB |
Output is correct |
42 |
Correct |
43 ms |
16084 KB |
Output is correct |
43 |
Correct |
53 ms |
15952 KB |
Output is correct |
44 |
Correct |
46 ms |
15948 KB |
Output is correct |
45 |
Correct |
39 ms |
16080 KB |
Output is correct |
46 |
Correct |
58 ms |
16068 KB |
Output is correct |
47 |
Correct |
55 ms |
16248 KB |
Output is correct |
48 |
Correct |
35 ms |
16236 KB |
Output is correct |
49 |
Correct |
40 ms |
15828 KB |
Output is correct |
50 |
Correct |
38 ms |
16088 KB |
Output is correct |
51 |
Correct |
44 ms |
16072 KB |
Output is correct |
52 |
Correct |
49 ms |
16052 KB |
Output is correct |
53 |
Correct |
51 ms |
15980 KB |
Output is correct |
54 |
Correct |
60 ms |
16036 KB |
Output is correct |
55 |
Correct |
53 ms |
15964 KB |
Output is correct |
56 |
Correct |
31 ms |
15852 KB |
Output is correct |
57 |
Correct |
36 ms |
15984 KB |
Output is correct |
58 |
Correct |
45 ms |
15832 KB |
Output is correct |
59 |
Correct |
45 ms |
15912 KB |
Output is correct |
60 |
Correct |
50 ms |
15980 KB |
Output is correct |
61 |
Correct |
40 ms |
15944 KB |
Output is correct |
62 |
Correct |
55 ms |
15980 KB |
Output is correct |
63 |
Correct |
53 ms |
15944 KB |
Output is correct |
64 |
Correct |
33 ms |
15828 KB |
Output is correct |
65 |
Correct |
48 ms |
16036 KB |
Output is correct |
66 |
Correct |
47 ms |
16020 KB |
Output is correct |
67 |
Correct |
48 ms |
15956 KB |
Output is correct |
68 |
Correct |
58 ms |
15956 KB |
Output is correct |
69 |
Correct |
64 ms |
16012 KB |
Output is correct |
70 |
Correct |
51 ms |
16012 KB |
Output is correct |
71 |
Correct |
335 ms |
40352 KB |
Output is correct |
72 |
Correct |
340 ms |
40544 KB |
Output is correct |
73 |
Correct |
442 ms |
38980 KB |
Output is correct |
74 |
Correct |
589 ms |
39288 KB |
Output is correct |
75 |
Correct |
372 ms |
41208 KB |
Output is correct |
76 |
Correct |
794 ms |
39588 KB |
Output is correct |
77 |
Correct |
661 ms |
39552 KB |
Output is correct |
78 |
Correct |
217 ms |
43392 KB |
Output is correct |
79 |
Correct |
321 ms |
37312 KB |
Output is correct |
80 |
Correct |
361 ms |
40372 KB |
Output is correct |
81 |
Correct |
487 ms |
40248 KB |
Output is correct |
82 |
Correct |
545 ms |
39208 KB |
Output is correct |
83 |
Correct |
380 ms |
38600 KB |
Output is correct |
84 |
Correct |
635 ms |
39232 KB |
Output is correct |
85 |
Correct |
720 ms |
39500 KB |
Output is correct |
86 |
Correct |
254 ms |
37292 KB |
Output is correct |
87 |
Correct |
325 ms |
40116 KB |
Output is correct |
88 |
Correct |
334 ms |
37352 KB |
Output is correct |
89 |
Correct |
482 ms |
38904 KB |
Output is correct |
90 |
Correct |
649 ms |
39348 KB |
Output is correct |
91 |
Correct |
349 ms |
38152 KB |
Output is correct |
92 |
Correct |
789 ms |
39544 KB |
Output is correct |
93 |
Correct |
640 ms |
39464 KB |
Output is correct |
94 |
Correct |
205 ms |
37216 KB |
Output is correct |
95 |
Correct |
548 ms |
39420 KB |
Output is correct |
96 |
Correct |
558 ms |
39408 KB |
Output is correct |
97 |
Correct |
563 ms |
39384 KB |
Output is correct |
98 |
Correct |
552 ms |
39380 KB |
Output is correct |
99 |
Correct |
575 ms |
39452 KB |
Output is correct |
100 |
Correct |
557 ms |
39380 KB |
Output is correct |