#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>>n>>q;
M[0]=1;
for (int i=1; i<=13; i++)
M[i]=M[i-1]*3;
Mid=13;
if (n<=Mid) {
for (i=0; i<(1<<n); i++) {
cin>>c;
C[i]=c-'0';
x=0;
for (j=0; j<n; j++)
if (i&(1<<j)) x+=M[j];
dp[x]+=C[i];
}
for (i=0; i<M[n]; i++) {
x=i;
for (j=n-1; j>=0; j--)
if (x>=2*M[j]) x-=2*M[j],dp[i]=(dp[i-2*M[j]]+dp[i-M[j]]);
else
if (x>=M[j]) x-=M[j];
}
for (i=1; i<=q; i++) {
x=0;
cin>>s;
for (j=0; j<s.size(); j++)
if (s[s.size()-j-1]=='?') x+=2*M[j];
else
if (s[s.size()-j-1]=='1') x+=M[j];
printf("%d\n",dp[x]);
}
}
else {
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 (int j=0; j<M[Mid]; j++)
dp[j]=0;
for (j=0; j<(1<<Mid); j++) {
x=0;
for (int 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++) {
x=j;
if (Last2[j]==-1) continue;
dp[j]=(dp[j-2*M[Last2[j]]]+dp[j-M[Last2[j]]]);
}
for (j=1; j<=q; j++) {
ko=0;
for (int k=0; k<K; k++) {
if (Boz2[j]&(1<<k)) continue;
if ((Boz[j]&(1<<k)) && (i&(1<<k))) continue;
if (!(Boz[j]&(1<<k)) && !(i&(1<<k))) continue;
ko=1;
break;
}
if (!ko) 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 () {
^
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:43:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j=0; j<s.size(); j++)
~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
632 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
632 KB |
Output is correct |
4 |
Correct |
5 ms |
632 KB |
Output is correct |
5 |
Correct |
5 ms |
604 KB |
Output is correct |
6 |
Correct |
4 ms |
636 KB |
Output is correct |
7 |
Correct |
5 ms |
604 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
5 ms |
632 KB |
Output is correct |
10 |
Correct |
5 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
632 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
632 KB |
Output is correct |
4 |
Correct |
5 ms |
632 KB |
Output is correct |
5 |
Correct |
5 ms |
604 KB |
Output is correct |
6 |
Correct |
4 ms |
636 KB |
Output is correct |
7 |
Correct |
5 ms |
604 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
5 ms |
632 KB |
Output is correct |
10 |
Correct |
5 ms |
604 KB |
Output is correct |
11 |
Correct |
265 ms |
4624 KB |
Output is correct |
12 |
Correct |
273 ms |
4472 KB |
Output is correct |
13 |
Correct |
277 ms |
3548 KB |
Output is correct |
14 |
Correct |
289 ms |
3696 KB |
Output is correct |
15 |
Correct |
283 ms |
4600 KB |
Output is correct |
16 |
Correct |
297 ms |
3968 KB |
Output is correct |
17 |
Correct |
301 ms |
3824 KB |
Output is correct |
18 |
Correct |
206 ms |
5628 KB |
Output is correct |
19 |
Correct |
255 ms |
2664 KB |
Output is correct |
20 |
Correct |
270 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
632 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
632 KB |
Output is correct |
4 |
Correct |
5 ms |
632 KB |
Output is correct |
5 |
Correct |
5 ms |
604 KB |
Output is correct |
6 |
Correct |
4 ms |
636 KB |
Output is correct |
7 |
Correct |
5 ms |
604 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
5 ms |
632 KB |
Output is correct |
10 |
Correct |
5 ms |
604 KB |
Output is correct |
11 |
Correct |
265 ms |
4624 KB |
Output is correct |
12 |
Correct |
273 ms |
4472 KB |
Output is correct |
13 |
Correct |
277 ms |
3548 KB |
Output is correct |
14 |
Correct |
289 ms |
3696 KB |
Output is correct |
15 |
Correct |
283 ms |
4600 KB |
Output is correct |
16 |
Correct |
297 ms |
3968 KB |
Output is correct |
17 |
Correct |
301 ms |
3824 KB |
Output is correct |
18 |
Correct |
206 ms |
5628 KB |
Output is correct |
19 |
Correct |
255 ms |
2664 KB |
Output is correct |
20 |
Correct |
270 ms |
4444 KB |
Output is correct |
21 |
Correct |
353 ms |
10604 KB |
Output is correct |
22 |
Correct |
391 ms |
10872 KB |
Output is correct |
23 |
Correct |
435 ms |
10012 KB |
Output is correct |
24 |
Correct |
437 ms |
9848 KB |
Output is correct |
25 |
Correct |
471 ms |
11688 KB |
Output is correct |
26 |
Correct |
481 ms |
10104 KB |
Output is correct |
27 |
Correct |
493 ms |
10172 KB |
Output is correct |
28 |
Correct |
286 ms |
12664 KB |
Output is correct |
29 |
Correct |
360 ms |
8756 KB |
Output is correct |
30 |
Correct |
376 ms |
10872 KB |
Output is correct |
31 |
Correct |
466 ms |
10744 KB |
Output is correct |
32 |
Correct |
456 ms |
10764 KB |
Output is correct |
33 |
Correct |
382 ms |
9564 KB |
Output is correct |
34 |
Correct |
467 ms |
9724 KB |
Output is correct |
35 |
Correct |
489 ms |
10192 KB |
Output is correct |
36 |
Correct |
280 ms |
8760 KB |
Output is correct |
37 |
Correct |
372 ms |
10616 KB |
Output is correct |
38 |
Correct |
366 ms |
8736 KB |
Output is correct |
39 |
Correct |
460 ms |
9976 KB |
Output is correct |
40 |
Correct |
459 ms |
9700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
632 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
632 KB |
Output is correct |
4 |
Correct |
5 ms |
632 KB |
Output is correct |
5 |
Correct |
5 ms |
604 KB |
Output is correct |
6 |
Correct |
4 ms |
636 KB |
Output is correct |
7 |
Correct |
5 ms |
604 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
5 ms |
632 KB |
Output is correct |
10 |
Correct |
5 ms |
604 KB |
Output is correct |
11 |
Correct |
736 ms |
18040 KB |
Output is correct |
12 |
Correct |
719 ms |
17952 KB |
Output is correct |
13 |
Correct |
707 ms |
17916 KB |
Output is correct |
14 |
Correct |
725 ms |
17912 KB |
Output is correct |
15 |
Correct |
760 ms |
18040 KB |
Output is correct |
16 |
Correct |
710 ms |
18020 KB |
Output is correct |
17 |
Correct |
710 ms |
18040 KB |
Output is correct |
18 |
Correct |
637 ms |
18040 KB |
Output is correct |
19 |
Correct |
647 ms |
17656 KB |
Output is correct |
20 |
Correct |
698 ms |
18028 KB |
Output is correct |
21 |
Correct |
743 ms |
18140 KB |
Output is correct |
22 |
Correct |
720 ms |
18040 KB |
Output is correct |
23 |
Correct |
674 ms |
18040 KB |
Output is correct |
24 |
Correct |
716 ms |
17900 KB |
Output is correct |
25 |
Correct |
744 ms |
18040 KB |
Output is correct |
26 |
Correct |
609 ms |
17656 KB |
Output is correct |
27 |
Correct |
726 ms |
17784 KB |
Output is correct |
28 |
Correct |
677 ms |
17656 KB |
Output is correct |
29 |
Correct |
724 ms |
17912 KB |
Output is correct |
30 |
Correct |
706 ms |
18040 KB |
Output is correct |
31 |
Correct |
710 ms |
17936 KB |
Output is correct |
32 |
Correct |
718 ms |
18040 KB |
Output is correct |
33 |
Correct |
714 ms |
18012 KB |
Output is correct |
34 |
Correct |
583 ms |
17244 KB |
Output is correct |
35 |
Correct |
731 ms |
17992 KB |
Output is correct |
36 |
Correct |
756 ms |
17912 KB |
Output is correct |
37 |
Correct |
731 ms |
18012 KB |
Output is correct |
38 |
Correct |
731 ms |
18040 KB |
Output is correct |
39 |
Correct |
722 ms |
18040 KB |
Output is correct |
40 |
Correct |
717 ms |
17912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
632 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
632 KB |
Output is correct |
4 |
Correct |
5 ms |
632 KB |
Output is correct |
5 |
Correct |
5 ms |
604 KB |
Output is correct |
6 |
Correct |
4 ms |
636 KB |
Output is correct |
7 |
Correct |
5 ms |
604 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
5 ms |
632 KB |
Output is correct |
10 |
Correct |
5 ms |
604 KB |
Output is correct |
11 |
Correct |
265 ms |
4624 KB |
Output is correct |
12 |
Correct |
273 ms |
4472 KB |
Output is correct |
13 |
Correct |
277 ms |
3548 KB |
Output is correct |
14 |
Correct |
289 ms |
3696 KB |
Output is correct |
15 |
Correct |
283 ms |
4600 KB |
Output is correct |
16 |
Correct |
297 ms |
3968 KB |
Output is correct |
17 |
Correct |
301 ms |
3824 KB |
Output is correct |
18 |
Correct |
206 ms |
5628 KB |
Output is correct |
19 |
Correct |
255 ms |
2664 KB |
Output is correct |
20 |
Correct |
270 ms |
4444 KB |
Output is correct |
21 |
Correct |
353 ms |
10604 KB |
Output is correct |
22 |
Correct |
391 ms |
10872 KB |
Output is correct |
23 |
Correct |
435 ms |
10012 KB |
Output is correct |
24 |
Correct |
437 ms |
9848 KB |
Output is correct |
25 |
Correct |
471 ms |
11688 KB |
Output is correct |
26 |
Correct |
481 ms |
10104 KB |
Output is correct |
27 |
Correct |
493 ms |
10172 KB |
Output is correct |
28 |
Correct |
286 ms |
12664 KB |
Output is correct |
29 |
Correct |
360 ms |
8756 KB |
Output is correct |
30 |
Correct |
376 ms |
10872 KB |
Output is correct |
31 |
Correct |
466 ms |
10744 KB |
Output is correct |
32 |
Correct |
456 ms |
10764 KB |
Output is correct |
33 |
Correct |
382 ms |
9564 KB |
Output is correct |
34 |
Correct |
467 ms |
9724 KB |
Output is correct |
35 |
Correct |
489 ms |
10192 KB |
Output is correct |
36 |
Correct |
280 ms |
8760 KB |
Output is correct |
37 |
Correct |
372 ms |
10616 KB |
Output is correct |
38 |
Correct |
366 ms |
8736 KB |
Output is correct |
39 |
Correct |
460 ms |
9976 KB |
Output is correct |
40 |
Correct |
459 ms |
9700 KB |
Output is correct |
41 |
Correct |
736 ms |
18040 KB |
Output is correct |
42 |
Correct |
719 ms |
17952 KB |
Output is correct |
43 |
Correct |
707 ms |
17916 KB |
Output is correct |
44 |
Correct |
725 ms |
17912 KB |
Output is correct |
45 |
Correct |
760 ms |
18040 KB |
Output is correct |
46 |
Correct |
710 ms |
18020 KB |
Output is correct |
47 |
Correct |
710 ms |
18040 KB |
Output is correct |
48 |
Correct |
637 ms |
18040 KB |
Output is correct |
49 |
Correct |
647 ms |
17656 KB |
Output is correct |
50 |
Correct |
698 ms |
18028 KB |
Output is correct |
51 |
Correct |
743 ms |
18140 KB |
Output is correct |
52 |
Correct |
720 ms |
18040 KB |
Output is correct |
53 |
Correct |
674 ms |
18040 KB |
Output is correct |
54 |
Correct |
716 ms |
17900 KB |
Output is correct |
55 |
Correct |
744 ms |
18040 KB |
Output is correct |
56 |
Correct |
609 ms |
17656 KB |
Output is correct |
57 |
Correct |
726 ms |
17784 KB |
Output is correct |
58 |
Correct |
677 ms |
17656 KB |
Output is correct |
59 |
Correct |
724 ms |
17912 KB |
Output is correct |
60 |
Correct |
706 ms |
18040 KB |
Output is correct |
61 |
Correct |
710 ms |
17936 KB |
Output is correct |
62 |
Correct |
718 ms |
18040 KB |
Output is correct |
63 |
Correct |
714 ms |
18012 KB |
Output is correct |
64 |
Correct |
583 ms |
17244 KB |
Output is correct |
65 |
Correct |
731 ms |
17992 KB |
Output is correct |
66 |
Correct |
756 ms |
17912 KB |
Output is correct |
67 |
Correct |
731 ms |
18012 KB |
Output is correct |
68 |
Correct |
731 ms |
18040 KB |
Output is correct |
69 |
Correct |
722 ms |
18040 KB |
Output is correct |
70 |
Correct |
717 ms |
17912 KB |
Output is correct |
71 |
Execution timed out |
2051 ms |
54264 KB |
Time limit exceeded |
72 |
Halted |
0 ms |
0 KB |
- |