#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,O3")
#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:3:20: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
3 | #define FOR(i,a,b) for(int i=a;i<=b;i++)
| ^~~
snake_escaping.cpp:78:2: note: in expansion of macro 'FOR'
78 | FOR(i,0,n-1) A[i]=getchar()-'0'; getchar();
| ^~~
snake_escaping.cpp:78:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
78 | FOR(i,0,n-1) A[i]=getchar()-'0'; getchar();
| ^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4428 KB |
Output is correct |
2 |
Correct |
2 ms |
4464 KB |
Output is correct |
3 |
Correct |
2 ms |
4428 KB |
Output is correct |
4 |
Correct |
2 ms |
4428 KB |
Output is correct |
5 |
Correct |
3 ms |
4428 KB |
Output is correct |
6 |
Correct |
2 ms |
4428 KB |
Output is correct |
7 |
Correct |
3 ms |
4468 KB |
Output is correct |
8 |
Correct |
2 ms |
4468 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 |
2 ms |
4428 KB |
Output is correct |
2 |
Correct |
2 ms |
4464 KB |
Output is correct |
3 |
Correct |
2 ms |
4428 KB |
Output is correct |
4 |
Correct |
2 ms |
4428 KB |
Output is correct |
5 |
Correct |
3 ms |
4428 KB |
Output is correct |
6 |
Correct |
2 ms |
4428 KB |
Output is correct |
7 |
Correct |
3 ms |
4468 KB |
Output is correct |
8 |
Correct |
2 ms |
4468 KB |
Output is correct |
9 |
Correct |
2 ms |
4428 KB |
Output is correct |
10 |
Correct |
3 ms |
4428 KB |
Output is correct |
11 |
Correct |
511 ms |
33028 KB |
Output is correct |
12 |
Correct |
592 ms |
32628 KB |
Output is correct |
13 |
Correct |
391 ms |
31860 KB |
Output is correct |
14 |
Correct |
411 ms |
32000 KB |
Output is correct |
15 |
Correct |
705 ms |
39484 KB |
Output is correct |
16 |
Correct |
527 ms |
41728 KB |
Output is correct |
17 |
Correct |
469 ms |
40696 KB |
Output is correct |
18 |
Correct |
187 ms |
44304 KB |
Output is correct |
19 |
Correct |
247 ms |
41664 KB |
Output is correct |
20 |
Correct |
607 ms |
43344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4428 KB |
Output is correct |
2 |
Correct |
2 ms |
4464 KB |
Output is correct |
3 |
Correct |
2 ms |
4428 KB |
Output is correct |
4 |
Correct |
2 ms |
4428 KB |
Output is correct |
5 |
Correct |
3 ms |
4428 KB |
Output is correct |
6 |
Correct |
2 ms |
4428 KB |
Output is correct |
7 |
Correct |
3 ms |
4468 KB |
Output is correct |
8 |
Correct |
2 ms |
4468 KB |
Output is correct |
9 |
Correct |
2 ms |
4428 KB |
Output is correct |
10 |
Correct |
3 ms |
4428 KB |
Output is correct |
11 |
Correct |
511 ms |
33028 KB |
Output is correct |
12 |
Correct |
592 ms |
32628 KB |
Output is correct |
13 |
Correct |
391 ms |
31860 KB |
Output is correct |
14 |
Correct |
411 ms |
32000 KB |
Output is correct |
15 |
Correct |
705 ms |
39484 KB |
Output is correct |
16 |
Correct |
527 ms |
41728 KB |
Output is correct |
17 |
Correct |
469 ms |
40696 KB |
Output is correct |
18 |
Correct |
187 ms |
44304 KB |
Output is correct |
19 |
Correct |
247 ms |
41664 KB |
Output is correct |
20 |
Correct |
607 ms |
43344 KB |
Output is correct |
21 |
Correct |
1144 ms |
44460 KB |
Output is correct |
22 |
Correct |
680 ms |
44740 KB |
Output is correct |
23 |
Correct |
525 ms |
45860 KB |
Output is correct |
24 |
Correct |
528 ms |
45596 KB |
Output is correct |
25 |
Correct |
473 ms |
47648 KB |
Output is correct |
26 |
Correct |
672 ms |
46076 KB |
Output is correct |
27 |
Correct |
626 ms |
46008 KB |
Output is correct |
28 |
Correct |
215 ms |
48616 KB |
Output is correct |
29 |
Correct |
310 ms |
44648 KB |
Output is correct |
30 |
Correct |
687 ms |
46740 KB |
Output is correct |
31 |
Correct |
789 ms |
46712 KB |
Output is correct |
32 |
Correct |
666 ms |
46620 KB |
Output is correct |
33 |
Correct |
417 ms |
45508 KB |
Output is correct |
34 |
Correct |
503 ms |
45600 KB |
Output is correct |
35 |
Correct |
621 ms |
46056 KB |
Output is correct |
36 |
Correct |
240 ms |
44660 KB |
Output is correct |
37 |
Correct |
743 ms |
46660 KB |
Output is correct |
38 |
Correct |
315 ms |
44592 KB |
Output is correct |
39 |
Correct |
511 ms |
45828 KB |
Output is correct |
40 |
Correct |
503 ms |
45600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4428 KB |
Output is correct |
2 |
Correct |
2 ms |
4464 KB |
Output is correct |
3 |
Correct |
2 ms |
4428 KB |
Output is correct |
4 |
Correct |
2 ms |
4428 KB |
Output is correct |
5 |
Correct |
3 ms |
4428 KB |
Output is correct |
6 |
Correct |
2 ms |
4428 KB |
Output is correct |
7 |
Correct |
3 ms |
4468 KB |
Output is correct |
8 |
Correct |
2 ms |
4468 KB |
Output is correct |
9 |
Correct |
2 ms |
4428 KB |
Output is correct |
10 |
Correct |
3 ms |
4428 KB |
Output is correct |
11 |
Correct |
80 ms |
16148 KB |
Output is correct |
12 |
Correct |
80 ms |
16188 KB |
Output is correct |
13 |
Correct |
95 ms |
16068 KB |
Output is correct |
14 |
Correct |
117 ms |
16144 KB |
Output is correct |
15 |
Correct |
79 ms |
16140 KB |
Output is correct |
16 |
Correct |
116 ms |
16068 KB |
Output is correct |
17 |
Correct |
100 ms |
16080 KB |
Output is correct |
18 |
Correct |
63 ms |
16324 KB |
Output is correct |
19 |
Correct |
66 ms |
15960 KB |
Output is correct |
20 |
Correct |
87 ms |
16220 KB |
Output is correct |
21 |
Correct |
89 ms |
16200 KB |
Output is correct |
22 |
Correct |
114 ms |
16076 KB |
Output is correct |
23 |
Correct |
77 ms |
16084 KB |
Output is correct |
24 |
Correct |
95 ms |
16148 KB |
Output is correct |
25 |
Correct |
100 ms |
16144 KB |
Output is correct |
26 |
Correct |
62 ms |
15940 KB |
Output is correct |
27 |
Correct |
72 ms |
16112 KB |
Output is correct |
28 |
Correct |
79 ms |
15944 KB |
Output is correct |
29 |
Correct |
89 ms |
16064 KB |
Output is correct |
30 |
Correct |
95 ms |
16140 KB |
Output is correct |
31 |
Correct |
76 ms |
16012 KB |
Output is correct |
32 |
Correct |
122 ms |
16068 KB |
Output is correct |
33 |
Correct |
99 ms |
16136 KB |
Output is correct |
34 |
Correct |
61 ms |
15984 KB |
Output is correct |
35 |
Correct |
90 ms |
16048 KB |
Output is correct |
36 |
Correct |
92 ms |
16068 KB |
Output is correct |
37 |
Correct |
89 ms |
16136 KB |
Output is correct |
38 |
Correct |
89 ms |
16076 KB |
Output is correct |
39 |
Correct |
89 ms |
16084 KB |
Output is correct |
40 |
Correct |
95 ms |
16068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4428 KB |
Output is correct |
2 |
Correct |
2 ms |
4464 KB |
Output is correct |
3 |
Correct |
2 ms |
4428 KB |
Output is correct |
4 |
Correct |
2 ms |
4428 KB |
Output is correct |
5 |
Correct |
3 ms |
4428 KB |
Output is correct |
6 |
Correct |
2 ms |
4428 KB |
Output is correct |
7 |
Correct |
3 ms |
4468 KB |
Output is correct |
8 |
Correct |
2 ms |
4468 KB |
Output is correct |
9 |
Correct |
2 ms |
4428 KB |
Output is correct |
10 |
Correct |
3 ms |
4428 KB |
Output is correct |
11 |
Correct |
511 ms |
33028 KB |
Output is correct |
12 |
Correct |
592 ms |
32628 KB |
Output is correct |
13 |
Correct |
391 ms |
31860 KB |
Output is correct |
14 |
Correct |
411 ms |
32000 KB |
Output is correct |
15 |
Correct |
705 ms |
39484 KB |
Output is correct |
16 |
Correct |
527 ms |
41728 KB |
Output is correct |
17 |
Correct |
469 ms |
40696 KB |
Output is correct |
18 |
Correct |
187 ms |
44304 KB |
Output is correct |
19 |
Correct |
247 ms |
41664 KB |
Output is correct |
20 |
Correct |
607 ms |
43344 KB |
Output is correct |
21 |
Correct |
1144 ms |
44460 KB |
Output is correct |
22 |
Correct |
680 ms |
44740 KB |
Output is correct |
23 |
Correct |
525 ms |
45860 KB |
Output is correct |
24 |
Correct |
528 ms |
45596 KB |
Output is correct |
25 |
Correct |
473 ms |
47648 KB |
Output is correct |
26 |
Correct |
672 ms |
46076 KB |
Output is correct |
27 |
Correct |
626 ms |
46008 KB |
Output is correct |
28 |
Correct |
215 ms |
48616 KB |
Output is correct |
29 |
Correct |
310 ms |
44648 KB |
Output is correct |
30 |
Correct |
687 ms |
46740 KB |
Output is correct |
31 |
Correct |
789 ms |
46712 KB |
Output is correct |
32 |
Correct |
666 ms |
46620 KB |
Output is correct |
33 |
Correct |
417 ms |
45508 KB |
Output is correct |
34 |
Correct |
503 ms |
45600 KB |
Output is correct |
35 |
Correct |
621 ms |
46056 KB |
Output is correct |
36 |
Correct |
240 ms |
44660 KB |
Output is correct |
37 |
Correct |
743 ms |
46660 KB |
Output is correct |
38 |
Correct |
315 ms |
44592 KB |
Output is correct |
39 |
Correct |
511 ms |
45828 KB |
Output is correct |
40 |
Correct |
503 ms |
45600 KB |
Output is correct |
41 |
Correct |
80 ms |
16148 KB |
Output is correct |
42 |
Correct |
80 ms |
16188 KB |
Output is correct |
43 |
Correct |
95 ms |
16068 KB |
Output is correct |
44 |
Correct |
117 ms |
16144 KB |
Output is correct |
45 |
Correct |
79 ms |
16140 KB |
Output is correct |
46 |
Correct |
116 ms |
16068 KB |
Output is correct |
47 |
Correct |
100 ms |
16080 KB |
Output is correct |
48 |
Correct |
63 ms |
16324 KB |
Output is correct |
49 |
Correct |
66 ms |
15960 KB |
Output is correct |
50 |
Correct |
87 ms |
16220 KB |
Output is correct |
51 |
Correct |
89 ms |
16200 KB |
Output is correct |
52 |
Correct |
114 ms |
16076 KB |
Output is correct |
53 |
Correct |
77 ms |
16084 KB |
Output is correct |
54 |
Correct |
95 ms |
16148 KB |
Output is correct |
55 |
Correct |
100 ms |
16144 KB |
Output is correct |
56 |
Correct |
62 ms |
15940 KB |
Output is correct |
57 |
Correct |
72 ms |
16112 KB |
Output is correct |
58 |
Correct |
79 ms |
15944 KB |
Output is correct |
59 |
Correct |
89 ms |
16064 KB |
Output is correct |
60 |
Correct |
95 ms |
16140 KB |
Output is correct |
61 |
Correct |
76 ms |
16012 KB |
Output is correct |
62 |
Correct |
122 ms |
16068 KB |
Output is correct |
63 |
Correct |
99 ms |
16136 KB |
Output is correct |
64 |
Correct |
61 ms |
15984 KB |
Output is correct |
65 |
Correct |
90 ms |
16048 KB |
Output is correct |
66 |
Correct |
92 ms |
16068 KB |
Output is correct |
67 |
Correct |
89 ms |
16136 KB |
Output is correct |
68 |
Correct |
89 ms |
16076 KB |
Output is correct |
69 |
Correct |
89 ms |
16084 KB |
Output is correct |
70 |
Correct |
95 ms |
16068 KB |
Output is correct |
71 |
Correct |
684 ms |
45632 KB |
Output is correct |
72 |
Correct |
666 ms |
45704 KB |
Output is correct |
73 |
Correct |
856 ms |
44124 KB |
Output is correct |
74 |
Correct |
1417 ms |
44596 KB |
Output is correct |
75 |
Correct |
679 ms |
46580 KB |
Output is correct |
76 |
Correct |
1220 ms |
44756 KB |
Output is correct |
77 |
Correct |
1099 ms |
44732 KB |
Output is correct |
78 |
Correct |
322 ms |
49620 KB |
Output is correct |
79 |
Correct |
431 ms |
42508 KB |
Output is correct |
80 |
Correct |
757 ms |
45636 KB |
Output is correct |
81 |
Correct |
856 ms |
45452 KB |
Output is correct |
82 |
Correct |
1499 ms |
44456 KB |
Output is correct |
83 |
Correct |
644 ms |
43624 KB |
Output is correct |
84 |
Correct |
1020 ms |
44400 KB |
Output is correct |
85 |
Correct |
1097 ms |
44636 KB |
Output is correct |
86 |
Correct |
357 ms |
42308 KB |
Output is correct |
87 |
Correct |
583 ms |
45384 KB |
Output is correct |
88 |
Correct |
454 ms |
42276 KB |
Output is correct |
89 |
Correct |
878 ms |
43976 KB |
Output is correct |
90 |
Correct |
1044 ms |
44348 KB |
Output is correct |
91 |
Correct |
648 ms |
43456 KB |
Output is correct |
92 |
Correct |
1210 ms |
44472 KB |
Output is correct |
93 |
Correct |
1122 ms |
44268 KB |
Output is correct |
94 |
Correct |
350 ms |
41604 KB |
Output is correct |
95 |
Correct |
875 ms |
52260 KB |
Output is correct |
96 |
Correct |
882 ms |
43376 KB |
Output is correct |
97 |
Correct |
868 ms |
43456 KB |
Output is correct |
98 |
Correct |
868 ms |
43112 KB |
Output is correct |
99 |
Correct |
882 ms |
42712 KB |
Output is correct |
100 |
Correct |
900 ms |
42456 KB |
Output is correct |