#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#include <bits/stdc++.h>
using namespace std;
const int Q=1e6+5;
const int M20=1048576;
const int M13=1594323;
int n,q,C[M20+5],M[15],dp[M13+5],ANS[Q],Y[Q],Last2[M13+5],Boz[Q],Boz2[Q],x,ko,K,i,j,k,Mid;
string S,s;
char c;
main () {
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>q;
M[0]=1;
for (int i=1; i<=13; i++)
M[i]=M[i-1]*3;
Mid=min(n,13);
for (i=0; i<(1<<n); i++) {
cin>>c;
C[i]=c-'0';
}
K=(n-Mid);
for (i=1; i<=q; i++) {
cin>>S;
for (k=S.size()-1; k>=K; k--) {
if (S[k]=='?') Y[i]+=2*M[S.size()-1-k];
else
if (S[k]=='1') Y[i]+=M[S.size()-1-k];
}
for (k=0; k<K; k++)
if (S[K-k-1]=='?') Boz2[i]|=(1<<(k));
else
if (S[K-k-1]=='1') Boz[i]|=(1<<(k));
}
for (i=0; i<M[Mid]; i++) {
x=i,Last2[i]=-1;
for (j=Mid-1; j>=0; j--)
if (x>=2*M[j]) { Last2[i]=j; break; }
else
if (x>=M[j]) x-=M[j];
}
for (i=0; i<(1<<K); i++) {
for (j=0; j<(1<<Mid); j++) {
x=0;
for (k=0; k<Mid; k++)
if (j&(1<<k)) x+=M[k];
dp[x]=C[i*(1<<Mid)+j];
}
for (j=0; j<M[Mid]; j++)
if (Last2[j]!=-1) dp[j]=(dp[j-2*M[Last2[j]]]+dp[j-M[Last2[j]]]);
for (j=1; j<=q; j++)
if (!((i-(i&Boz2[j]))^Boz[j])) ANS[j]+=dp[Y[j]];
}
for (i=1; i<=q; i++)
printf("%d\n",ANS[i]);
}
Compilation message
snake_escaping.cpp:11:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main () {
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
888 KB |
Output is correct |
2 |
Correct |
3 ms |
888 KB |
Output is correct |
3 |
Correct |
3 ms |
888 KB |
Output is correct |
4 |
Correct |
3 ms |
888 KB |
Output is correct |
5 |
Correct |
3 ms |
888 KB |
Output is correct |
6 |
Correct |
3 ms |
936 KB |
Output is correct |
7 |
Correct |
3 ms |
888 KB |
Output is correct |
8 |
Correct |
3 ms |
888 KB |
Output is correct |
9 |
Correct |
3 ms |
888 KB |
Output is correct |
10 |
Correct |
3 ms |
888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
888 KB |
Output is correct |
2 |
Correct |
3 ms |
888 KB |
Output is correct |
3 |
Correct |
3 ms |
888 KB |
Output is correct |
4 |
Correct |
3 ms |
888 KB |
Output is correct |
5 |
Correct |
3 ms |
888 KB |
Output is correct |
6 |
Correct |
3 ms |
936 KB |
Output is correct |
7 |
Correct |
3 ms |
888 KB |
Output is correct |
8 |
Correct |
3 ms |
888 KB |
Output is correct |
9 |
Correct |
3 ms |
888 KB |
Output is correct |
10 |
Correct |
3 ms |
888 KB |
Output is correct |
11 |
Correct |
250 ms |
12860 KB |
Output is correct |
12 |
Correct |
272 ms |
12400 KB |
Output is correct |
13 |
Correct |
270 ms |
11612 KB |
Output is correct |
14 |
Correct |
272 ms |
11768 KB |
Output is correct |
15 |
Correct |
285 ms |
12652 KB |
Output is correct |
16 |
Correct |
288 ms |
11896 KB |
Output is correct |
17 |
Correct |
289 ms |
11824 KB |
Output is correct |
18 |
Correct |
209 ms |
13688 KB |
Output is correct |
19 |
Correct |
254 ms |
10748 KB |
Output is correct |
20 |
Correct |
270 ms |
12280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
888 KB |
Output is correct |
2 |
Correct |
3 ms |
888 KB |
Output is correct |
3 |
Correct |
3 ms |
888 KB |
Output is correct |
4 |
Correct |
3 ms |
888 KB |
Output is correct |
5 |
Correct |
3 ms |
888 KB |
Output is correct |
6 |
Correct |
3 ms |
936 KB |
Output is correct |
7 |
Correct |
3 ms |
888 KB |
Output is correct |
8 |
Correct |
3 ms |
888 KB |
Output is correct |
9 |
Correct |
3 ms |
888 KB |
Output is correct |
10 |
Correct |
3 ms |
888 KB |
Output is correct |
11 |
Correct |
250 ms |
12860 KB |
Output is correct |
12 |
Correct |
272 ms |
12400 KB |
Output is correct |
13 |
Correct |
270 ms |
11612 KB |
Output is correct |
14 |
Correct |
272 ms |
11768 KB |
Output is correct |
15 |
Correct |
285 ms |
12652 KB |
Output is correct |
16 |
Correct |
288 ms |
11896 KB |
Output is correct |
17 |
Correct |
289 ms |
11824 KB |
Output is correct |
18 |
Correct |
209 ms |
13688 KB |
Output is correct |
19 |
Correct |
254 ms |
10748 KB |
Output is correct |
20 |
Correct |
270 ms |
12280 KB |
Output is correct |
21 |
Correct |
295 ms |
24692 KB |
Output is correct |
22 |
Correct |
319 ms |
24972 KB |
Output is correct |
23 |
Correct |
332 ms |
23996 KB |
Output is correct |
24 |
Correct |
334 ms |
23800 KB |
Output is correct |
25 |
Correct |
328 ms |
25720 KB |
Output is correct |
26 |
Correct |
344 ms |
24440 KB |
Output is correct |
27 |
Correct |
346 ms |
24316 KB |
Output is correct |
28 |
Correct |
240 ms |
26744 KB |
Output is correct |
29 |
Correct |
297 ms |
22776 KB |
Output is correct |
30 |
Correct |
311 ms |
24952 KB |
Output is correct |
31 |
Correct |
343 ms |
24824 KB |
Output is correct |
32 |
Correct |
332 ms |
24848 KB |
Output is correct |
33 |
Correct |
297 ms |
23652 KB |
Output is correct |
34 |
Correct |
326 ms |
23672 KB |
Output is correct |
35 |
Correct |
353 ms |
24184 KB |
Output is correct |
36 |
Correct |
281 ms |
22904 KB |
Output is correct |
37 |
Correct |
298 ms |
24696 KB |
Output is correct |
38 |
Correct |
304 ms |
22780 KB |
Output is correct |
39 |
Correct |
323 ms |
23916 KB |
Output is correct |
40 |
Correct |
333 ms |
23672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
888 KB |
Output is correct |
2 |
Correct |
3 ms |
888 KB |
Output is correct |
3 |
Correct |
3 ms |
888 KB |
Output is correct |
4 |
Correct |
3 ms |
888 KB |
Output is correct |
5 |
Correct |
3 ms |
888 KB |
Output is correct |
6 |
Correct |
3 ms |
936 KB |
Output is correct |
7 |
Correct |
3 ms |
888 KB |
Output is correct |
8 |
Correct |
3 ms |
888 KB |
Output is correct |
9 |
Correct |
3 ms |
888 KB |
Output is correct |
10 |
Correct |
3 ms |
888 KB |
Output is correct |
11 |
Correct |
534 ms |
18032 KB |
Output is correct |
12 |
Correct |
531 ms |
17912 KB |
Output is correct |
13 |
Correct |
526 ms |
17912 KB |
Output is correct |
14 |
Correct |
531 ms |
18044 KB |
Output is correct |
15 |
Correct |
541 ms |
18040 KB |
Output is correct |
16 |
Correct |
522 ms |
17972 KB |
Output is correct |
17 |
Correct |
522 ms |
17916 KB |
Output is correct |
18 |
Correct |
509 ms |
17964 KB |
Output is correct |
19 |
Correct |
522 ms |
17656 KB |
Output is correct |
20 |
Correct |
530 ms |
18040 KB |
Output is correct |
21 |
Correct |
535 ms |
18108 KB |
Output is correct |
22 |
Correct |
572 ms |
17912 KB |
Output is correct |
23 |
Correct |
529 ms |
17912 KB |
Output is correct |
24 |
Correct |
539 ms |
17912 KB |
Output is correct |
25 |
Correct |
542 ms |
18040 KB |
Output is correct |
26 |
Correct |
813 ms |
17784 KB |
Output is correct |
27 |
Correct |
529 ms |
17784 KB |
Output is correct |
28 |
Correct |
523 ms |
17656 KB |
Output is correct |
29 |
Correct |
549 ms |
17912 KB |
Output is correct |
30 |
Correct |
530 ms |
18040 KB |
Output is correct |
31 |
Correct |
553 ms |
18040 KB |
Output is correct |
32 |
Correct |
532 ms |
17912 KB |
Output is correct |
33 |
Correct |
532 ms |
18040 KB |
Output is correct |
34 |
Correct |
508 ms |
17272 KB |
Output is correct |
35 |
Correct |
524 ms |
17988 KB |
Output is correct |
36 |
Correct |
532 ms |
17912 KB |
Output is correct |
37 |
Correct |
532 ms |
17912 KB |
Output is correct |
38 |
Correct |
545 ms |
17912 KB |
Output is correct |
39 |
Correct |
523 ms |
17912 KB |
Output is correct |
40 |
Correct |
520 ms |
17912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
888 KB |
Output is correct |
2 |
Correct |
3 ms |
888 KB |
Output is correct |
3 |
Correct |
3 ms |
888 KB |
Output is correct |
4 |
Correct |
3 ms |
888 KB |
Output is correct |
5 |
Correct |
3 ms |
888 KB |
Output is correct |
6 |
Correct |
3 ms |
936 KB |
Output is correct |
7 |
Correct |
3 ms |
888 KB |
Output is correct |
8 |
Correct |
3 ms |
888 KB |
Output is correct |
9 |
Correct |
3 ms |
888 KB |
Output is correct |
10 |
Correct |
3 ms |
888 KB |
Output is correct |
11 |
Correct |
250 ms |
12860 KB |
Output is correct |
12 |
Correct |
272 ms |
12400 KB |
Output is correct |
13 |
Correct |
270 ms |
11612 KB |
Output is correct |
14 |
Correct |
272 ms |
11768 KB |
Output is correct |
15 |
Correct |
285 ms |
12652 KB |
Output is correct |
16 |
Correct |
288 ms |
11896 KB |
Output is correct |
17 |
Correct |
289 ms |
11824 KB |
Output is correct |
18 |
Correct |
209 ms |
13688 KB |
Output is correct |
19 |
Correct |
254 ms |
10748 KB |
Output is correct |
20 |
Correct |
270 ms |
12280 KB |
Output is correct |
21 |
Correct |
295 ms |
24692 KB |
Output is correct |
22 |
Correct |
319 ms |
24972 KB |
Output is correct |
23 |
Correct |
332 ms |
23996 KB |
Output is correct |
24 |
Correct |
334 ms |
23800 KB |
Output is correct |
25 |
Correct |
328 ms |
25720 KB |
Output is correct |
26 |
Correct |
344 ms |
24440 KB |
Output is correct |
27 |
Correct |
346 ms |
24316 KB |
Output is correct |
28 |
Correct |
240 ms |
26744 KB |
Output is correct |
29 |
Correct |
297 ms |
22776 KB |
Output is correct |
30 |
Correct |
311 ms |
24952 KB |
Output is correct |
31 |
Correct |
343 ms |
24824 KB |
Output is correct |
32 |
Correct |
332 ms |
24848 KB |
Output is correct |
33 |
Correct |
297 ms |
23652 KB |
Output is correct |
34 |
Correct |
326 ms |
23672 KB |
Output is correct |
35 |
Correct |
353 ms |
24184 KB |
Output is correct |
36 |
Correct |
281 ms |
22904 KB |
Output is correct |
37 |
Correct |
298 ms |
24696 KB |
Output is correct |
38 |
Correct |
304 ms |
22780 KB |
Output is correct |
39 |
Correct |
323 ms |
23916 KB |
Output is correct |
40 |
Correct |
333 ms |
23672 KB |
Output is correct |
41 |
Correct |
534 ms |
18032 KB |
Output is correct |
42 |
Correct |
531 ms |
17912 KB |
Output is correct |
43 |
Correct |
526 ms |
17912 KB |
Output is correct |
44 |
Correct |
531 ms |
18044 KB |
Output is correct |
45 |
Correct |
541 ms |
18040 KB |
Output is correct |
46 |
Correct |
522 ms |
17972 KB |
Output is correct |
47 |
Correct |
522 ms |
17916 KB |
Output is correct |
48 |
Correct |
509 ms |
17964 KB |
Output is correct |
49 |
Correct |
522 ms |
17656 KB |
Output is correct |
50 |
Correct |
530 ms |
18040 KB |
Output is correct |
51 |
Correct |
535 ms |
18108 KB |
Output is correct |
52 |
Correct |
572 ms |
17912 KB |
Output is correct |
53 |
Correct |
529 ms |
17912 KB |
Output is correct |
54 |
Correct |
539 ms |
17912 KB |
Output is correct |
55 |
Correct |
542 ms |
18040 KB |
Output is correct |
56 |
Correct |
813 ms |
17784 KB |
Output is correct |
57 |
Correct |
529 ms |
17784 KB |
Output is correct |
58 |
Correct |
523 ms |
17656 KB |
Output is correct |
59 |
Correct |
549 ms |
17912 KB |
Output is correct |
60 |
Correct |
530 ms |
18040 KB |
Output is correct |
61 |
Correct |
553 ms |
18040 KB |
Output is correct |
62 |
Correct |
532 ms |
17912 KB |
Output is correct |
63 |
Correct |
532 ms |
18040 KB |
Output is correct |
64 |
Correct |
508 ms |
17272 KB |
Output is correct |
65 |
Correct |
524 ms |
17988 KB |
Output is correct |
66 |
Correct |
532 ms |
17912 KB |
Output is correct |
67 |
Correct |
532 ms |
17912 KB |
Output is correct |
68 |
Correct |
545 ms |
17912 KB |
Output is correct |
69 |
Correct |
523 ms |
17912 KB |
Output is correct |
70 |
Correct |
520 ms |
17912 KB |
Output is correct |
71 |
Correct |
1266 ms |
37740 KB |
Output is correct |
72 |
Correct |
1351 ms |
55672 KB |
Output is correct |
73 |
Correct |
1354 ms |
58012 KB |
Output is correct |
74 |
Correct |
1491 ms |
58260 KB |
Output is correct |
75 |
Correct |
1646 ms |
60304 KB |
Output is correct |
76 |
Correct |
1490 ms |
58668 KB |
Output is correct |
77 |
Correct |
1526 ms |
58460 KB |
Output is correct |
78 |
Correct |
959 ms |
58404 KB |
Output is correct |
79 |
Correct |
1082 ms |
52216 KB |
Output is correct |
80 |
Correct |
1363 ms |
59380 KB |
Output is correct |
81 |
Correct |
1578 ms |
59384 KB |
Output is correct |
82 |
Correct |
1459 ms |
58264 KB |
Output is correct |
83 |
Correct |
1134 ms |
57592 KB |
Output is correct |
84 |
Correct |
1484 ms |
58360 KB |
Output is correct |
85 |
Correct |
1479 ms |
58504 KB |
Output is correct |
86 |
Correct |
858 ms |
52344 KB |
Output is correct |
87 |
Correct |
1334 ms |
55364 KB |
Output is correct |
88 |
Correct |
1086 ms |
52368 KB |
Output is correct |
89 |
Correct |
1317 ms |
57944 KB |
Output is correct |
90 |
Correct |
1462 ms |
58324 KB |
Output is correct |
91 |
Correct |
1166 ms |
57464 KB |
Output is correct |
92 |
Correct |
1458 ms |
58620 KB |
Output is correct |
93 |
Correct |
1504 ms |
58456 KB |
Output is correct |
94 |
Correct |
856 ms |
44552 KB |
Output is correct |
95 |
Correct |
1468 ms |
58524 KB |
Output is correct |
96 |
Correct |
1489 ms |
58392 KB |
Output is correct |
97 |
Correct |
1457 ms |
58384 KB |
Output is correct |
98 |
Correct |
1527 ms |
58220 KB |
Output is correct |
99 |
Correct |
1555 ms |
58320 KB |
Output is correct |
100 |
Correct |
1540 ms |
58356 KB |
Output is correct |