#include <bits/stdc++.h>
#pragma GCC optimize("O3,no-stack-protector,unroll-loops")
#pragma GCC target("avx2,fma")
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define S second
#define F first
#define pb push_back
#define SZ(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define err(x) cout<<#x<<": "<<x<<'\n';
using namespace std;
const int Q=1e6+4;
const int N=1050000;
int ans[Q];
int A[N];
char qu[Q][21];
int l,q;
int n;
int pw[21];
int Lg[N];
int F[N];
void solve(){
FOR(i,0,n-1) F[i]=A[i];
FOR(i,0,l-1) FOR(msk,0,n-1)
if(msk&pw[i]) F[msk]+=F[msk^pw[i]];
int dp0[N];
FOR(i,0,n-1) dp0[i]=F[i^(n-1)];
FOR(i,0,q-1) if(ans[i]==-1){
int tq(0),t[2]={};
FOR(j,0,l-1) if(qu[i][j]=='?') tq++; else t[qu[i][j]-'0']++;
if(tq<=7){
ans[i]=0;
vector<int> qq;
int K=0;
FOR(j,0,l-1) if(qu[i][j]=='?') qq.pb(j); else if(qu[i][j]=='1') K+=pw[j];
FOR(j,0,pw[tq]-1){
int J=j;
int k=K;
while(J){
k+=pw[qq[Lg[(J&(-J))]]];
J-=(J&(-J));
}
ans[i]+=A[k];
}
}else if(t[0]>=t[1]){
ans[i]=0;
vector<int> qq;
int K=0;
FOR(j,0,l-1) if(qu[i][j]=='0') K+=pw[j]; else if(qu[i][j]=='1') qq.pb(j);
FOR(j,0,pw[t[1]]-1){
int J=j;
int k=K;
int t=0;
while(J){
k+=pw[qq[Lg[(J&(-J))]]];
t++;
J-=(J&(-J));
}
if(t&1) ans[i]-=dp0[k];
else ans[i]+=dp0[k];
}
}
}
}
int32_t main() {
/*ios_base::sync_with_stdio(0);*/ cin.tie(0);
pw[0]=1; FOR(i,1,20) pw[i]=2*pw[i-1];
FOR(i,0,20) Lg[pw[i]]=i;
cin>>l>>q;
n=pw[l];
FOR(i,0,q) ans[i]=-1;
//string s0; cin>>s0;
getchar();
FOR(i,0,n-1) A[i]=getchar()-'0'; getchar();
FOR(i,0,q-1){
//string x0; cin>>x0; qu.pb(x0);
FOR(j,0,l-1) qu[i][j]=getchar();
FOR(j,0,l-1){
if(((l-1)-j)<=j) break;
swap(qu[i][j],qu[i][((l-1)-j)]);
}
getchar();
}
solve();
FOR(i,0,n-1){
if((i^(n-1))<=i) break;
swap(A[i],A[(i^(n-1))]);
}
FOR(i,0,q-1){
FOR(j,0,l-1) if(qu[i][j]!='?') qu[i][j]=((1-(qu[i][j]-'0'))+'0');
}
solve();
FOR(i,0,q-1) cout<<ans[i]<<'\n';
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int32_t main()':
snake_escaping.cpp:4:20: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
4 | #define FOR(i,a,b) for(int i=a;i<=b;i++)
| ^~~
snake_escaping.cpp:79:2: note: in expansion of macro 'FOR'
79 | FOR(i,0,n-1) A[i]=getchar()-'0'; getchar();
| ^~~
snake_escaping.cpp:79:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
79 | FOR(i,0,n-1) A[i]=getchar()-'0'; getchar();
| ^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4428 KB |
Output is correct |
2 |
Correct |
4 ms |
4428 KB |
Output is correct |
3 |
Correct |
3 ms |
4468 KB |
Output is correct |
4 |
Correct |
2 ms |
4428 KB |
Output is correct |
5 |
Correct |
3 ms |
4472 KB |
Output is correct |
6 |
Correct |
3 ms |
4428 KB |
Output is correct |
7 |
Correct |
2 ms |
4428 KB |
Output is correct |
8 |
Correct |
2 ms |
4456 KB |
Output is correct |
9 |
Correct |
2 ms |
4428 KB |
Output is correct |
10 |
Correct |
3 ms |
4428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4428 KB |
Output is correct |
2 |
Correct |
4 ms |
4428 KB |
Output is correct |
3 |
Correct |
3 ms |
4468 KB |
Output is correct |
4 |
Correct |
2 ms |
4428 KB |
Output is correct |
5 |
Correct |
3 ms |
4472 KB |
Output is correct |
6 |
Correct |
3 ms |
4428 KB |
Output is correct |
7 |
Correct |
2 ms |
4428 KB |
Output is correct |
8 |
Correct |
2 ms |
4456 KB |
Output is correct |
9 |
Correct |
2 ms |
4428 KB |
Output is correct |
10 |
Correct |
3 ms |
4428 KB |
Output is correct |
11 |
Correct |
590 ms |
43664 KB |
Output is correct |
12 |
Correct |
679 ms |
43372 KB |
Output is correct |
13 |
Correct |
394 ms |
42604 KB |
Output is correct |
14 |
Correct |
405 ms |
42784 KB |
Output is correct |
15 |
Correct |
770 ms |
43652 KB |
Output is correct |
16 |
Correct |
480 ms |
42784 KB |
Output is correct |
17 |
Correct |
494 ms |
42820 KB |
Output is correct |
18 |
Correct |
191 ms |
44708 KB |
Output is correct |
19 |
Correct |
243 ms |
41736 KB |
Output is correct |
20 |
Correct |
650 ms |
43312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4428 KB |
Output is correct |
2 |
Correct |
4 ms |
4428 KB |
Output is correct |
3 |
Correct |
3 ms |
4468 KB |
Output is correct |
4 |
Correct |
2 ms |
4428 KB |
Output is correct |
5 |
Correct |
3 ms |
4472 KB |
Output is correct |
6 |
Correct |
3 ms |
4428 KB |
Output is correct |
7 |
Correct |
2 ms |
4428 KB |
Output is correct |
8 |
Correct |
2 ms |
4456 KB |
Output is correct |
9 |
Correct |
2 ms |
4428 KB |
Output is correct |
10 |
Correct |
3 ms |
4428 KB |
Output is correct |
11 |
Correct |
590 ms |
43664 KB |
Output is correct |
12 |
Correct |
679 ms |
43372 KB |
Output is correct |
13 |
Correct |
394 ms |
42604 KB |
Output is correct |
14 |
Correct |
405 ms |
42784 KB |
Output is correct |
15 |
Correct |
770 ms |
43652 KB |
Output is correct |
16 |
Correct |
480 ms |
42784 KB |
Output is correct |
17 |
Correct |
494 ms |
42820 KB |
Output is correct |
18 |
Correct |
191 ms |
44708 KB |
Output is correct |
19 |
Correct |
243 ms |
41736 KB |
Output is correct |
20 |
Correct |
650 ms |
43312 KB |
Output is correct |
21 |
Correct |
1410 ms |
46780 KB |
Output is correct |
22 |
Correct |
744 ms |
46796 KB |
Output is correct |
23 |
Correct |
554 ms |
45828 KB |
Output is correct |
24 |
Correct |
504 ms |
45700 KB |
Output is correct |
25 |
Correct |
440 ms |
47676 KB |
Output is correct |
26 |
Correct |
656 ms |
46168 KB |
Output is correct |
27 |
Correct |
647 ms |
46064 KB |
Output is correct |
28 |
Correct |
214 ms |
48692 KB |
Output is correct |
29 |
Correct |
279 ms |
44688 KB |
Output is correct |
30 |
Correct |
757 ms |
46788 KB |
Output is correct |
31 |
Correct |
839 ms |
46648 KB |
Output is correct |
32 |
Correct |
648 ms |
46684 KB |
Output is correct |
33 |
Correct |
414 ms |
45500 KB |
Output is correct |
34 |
Correct |
500 ms |
45628 KB |
Output is correct |
35 |
Correct |
647 ms |
46104 KB |
Output is correct |
36 |
Correct |
233 ms |
44688 KB |
Output is correct |
37 |
Correct |
805 ms |
46648 KB |
Output is correct |
38 |
Correct |
294 ms |
44800 KB |
Output is correct |
39 |
Correct |
522 ms |
45900 KB |
Output is correct |
40 |
Correct |
498 ms |
45636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4428 KB |
Output is correct |
2 |
Correct |
4 ms |
4428 KB |
Output is correct |
3 |
Correct |
3 ms |
4468 KB |
Output is correct |
4 |
Correct |
2 ms |
4428 KB |
Output is correct |
5 |
Correct |
3 ms |
4472 KB |
Output is correct |
6 |
Correct |
3 ms |
4428 KB |
Output is correct |
7 |
Correct |
2 ms |
4428 KB |
Output is correct |
8 |
Correct |
2 ms |
4456 KB |
Output is correct |
9 |
Correct |
2 ms |
4428 KB |
Output is correct |
10 |
Correct |
3 ms |
4428 KB |
Output is correct |
11 |
Correct |
69 ms |
16148 KB |
Output is correct |
12 |
Correct |
70 ms |
16204 KB |
Output is correct |
13 |
Correct |
85 ms |
16076 KB |
Output is correct |
14 |
Correct |
119 ms |
16080 KB |
Output is correct |
15 |
Correct |
69 ms |
16212 KB |
Output is correct |
16 |
Correct |
97 ms |
16088 KB |
Output is correct |
17 |
Correct |
90 ms |
16096 KB |
Output is correct |
18 |
Correct |
50 ms |
16332 KB |
Output is correct |
19 |
Correct |
56 ms |
15956 KB |
Output is correct |
20 |
Correct |
73 ms |
16192 KB |
Output is correct |
21 |
Correct |
78 ms |
16092 KB |
Output is correct |
22 |
Correct |
116 ms |
16148 KB |
Output is correct |
23 |
Correct |
67 ms |
16100 KB |
Output is correct |
24 |
Correct |
90 ms |
16180 KB |
Output is correct |
25 |
Correct |
92 ms |
16100 KB |
Output is correct |
26 |
Correct |
54 ms |
15948 KB |
Output is correct |
27 |
Correct |
65 ms |
16180 KB |
Output is correct |
28 |
Correct |
57 ms |
15964 KB |
Output is correct |
29 |
Correct |
80 ms |
16096 KB |
Output is correct |
30 |
Correct |
86 ms |
16096 KB |
Output is correct |
31 |
Correct |
76 ms |
16068 KB |
Output is correct |
32 |
Correct |
98 ms |
16068 KB |
Output is correct |
33 |
Correct |
92 ms |
16116 KB |
Output is correct |
34 |
Correct |
52 ms |
15988 KB |
Output is correct |
35 |
Correct |
81 ms |
16112 KB |
Output is correct |
36 |
Correct |
81 ms |
16084 KB |
Output is correct |
37 |
Correct |
82 ms |
16112 KB |
Output is correct |
38 |
Correct |
80 ms |
16080 KB |
Output is correct |
39 |
Correct |
84 ms |
16096 KB |
Output is correct |
40 |
Correct |
91 ms |
16076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4428 KB |
Output is correct |
2 |
Correct |
4 ms |
4428 KB |
Output is correct |
3 |
Correct |
3 ms |
4468 KB |
Output is correct |
4 |
Correct |
2 ms |
4428 KB |
Output is correct |
5 |
Correct |
3 ms |
4472 KB |
Output is correct |
6 |
Correct |
3 ms |
4428 KB |
Output is correct |
7 |
Correct |
2 ms |
4428 KB |
Output is correct |
8 |
Correct |
2 ms |
4456 KB |
Output is correct |
9 |
Correct |
2 ms |
4428 KB |
Output is correct |
10 |
Correct |
3 ms |
4428 KB |
Output is correct |
11 |
Correct |
590 ms |
43664 KB |
Output is correct |
12 |
Correct |
679 ms |
43372 KB |
Output is correct |
13 |
Correct |
394 ms |
42604 KB |
Output is correct |
14 |
Correct |
405 ms |
42784 KB |
Output is correct |
15 |
Correct |
770 ms |
43652 KB |
Output is correct |
16 |
Correct |
480 ms |
42784 KB |
Output is correct |
17 |
Correct |
494 ms |
42820 KB |
Output is correct |
18 |
Correct |
191 ms |
44708 KB |
Output is correct |
19 |
Correct |
243 ms |
41736 KB |
Output is correct |
20 |
Correct |
650 ms |
43312 KB |
Output is correct |
21 |
Correct |
1410 ms |
46780 KB |
Output is correct |
22 |
Correct |
744 ms |
46796 KB |
Output is correct |
23 |
Correct |
554 ms |
45828 KB |
Output is correct |
24 |
Correct |
504 ms |
45700 KB |
Output is correct |
25 |
Correct |
440 ms |
47676 KB |
Output is correct |
26 |
Correct |
656 ms |
46168 KB |
Output is correct |
27 |
Correct |
647 ms |
46064 KB |
Output is correct |
28 |
Correct |
214 ms |
48692 KB |
Output is correct |
29 |
Correct |
279 ms |
44688 KB |
Output is correct |
30 |
Correct |
757 ms |
46788 KB |
Output is correct |
31 |
Correct |
839 ms |
46648 KB |
Output is correct |
32 |
Correct |
648 ms |
46684 KB |
Output is correct |
33 |
Correct |
414 ms |
45500 KB |
Output is correct |
34 |
Correct |
500 ms |
45628 KB |
Output is correct |
35 |
Correct |
647 ms |
46104 KB |
Output is correct |
36 |
Correct |
233 ms |
44688 KB |
Output is correct |
37 |
Correct |
805 ms |
46648 KB |
Output is correct |
38 |
Correct |
294 ms |
44800 KB |
Output is correct |
39 |
Correct |
522 ms |
45900 KB |
Output is correct |
40 |
Correct |
498 ms |
45636 KB |
Output is correct |
41 |
Correct |
69 ms |
16148 KB |
Output is correct |
42 |
Correct |
70 ms |
16204 KB |
Output is correct |
43 |
Correct |
85 ms |
16076 KB |
Output is correct |
44 |
Correct |
119 ms |
16080 KB |
Output is correct |
45 |
Correct |
69 ms |
16212 KB |
Output is correct |
46 |
Correct |
97 ms |
16088 KB |
Output is correct |
47 |
Correct |
90 ms |
16096 KB |
Output is correct |
48 |
Correct |
50 ms |
16332 KB |
Output is correct |
49 |
Correct |
56 ms |
15956 KB |
Output is correct |
50 |
Correct |
73 ms |
16192 KB |
Output is correct |
51 |
Correct |
78 ms |
16092 KB |
Output is correct |
52 |
Correct |
116 ms |
16148 KB |
Output is correct |
53 |
Correct |
67 ms |
16100 KB |
Output is correct |
54 |
Correct |
90 ms |
16180 KB |
Output is correct |
55 |
Correct |
92 ms |
16100 KB |
Output is correct |
56 |
Correct |
54 ms |
15948 KB |
Output is correct |
57 |
Correct |
65 ms |
16180 KB |
Output is correct |
58 |
Correct |
57 ms |
15964 KB |
Output is correct |
59 |
Correct |
80 ms |
16096 KB |
Output is correct |
60 |
Correct |
86 ms |
16096 KB |
Output is correct |
61 |
Correct |
76 ms |
16068 KB |
Output is correct |
62 |
Correct |
98 ms |
16068 KB |
Output is correct |
63 |
Correct |
92 ms |
16116 KB |
Output is correct |
64 |
Correct |
52 ms |
15988 KB |
Output is correct |
65 |
Correct |
81 ms |
16112 KB |
Output is correct |
66 |
Correct |
81 ms |
16084 KB |
Output is correct |
67 |
Correct |
82 ms |
16112 KB |
Output is correct |
68 |
Correct |
80 ms |
16080 KB |
Output is correct |
69 |
Correct |
84 ms |
16096 KB |
Output is correct |
70 |
Correct |
91 ms |
16076 KB |
Output is correct |
71 |
Correct |
677 ms |
63784 KB |
Output is correct |
72 |
Correct |
706 ms |
63832 KB |
Output is correct |
73 |
Correct |
888 ms |
62264 KB |
Output is correct |
74 |
Correct |
1621 ms |
62680 KB |
Output is correct |
75 |
Correct |
685 ms |
64616 KB |
Output is correct |
76 |
Correct |
1255 ms |
62940 KB |
Output is correct |
77 |
Correct |
1118 ms |
62780 KB |
Output is correct |
78 |
Correct |
309 ms |
65536 KB |
Output is correct |
79 |
Correct |
438 ms |
60612 KB |
Output is correct |
80 |
Correct |
777 ms |
63912 KB |
Output is correct |
81 |
Correct |
817 ms |
63668 KB |
Output is correct |
82 |
Correct |
1715 ms |
62776 KB |
Output is correct |
83 |
Correct |
646 ms |
61868 KB |
Output is correct |
84 |
Correct |
1078 ms |
62696 KB |
Output is correct |
85 |
Correct |
1098 ms |
62900 KB |
Output is correct |
86 |
Correct |
347 ms |
60716 KB |
Output is correct |
87 |
Correct |
573 ms |
63680 KB |
Output is correct |
88 |
Correct |
439 ms |
60624 KB |
Output is correct |
89 |
Correct |
893 ms |
62384 KB |
Output is correct |
90 |
Correct |
1075 ms |
62756 KB |
Output is correct |
91 |
Correct |
651 ms |
61948 KB |
Output is correct |
92 |
Correct |
1256 ms |
62692 KB |
Output is correct |
93 |
Correct |
1137 ms |
62600 KB |
Output is correct |
94 |
Correct |
343 ms |
60588 KB |
Output is correct |
95 |
Correct |
917 ms |
62800 KB |
Output is correct |
96 |
Correct |
921 ms |
62612 KB |
Output is correct |
97 |
Correct |
927 ms |
62668 KB |
Output is correct |
98 |
Correct |
912 ms |
62528 KB |
Output is correct |
99 |
Correct |
901 ms |
62552 KB |
Output is correct |
100 |
Correct |
913 ms |
62492 KB |
Output is correct |