#pragma GCC optimization O2
#pragma GCC optimization "unroll-loop"
#pragma target ("avx2")
#include <bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
using namespace std;
typedef pair<ll,ll> LL;
const ll N = (1 << 20) + 9;
const ll inf = 1e9 + 7;
char c;
string s;
ll a[N];
ll L,Q,dp[N],dpR[N];
bool bit(ll x,ll i){
return (x >> i) & 1;
}
ll take,now,ans;
void fQ(ll pos){
if (pos >= L){
//cout<<now<<"x\n";
ans += a[now]; return;
}
if (s[pos] == '1'){
now = now*2 + 1; fQ(pos + 1); now = (now - 1)/2;
}
else if (s[pos] == '0'){
now = now*2; fQ(pos + 1); now /= 2;
}
else{
now = now*2; fQ(pos + 1); now /= 2;
now = now*2 + 1; fQ(pos + 1); now = (now - 1)/2;
}
}
void f1(ll pos){
if (pos >= L){
//cout<<now<<"x\n";
if (take%2 == 0) ans += dp[now];
else ans -= dp[now];
return;
}
if (s[pos] == '1'){
now = now*2; take++; f1(pos + 1); now /= 2; take--;
now = now*2 + 1; f1(pos + 1); now = (now - 1)/2;
}
else if (s[pos] == '0'){
now = now*2; f1(pos + 1); now /= 2;
}
else{
now = now*2 + 1; f1(pos + 1); now = (now - 1)/2;
}
}
void f0(ll pos){
if (pos >= L){
if (take%2 == 0) ans += dpR[now];
else ans -= dpR[now];
return;
}
if (s[pos] == '1'){
now = now*2 + 1; f0(pos + 1); now = (now - 1)/2;
}
else if (s[pos] == '0'){
now = now*2 + 1; take++; f0(pos + 1); now = (now - 1)/2; take--;
now = now*2; f0(pos + 1); now /= 2;
}
else{
now = now*2; f0(pos + 1); now /= 2;
}
}
int main(){
ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
#define task "test"
if (fopen(task".INP","r")){
freopen(task".INP","r",stdin);
//freopen(task".OUT","w",stdout);
}
cin>>L>>Q;
for (ll i = 0;i < (1 << L);i++){
cin>>c; a[i] = c - '0';
dp[i] += a[i]; dpR[i] += a[i];
}
for (ll i = 0;i < L;i++){
for (ll mask = 0;mask < (1 << L);mask++)
if (bit(mask,i)) dp[mask] += dp[mask ^ (1 << i)];
for (ll mask = (1 << L) - 1;mask >= 0;mask--)
if (!bit(mask,i)) dpR[mask] += dpR[mask ^ (1 << i)];
}
while(Q--){
cin>>s; ll cnt1 = 0,cnt0 = 0,cntQ = 0; take = now = ans = 0;
for (ll i = 0;i < L;i++){
if (s[i] == '1') cnt1++;
else if (s[i] == '0') cnt0++;
else cntQ++;
}
ll mn = min(cntQ,min(cnt1,cnt0));
if (cntQ == mn) fQ(0);
else if (cnt1 == mn) f1(0);
else f0(0);
cout<<ans<<"\n";
}
}
Compilation message
snake_escaping.cpp:1: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
1 | #pragma GCC optimization O2
|
snake_escaping.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
2 | #pragma GCC optimization "unroll-loop"
|
snake_escaping.cpp:3: warning: ignoring '#pragma target ' [-Wunknown-pragmas]
3 | #pragma target ("avx2")
|
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen(task".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
319 ms |
4364 KB |
Output is correct |
12 |
Correct |
339 ms |
3908 KB |
Output is correct |
13 |
Correct |
458 ms |
3144 KB |
Output is correct |
14 |
Correct |
560 ms |
3288 KB |
Output is correct |
15 |
Correct |
414 ms |
4280 KB |
Output is correct |
16 |
Correct |
578 ms |
3436 KB |
Output is correct |
17 |
Correct |
572 ms |
3384 KB |
Output is correct |
18 |
Correct |
277 ms |
5316 KB |
Output is correct |
19 |
Correct |
336 ms |
2244 KB |
Output is correct |
20 |
Correct |
355 ms |
3940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
319 ms |
4364 KB |
Output is correct |
12 |
Correct |
339 ms |
3908 KB |
Output is correct |
13 |
Correct |
458 ms |
3144 KB |
Output is correct |
14 |
Correct |
560 ms |
3288 KB |
Output is correct |
15 |
Correct |
414 ms |
4280 KB |
Output is correct |
16 |
Correct |
578 ms |
3436 KB |
Output is correct |
17 |
Correct |
572 ms |
3384 KB |
Output is correct |
18 |
Correct |
277 ms |
5316 KB |
Output is correct |
19 |
Correct |
336 ms |
2244 KB |
Output is correct |
20 |
Correct |
355 ms |
3940 KB |
Output is correct |
21 |
Correct |
386 ms |
4824 KB |
Output is correct |
22 |
Correct |
428 ms |
18280 KB |
Output is correct |
23 |
Correct |
715 ms |
17240 KB |
Output is correct |
24 |
Correct |
959 ms |
17092 KB |
Output is correct |
25 |
Correct |
555 ms |
19184 KB |
Output is correct |
26 |
Correct |
916 ms |
17656 KB |
Output is correct |
27 |
Correct |
772 ms |
17548 KB |
Output is correct |
28 |
Correct |
325 ms |
20164 KB |
Output is correct |
29 |
Correct |
441 ms |
16180 KB |
Output is correct |
30 |
Correct |
462 ms |
18300 KB |
Output is correct |
31 |
Correct |
729 ms |
18084 KB |
Output is correct |
32 |
Correct |
1099 ms |
18100 KB |
Output is correct |
33 |
Correct |
583 ms |
16968 KB |
Output is correct |
34 |
Correct |
887 ms |
17064 KB |
Output is correct |
35 |
Correct |
805 ms |
17540 KB |
Output is correct |
36 |
Correct |
306 ms |
16068 KB |
Output is correct |
37 |
Correct |
428 ms |
18156 KB |
Output is correct |
38 |
Correct |
435 ms |
16064 KB |
Output is correct |
39 |
Correct |
745 ms |
17268 KB |
Output is correct |
40 |
Correct |
992 ms |
17184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
96 ms |
12856 KB |
Output is correct |
12 |
Correct |
103 ms |
12748 KB |
Output is correct |
13 |
Correct |
156 ms |
12740 KB |
Output is correct |
14 |
Correct |
281 ms |
12760 KB |
Output is correct |
15 |
Correct |
136 ms |
12864 KB |
Output is correct |
16 |
Correct |
286 ms |
12872 KB |
Output is correct |
17 |
Correct |
285 ms |
12780 KB |
Output is correct |
18 |
Correct |
91 ms |
12912 KB |
Output is correct |
19 |
Correct |
114 ms |
12696 KB |
Output is correct |
20 |
Correct |
133 ms |
12860 KB |
Output is correct |
21 |
Correct |
217 ms |
12792 KB |
Output is correct |
22 |
Correct |
267 ms |
12768 KB |
Output is correct |
23 |
Correct |
145 ms |
12732 KB |
Output is correct |
24 |
Correct |
257 ms |
12796 KB |
Output is correct |
25 |
Correct |
225 ms |
12740 KB |
Output is correct |
26 |
Correct |
87 ms |
12608 KB |
Output is correct |
27 |
Correct |
102 ms |
12864 KB |
Output is correct |
28 |
Correct |
105 ms |
12720 KB |
Output is correct |
29 |
Correct |
187 ms |
12732 KB |
Output is correct |
30 |
Correct |
243 ms |
12864 KB |
Output is correct |
31 |
Correct |
116 ms |
12748 KB |
Output is correct |
32 |
Correct |
280 ms |
12820 KB |
Output is correct |
33 |
Correct |
248 ms |
12816 KB |
Output is correct |
34 |
Correct |
87 ms |
12700 KB |
Output is correct |
35 |
Correct |
189 ms |
12704 KB |
Output is correct |
36 |
Correct |
214 ms |
12740 KB |
Output is correct |
37 |
Correct |
176 ms |
12736 KB |
Output is correct |
38 |
Correct |
193 ms |
12756 KB |
Output is correct |
39 |
Correct |
228 ms |
12780 KB |
Output is correct |
40 |
Correct |
182 ms |
12756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
319 ms |
4364 KB |
Output is correct |
12 |
Correct |
339 ms |
3908 KB |
Output is correct |
13 |
Correct |
458 ms |
3144 KB |
Output is correct |
14 |
Correct |
560 ms |
3288 KB |
Output is correct |
15 |
Correct |
414 ms |
4280 KB |
Output is correct |
16 |
Correct |
578 ms |
3436 KB |
Output is correct |
17 |
Correct |
572 ms |
3384 KB |
Output is correct |
18 |
Correct |
277 ms |
5316 KB |
Output is correct |
19 |
Correct |
336 ms |
2244 KB |
Output is correct |
20 |
Correct |
355 ms |
3940 KB |
Output is correct |
21 |
Correct |
386 ms |
4824 KB |
Output is correct |
22 |
Correct |
428 ms |
18280 KB |
Output is correct |
23 |
Correct |
715 ms |
17240 KB |
Output is correct |
24 |
Correct |
959 ms |
17092 KB |
Output is correct |
25 |
Correct |
555 ms |
19184 KB |
Output is correct |
26 |
Correct |
916 ms |
17656 KB |
Output is correct |
27 |
Correct |
772 ms |
17548 KB |
Output is correct |
28 |
Correct |
325 ms |
20164 KB |
Output is correct |
29 |
Correct |
441 ms |
16180 KB |
Output is correct |
30 |
Correct |
462 ms |
18300 KB |
Output is correct |
31 |
Correct |
729 ms |
18084 KB |
Output is correct |
32 |
Correct |
1099 ms |
18100 KB |
Output is correct |
33 |
Correct |
583 ms |
16968 KB |
Output is correct |
34 |
Correct |
887 ms |
17064 KB |
Output is correct |
35 |
Correct |
805 ms |
17540 KB |
Output is correct |
36 |
Correct |
306 ms |
16068 KB |
Output is correct |
37 |
Correct |
428 ms |
18156 KB |
Output is correct |
38 |
Correct |
435 ms |
16064 KB |
Output is correct |
39 |
Correct |
745 ms |
17268 KB |
Output is correct |
40 |
Correct |
992 ms |
17184 KB |
Output is correct |
41 |
Correct |
96 ms |
12856 KB |
Output is correct |
42 |
Correct |
103 ms |
12748 KB |
Output is correct |
43 |
Correct |
156 ms |
12740 KB |
Output is correct |
44 |
Correct |
281 ms |
12760 KB |
Output is correct |
45 |
Correct |
136 ms |
12864 KB |
Output is correct |
46 |
Correct |
286 ms |
12872 KB |
Output is correct |
47 |
Correct |
285 ms |
12780 KB |
Output is correct |
48 |
Correct |
91 ms |
12912 KB |
Output is correct |
49 |
Correct |
114 ms |
12696 KB |
Output is correct |
50 |
Correct |
133 ms |
12860 KB |
Output is correct |
51 |
Correct |
217 ms |
12792 KB |
Output is correct |
52 |
Correct |
267 ms |
12768 KB |
Output is correct |
53 |
Correct |
145 ms |
12732 KB |
Output is correct |
54 |
Correct |
257 ms |
12796 KB |
Output is correct |
55 |
Correct |
225 ms |
12740 KB |
Output is correct |
56 |
Correct |
87 ms |
12608 KB |
Output is correct |
57 |
Correct |
102 ms |
12864 KB |
Output is correct |
58 |
Correct |
105 ms |
12720 KB |
Output is correct |
59 |
Correct |
187 ms |
12732 KB |
Output is correct |
60 |
Correct |
243 ms |
12864 KB |
Output is correct |
61 |
Correct |
116 ms |
12748 KB |
Output is correct |
62 |
Correct |
280 ms |
12820 KB |
Output is correct |
63 |
Correct |
248 ms |
12816 KB |
Output is correct |
64 |
Correct |
87 ms |
12700 KB |
Output is correct |
65 |
Correct |
189 ms |
12704 KB |
Output is correct |
66 |
Correct |
214 ms |
12740 KB |
Output is correct |
67 |
Correct |
176 ms |
12736 KB |
Output is correct |
68 |
Correct |
193 ms |
12756 KB |
Output is correct |
69 |
Correct |
228 ms |
12780 KB |
Output is correct |
70 |
Correct |
182 ms |
12756 KB |
Output is correct |
71 |
Correct |
713 ms |
39152 KB |
Output is correct |
72 |
Correct |
746 ms |
39252 KB |
Output is correct |
73 |
Execution timed out |
2024 ms |
37956 KB |
Time limit exceeded |
74 |
Halted |
0 ms |
0 KB |
- |