#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define int long long
int DP1[1048576];
int DP2[1048576];
int arr[1048576];
int idx[6];
inline int preprocess(const string &s,char c){
int ans = 0;
int cnt = 0;
#pragma GCC unroll 20
#pragma GCC ivdep
for(int bit=0;bit<s.size();bit++){
if(s[bit]==c){
idx[cnt++]=bit;
} else if(s[bit]=='?'){
if(c=='1')ans|=(1<<bit);
} else {
if(s[bit]=='1')ans|=(1<<bit);
}
}
return ans;
}
inline int applymask(int qu,int mask,int cnt){
#pragma GCC unroll 20
#pragma GCC ivdep
for(int bit=0;bit<cnt;bit++){
if(mask&(1<<bit))qu|=(1<<idx[bit]);
}
return qu;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int l,q;
cin >> l >> q;
for(int mask=0;mask<(1<<l);mask++){
char a;cin>>a;a-='0';
DP1[mask]=DP2[mask]=arr[mask]=a;
}
#pragma GCC unroll 20
#pragma GCC ivdep
for(int bit=0;bit<20;bit++){
for(int mask=0;mask<(1<<20);mask++){
if(mask&(1<<bit)){
DP2[mask^(1<<bit)]+=DP2[mask];
} else {
DP1[mask|(1<<bit)]+=DP1[mask];
}
}
}
for(int i=1;i<=q;i++){
string s;cin>>s;
reverse(s.begin(),s.end());
int c1=0,c0=0,cq=0;
#pragma GCC unroll 20
#pragma GCC ivdep
for(char&j:s) {
if(j=='1')c1++;
else if(j=='0')c0++;
else if(j=='?')cq++;
}
if(cq<=6){
int qu = preprocess(s,'?');
int ans = 0;
#pragma GCC unroll 64
#pragma GCC ivdep
for(int mask=0;mask<(1<<cq);mask++){
ans+=arr[applymask(qu,mask,cq)];
}
cout<<ans<<'\n';
continue;
}
if(c1<=6){
int qu = preprocess(s,'1');
int ans = 0;
#pragma GCC unroll 64
#pragma GCC ivdep
for(int mask=0;mask<(1<<c1);mask++){
if(__builtin_parity(mask)==(c1&1)){
ans+=DP1[applymask(qu,mask,c1)];
} else {
ans-=DP1[applymask(qu,mask,c1)];
}
}
cout<<ans<<'\n';
continue;
}
if(c0<=6){
int qu = preprocess(s,'0');
int ans = 0;
#pragma GCC unroll 64
#pragma GCC ivdep
for(int mask=0;mask<(1<<c0);mask++){
if(__builtin_parity(mask)){
ans-=DP2[applymask(qu,mask,c0)];
} else {
ans+=DP2[applymask(qu,mask,c0)];
}
}
cout<<ans<<'\n';
continue;
}
}
}
Compilation message
snake_escaping.cpp: In function 'long long int preprocess(const string&, char)':
snake_escaping.cpp:18:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for(int bit=0;bit<s.size();bit++){
| ~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
18796 KB |
Output is correct |
2 |
Correct |
27 ms |
18776 KB |
Output is correct |
3 |
Correct |
29 ms |
18780 KB |
Output is correct |
4 |
Correct |
33 ms |
18776 KB |
Output is correct |
5 |
Correct |
34 ms |
18776 KB |
Output is correct |
6 |
Correct |
31 ms |
18780 KB |
Output is correct |
7 |
Correct |
29 ms |
18776 KB |
Output is correct |
8 |
Correct |
25 ms |
18776 KB |
Output is correct |
9 |
Correct |
29 ms |
18780 KB |
Output is correct |
10 |
Correct |
26 ms |
18776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
18796 KB |
Output is correct |
2 |
Correct |
27 ms |
18776 KB |
Output is correct |
3 |
Correct |
29 ms |
18780 KB |
Output is correct |
4 |
Correct |
33 ms |
18776 KB |
Output is correct |
5 |
Correct |
34 ms |
18776 KB |
Output is correct |
6 |
Correct |
31 ms |
18780 KB |
Output is correct |
7 |
Correct |
29 ms |
18776 KB |
Output is correct |
8 |
Correct |
25 ms |
18776 KB |
Output is correct |
9 |
Correct |
29 ms |
18780 KB |
Output is correct |
10 |
Correct |
26 ms |
18776 KB |
Output is correct |
11 |
Correct |
325 ms |
22880 KB |
Output is correct |
12 |
Correct |
294 ms |
22264 KB |
Output is correct |
13 |
Correct |
302 ms |
21652 KB |
Output is correct |
14 |
Correct |
288 ms |
21736 KB |
Output is correct |
15 |
Correct |
411 ms |
22608 KB |
Output is correct |
16 |
Correct |
332 ms |
21840 KB |
Output is correct |
17 |
Correct |
339 ms |
21772 KB |
Output is correct |
18 |
Correct |
211 ms |
23608 KB |
Output is correct |
19 |
Correct |
242 ms |
20600 KB |
Output is correct |
20 |
Correct |
300 ms |
22352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
18796 KB |
Output is correct |
2 |
Correct |
27 ms |
18776 KB |
Output is correct |
3 |
Correct |
29 ms |
18780 KB |
Output is correct |
4 |
Correct |
33 ms |
18776 KB |
Output is correct |
5 |
Correct |
34 ms |
18776 KB |
Output is correct |
6 |
Correct |
31 ms |
18780 KB |
Output is correct |
7 |
Correct |
29 ms |
18776 KB |
Output is correct |
8 |
Correct |
25 ms |
18776 KB |
Output is correct |
9 |
Correct |
29 ms |
18780 KB |
Output is correct |
10 |
Correct |
26 ms |
18776 KB |
Output is correct |
11 |
Correct |
325 ms |
22880 KB |
Output is correct |
12 |
Correct |
294 ms |
22264 KB |
Output is correct |
13 |
Correct |
302 ms |
21652 KB |
Output is correct |
14 |
Correct |
288 ms |
21736 KB |
Output is correct |
15 |
Correct |
411 ms |
22608 KB |
Output is correct |
16 |
Correct |
332 ms |
21840 KB |
Output is correct |
17 |
Correct |
339 ms |
21772 KB |
Output is correct |
18 |
Correct |
211 ms |
23608 KB |
Output is correct |
19 |
Correct |
242 ms |
20600 KB |
Output is correct |
20 |
Correct |
300 ms |
22352 KB |
Output is correct |
21 |
Correct |
450 ms |
22896 KB |
Output is correct |
22 |
Correct |
372 ms |
23108 KB |
Output is correct |
23 |
Correct |
363 ms |
21856 KB |
Output is correct |
24 |
Correct |
367 ms |
21840 KB |
Output is correct |
25 |
Correct |
325 ms |
23656 KB |
Output is correct |
26 |
Correct |
407 ms |
22180 KB |
Output is correct |
27 |
Correct |
404 ms |
22096 KB |
Output is correct |
28 |
Correct |
238 ms |
24964 KB |
Output is correct |
29 |
Correct |
290 ms |
20816 KB |
Output is correct |
30 |
Correct |
403 ms |
22864 KB |
Output is correct |
31 |
Correct |
486 ms |
22864 KB |
Output is correct |
32 |
Correct |
403 ms |
22760 KB |
Output is correct |
33 |
Correct |
337 ms |
21768 KB |
Output is correct |
34 |
Correct |
382 ms |
21916 KB |
Output is correct |
35 |
Correct |
397 ms |
22096 KB |
Output is correct |
36 |
Correct |
243 ms |
20816 KB |
Output is correct |
37 |
Correct |
420 ms |
22868 KB |
Output is correct |
38 |
Correct |
318 ms |
20840 KB |
Output is correct |
39 |
Correct |
380 ms |
21840 KB |
Output is correct |
40 |
Correct |
370 ms |
21840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
18796 KB |
Output is correct |
2 |
Correct |
27 ms |
18776 KB |
Output is correct |
3 |
Correct |
29 ms |
18780 KB |
Output is correct |
4 |
Correct |
33 ms |
18776 KB |
Output is correct |
5 |
Correct |
34 ms |
18776 KB |
Output is correct |
6 |
Correct |
31 ms |
18780 KB |
Output is correct |
7 |
Correct |
29 ms |
18776 KB |
Output is correct |
8 |
Correct |
25 ms |
18776 KB |
Output is correct |
9 |
Correct |
29 ms |
18780 KB |
Output is correct |
10 |
Correct |
26 ms |
18776 KB |
Output is correct |
11 |
Correct |
50 ms |
25168 KB |
Output is correct |
12 |
Correct |
54 ms |
25128 KB |
Output is correct |
13 |
Correct |
59 ms |
25168 KB |
Output is correct |
14 |
Correct |
66 ms |
25168 KB |
Output is correct |
15 |
Correct |
55 ms |
25316 KB |
Output is correct |
16 |
Correct |
95 ms |
25424 KB |
Output is correct |
17 |
Correct |
78 ms |
25168 KB |
Output is correct |
18 |
Correct |
47 ms |
25424 KB |
Output is correct |
19 |
Correct |
54 ms |
25072 KB |
Output is correct |
20 |
Correct |
52 ms |
25156 KB |
Output is correct |
21 |
Correct |
60 ms |
25168 KB |
Output is correct |
22 |
Correct |
59 ms |
25100 KB |
Output is correct |
23 |
Correct |
54 ms |
25168 KB |
Output is correct |
24 |
Correct |
65 ms |
25168 KB |
Output is correct |
25 |
Correct |
83 ms |
25192 KB |
Output is correct |
26 |
Correct |
46 ms |
25176 KB |
Output is correct |
27 |
Correct |
61 ms |
25316 KB |
Output is correct |
28 |
Correct |
57 ms |
24924 KB |
Output is correct |
29 |
Correct |
58 ms |
25168 KB |
Output is correct |
30 |
Correct |
65 ms |
25172 KB |
Output is correct |
31 |
Correct |
53 ms |
25172 KB |
Output is correct |
32 |
Correct |
73 ms |
25240 KB |
Output is correct |
33 |
Correct |
93 ms |
25392 KB |
Output is correct |
34 |
Correct |
45 ms |
25072 KB |
Output is correct |
35 |
Correct |
71 ms |
25168 KB |
Output is correct |
36 |
Correct |
76 ms |
25168 KB |
Output is correct |
37 |
Correct |
71 ms |
25168 KB |
Output is correct |
38 |
Correct |
74 ms |
25088 KB |
Output is correct |
39 |
Correct |
74 ms |
25144 KB |
Output is correct |
40 |
Correct |
72 ms |
25168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
18796 KB |
Output is correct |
2 |
Correct |
27 ms |
18776 KB |
Output is correct |
3 |
Correct |
29 ms |
18780 KB |
Output is correct |
4 |
Correct |
33 ms |
18776 KB |
Output is correct |
5 |
Correct |
34 ms |
18776 KB |
Output is correct |
6 |
Correct |
31 ms |
18780 KB |
Output is correct |
7 |
Correct |
29 ms |
18776 KB |
Output is correct |
8 |
Correct |
25 ms |
18776 KB |
Output is correct |
9 |
Correct |
29 ms |
18780 KB |
Output is correct |
10 |
Correct |
26 ms |
18776 KB |
Output is correct |
11 |
Correct |
325 ms |
22880 KB |
Output is correct |
12 |
Correct |
294 ms |
22264 KB |
Output is correct |
13 |
Correct |
302 ms |
21652 KB |
Output is correct |
14 |
Correct |
288 ms |
21736 KB |
Output is correct |
15 |
Correct |
411 ms |
22608 KB |
Output is correct |
16 |
Correct |
332 ms |
21840 KB |
Output is correct |
17 |
Correct |
339 ms |
21772 KB |
Output is correct |
18 |
Correct |
211 ms |
23608 KB |
Output is correct |
19 |
Correct |
242 ms |
20600 KB |
Output is correct |
20 |
Correct |
300 ms |
22352 KB |
Output is correct |
21 |
Correct |
450 ms |
22896 KB |
Output is correct |
22 |
Correct |
372 ms |
23108 KB |
Output is correct |
23 |
Correct |
363 ms |
21856 KB |
Output is correct |
24 |
Correct |
367 ms |
21840 KB |
Output is correct |
25 |
Correct |
325 ms |
23656 KB |
Output is correct |
26 |
Correct |
407 ms |
22180 KB |
Output is correct |
27 |
Correct |
404 ms |
22096 KB |
Output is correct |
28 |
Correct |
238 ms |
24964 KB |
Output is correct |
29 |
Correct |
290 ms |
20816 KB |
Output is correct |
30 |
Correct |
403 ms |
22864 KB |
Output is correct |
31 |
Correct |
486 ms |
22864 KB |
Output is correct |
32 |
Correct |
403 ms |
22760 KB |
Output is correct |
33 |
Correct |
337 ms |
21768 KB |
Output is correct |
34 |
Correct |
382 ms |
21916 KB |
Output is correct |
35 |
Correct |
397 ms |
22096 KB |
Output is correct |
36 |
Correct |
243 ms |
20816 KB |
Output is correct |
37 |
Correct |
420 ms |
22868 KB |
Output is correct |
38 |
Correct |
318 ms |
20840 KB |
Output is correct |
39 |
Correct |
380 ms |
21840 KB |
Output is correct |
40 |
Correct |
370 ms |
21840 KB |
Output is correct |
41 |
Correct |
50 ms |
25168 KB |
Output is correct |
42 |
Correct |
54 ms |
25128 KB |
Output is correct |
43 |
Correct |
59 ms |
25168 KB |
Output is correct |
44 |
Correct |
66 ms |
25168 KB |
Output is correct |
45 |
Correct |
55 ms |
25316 KB |
Output is correct |
46 |
Correct |
95 ms |
25424 KB |
Output is correct |
47 |
Correct |
78 ms |
25168 KB |
Output is correct |
48 |
Correct |
47 ms |
25424 KB |
Output is correct |
49 |
Correct |
54 ms |
25072 KB |
Output is correct |
50 |
Correct |
52 ms |
25156 KB |
Output is correct |
51 |
Correct |
60 ms |
25168 KB |
Output is correct |
52 |
Correct |
59 ms |
25100 KB |
Output is correct |
53 |
Correct |
54 ms |
25168 KB |
Output is correct |
54 |
Correct |
65 ms |
25168 KB |
Output is correct |
55 |
Correct |
83 ms |
25192 KB |
Output is correct |
56 |
Correct |
46 ms |
25176 KB |
Output is correct |
57 |
Correct |
61 ms |
25316 KB |
Output is correct |
58 |
Correct |
57 ms |
24924 KB |
Output is correct |
59 |
Correct |
58 ms |
25168 KB |
Output is correct |
60 |
Correct |
65 ms |
25172 KB |
Output is correct |
61 |
Correct |
53 ms |
25172 KB |
Output is correct |
62 |
Correct |
73 ms |
25240 KB |
Output is correct |
63 |
Correct |
93 ms |
25392 KB |
Output is correct |
64 |
Correct |
45 ms |
25072 KB |
Output is correct |
65 |
Correct |
71 ms |
25168 KB |
Output is correct |
66 |
Correct |
76 ms |
25168 KB |
Output is correct |
67 |
Correct |
71 ms |
25168 KB |
Output is correct |
68 |
Correct |
74 ms |
25088 KB |
Output is correct |
69 |
Correct |
74 ms |
25144 KB |
Output is correct |
70 |
Correct |
72 ms |
25168 KB |
Output is correct |
71 |
Correct |
403 ms |
30068 KB |
Output is correct |
72 |
Correct |
461 ms |
30084 KB |
Output is correct |
73 |
Correct |
513 ms |
28604 KB |
Output is correct |
74 |
Correct |
636 ms |
29060 KB |
Output is correct |
75 |
Correct |
419 ms |
30936 KB |
Output is correct |
76 |
Correct |
1260 ms |
29336 KB |
Output is correct |
77 |
Correct |
1022 ms |
29304 KB |
Output is correct |
78 |
Correct |
348 ms |
33096 KB |
Output is correct |
79 |
Correct |
411 ms |
27036 KB |
Output is correct |
80 |
Correct |
434 ms |
30032 KB |
Output is correct |
81 |
Correct |
510 ms |
30288 KB |
Output is correct |
82 |
Correct |
576 ms |
29008 KB |
Output is correct |
83 |
Correct |
411 ms |
28100 KB |
Output is correct |
84 |
Correct |
586 ms |
28880 KB |
Output is correct |
85 |
Correct |
974 ms |
29380 KB |
Output is correct |
86 |
Correct |
293 ms |
27316 KB |
Output is correct |
87 |
Correct |
407 ms |
30024 KB |
Output is correct |
88 |
Correct |
387 ms |
26964 KB |
Output is correct |
89 |
Correct |
482 ms |
28512 KB |
Output is correct |
90 |
Correct |
577 ms |
28852 KB |
Output is correct |
91 |
Correct |
401 ms |
28240 KB |
Output is correct |
92 |
Correct |
840 ms |
29308 KB |
Output is correct |
93 |
Correct |
967 ms |
29136 KB |
Output is correct |
94 |
Correct |
297 ms |
27120 KB |
Output is correct |
95 |
Correct |
765 ms |
29020 KB |
Output is correct |
96 |
Correct |
833 ms |
29320 KB |
Output is correct |
97 |
Correct |
782 ms |
28952 KB |
Output is correct |
98 |
Correct |
774 ms |
29272 KB |
Output is correct |
99 |
Correct |
790 ms |
29048 KB |
Output is correct |
100 |
Correct |
819 ms |
29272 KB |
Output is correct |