#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=(1<<20)+5;
const int M=1600050;
const int Q=1000050;
int l,n,q,lst[M],pw[20],ans[Q],dp[M],msk[N],qmask[Q],omask[Q],qv[Q];
char s[N],qs[25];
void calc(){
pw[0]=1;
for(int i=1;i<14;i++)pw[i]=pw[i-1]*3;
for(int i=0;i<M;i++){
vector<int> bits;
int x=i;
while(x>0)bits.push_back(x%3),x/=3;
lst[i]=-1;
for(int j=0;j<bits.size();j++)if(bits[j]==2)lst[i]=j;
}
}
void BruteForce(){
memset(dp,0,sizeof *dp);
for(int i=0;i<n;i++){
int sum=0,x=i,b=0;
while(x>0)sum+=(x%2)*pw[b++],x/=2;
dp[sum]+=(int)(s[i]-'0');
}
for(int i=0;i<M;i++)if(lst[i]!=-1){
dp[i]+=dp[i-2*pw[lst[i]]];
dp[i]+=dp[i-pw[lst[i]]];
}
while(q--){
scanf("%s",qs);
int sum=0;
for(int i=0;i<l;i++)sum+=(qs[i]=='0'?0:(qs[i]=='1'?1:2))*pw[l-i-1];
printf("%i\n",dp[sum]);
}
}
int main(){
scanf("%i%i",&l,&q);
scanf("%s",s);
n=(1<<l),calc();
if(l<=13){
BruteForce();
return 0;
}
for(int i=0;i<n;i++){
vector<int> bits;
int x=i;
while(x>0)bits.pb(x%2),x/=2;
while(bits.size()<l)bits.pb(0);
reverse(bits.begin(),bits.end());
msk[i]=0;
for(int j=7;j<l;j++)msk[i]+=bits[j]*pw[j-7];
}
for(int i=1;i<=q;i++){
scanf("%s",qs);
qv[i]=0;
for(int j=7;j<l;j++)qv[i]+=(qs[j]=='0'?0:(qs[j]=='1'?1:2))*pw[j-7];
qmask[i]=0;
for(int b=0;b<7;b++)if(qs[b]!='0')qmask[i]+=(1<<(6-b));
for(int b=0;b<7;b++)if(qs[b]=='1')omask[i]+=(1<<(6-b));
}
for(int i=0;i<128;i++){
memset(dp,0,M*sizeof *dp);
for(int j=0;j<(1<<(l-7));j++)dp[msk[(i<<(l-7))+j]]+=(int)(s[(i<<(l-7))+j]-'0');
for(int j=0;j<M;j++)if(lst[j]!=-1){
dp[j]+=dp[j-2*pw[lst[j]]];
dp[j]+=dp[j-pw[lst[j]]];
}
for(int j=1;j<=q;j++)if((qmask[j]&i)==i&&(omask[j]|i)==i)ans[j]+=dp[qv[j]];
}
for(int i=1;i<=q;i++)printf("%i\n",ans[i]);
return 0;
}
Compilation message
snake_escaping.cpp: In function 'void calc()':
snake_escaping.cpp:17:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for(int j=0;j<bits.size();j++)if(bits[j]==2)lst[i]=j;
| ~^~~~~~~~~~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:50:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
50 | while(bits.size()<l)bits.pb(0);
| ~~~~~~~~~~~^~
snake_escaping.cpp: In function 'void BruteForce()':
snake_escaping.cpp:32:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | scanf("%s",qs);
| ~~~~~^~~~~~~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:39:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | scanf("%i%i",&l,&q);
| ~~~~~^~~~~~~~~~~~~~
snake_escaping.cpp:40:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%s",s);
| ~~~~~^~~~~~~~
snake_escaping.cpp:56:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | scanf("%s",qs);
| ~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
255 ms |
12740 KB |
Output is correct |
2 |
Correct |
258 ms |
12732 KB |
Output is correct |
3 |
Correct |
268 ms |
12728 KB |
Output is correct |
4 |
Correct |
261 ms |
12788 KB |
Output is correct |
5 |
Correct |
267 ms |
12832 KB |
Output is correct |
6 |
Correct |
260 ms |
12860 KB |
Output is correct |
7 |
Correct |
279 ms |
12824 KB |
Output is correct |
8 |
Correct |
264 ms |
12792 KB |
Output is correct |
9 |
Correct |
265 ms |
12900 KB |
Output is correct |
10 |
Correct |
271 ms |
12724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
255 ms |
12740 KB |
Output is correct |
2 |
Correct |
258 ms |
12732 KB |
Output is correct |
3 |
Correct |
268 ms |
12728 KB |
Output is correct |
4 |
Correct |
261 ms |
12788 KB |
Output is correct |
5 |
Correct |
267 ms |
12832 KB |
Output is correct |
6 |
Correct |
260 ms |
12860 KB |
Output is correct |
7 |
Correct |
279 ms |
12824 KB |
Output is correct |
8 |
Correct |
264 ms |
12792 KB |
Output is correct |
9 |
Correct |
265 ms |
12900 KB |
Output is correct |
10 |
Correct |
271 ms |
12724 KB |
Output is correct |
11 |
Correct |
474 ms |
18884 KB |
Output is correct |
12 |
Correct |
492 ms |
18556 KB |
Output is correct |
13 |
Correct |
487 ms |
17664 KB |
Output is correct |
14 |
Correct |
481 ms |
17936 KB |
Output is correct |
15 |
Correct |
466 ms |
18876 KB |
Output is correct |
16 |
Correct |
478 ms |
18064 KB |
Output is correct |
17 |
Correct |
487 ms |
18016 KB |
Output is correct |
18 |
Correct |
475 ms |
19968 KB |
Output is correct |
19 |
Correct |
483 ms |
16956 KB |
Output is correct |
20 |
Correct |
460 ms |
18676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
255 ms |
12740 KB |
Output is correct |
2 |
Correct |
258 ms |
12732 KB |
Output is correct |
3 |
Correct |
268 ms |
12728 KB |
Output is correct |
4 |
Correct |
261 ms |
12788 KB |
Output is correct |
5 |
Correct |
267 ms |
12832 KB |
Output is correct |
6 |
Correct |
260 ms |
12860 KB |
Output is correct |
7 |
Correct |
279 ms |
12824 KB |
Output is correct |
8 |
Correct |
264 ms |
12792 KB |
Output is correct |
9 |
Correct |
265 ms |
12900 KB |
Output is correct |
10 |
Correct |
271 ms |
12724 KB |
Output is correct |
11 |
Correct |
474 ms |
18884 KB |
Output is correct |
12 |
Correct |
492 ms |
18556 KB |
Output is correct |
13 |
Correct |
487 ms |
17664 KB |
Output is correct |
14 |
Correct |
481 ms |
17936 KB |
Output is correct |
15 |
Correct |
466 ms |
18876 KB |
Output is correct |
16 |
Correct |
478 ms |
18064 KB |
Output is correct |
17 |
Correct |
487 ms |
18016 KB |
Output is correct |
18 |
Correct |
475 ms |
19968 KB |
Output is correct |
19 |
Correct |
483 ms |
16956 KB |
Output is correct |
20 |
Correct |
460 ms |
18676 KB |
Output is correct |
21 |
Correct |
475 ms |
19424 KB |
Output is correct |
22 |
Correct |
523 ms |
19668 KB |
Output is correct |
23 |
Correct |
555 ms |
18636 KB |
Output is correct |
24 |
Correct |
523 ms |
18312 KB |
Output is correct |
25 |
Correct |
513 ms |
20452 KB |
Output is correct |
26 |
Correct |
529 ms |
18904 KB |
Output is correct |
27 |
Correct |
548 ms |
18824 KB |
Output is correct |
28 |
Correct |
463 ms |
21308 KB |
Output is correct |
29 |
Correct |
503 ms |
17300 KB |
Output is correct |
30 |
Correct |
477 ms |
19496 KB |
Output is correct |
31 |
Correct |
526 ms |
19312 KB |
Output is correct |
32 |
Correct |
538 ms |
19256 KB |
Output is correct |
33 |
Correct |
494 ms |
18228 KB |
Output is correct |
34 |
Correct |
528 ms |
18288 KB |
Output is correct |
35 |
Correct |
538 ms |
18736 KB |
Output is correct |
36 |
Correct |
457 ms |
17444 KB |
Output is correct |
37 |
Correct |
517 ms |
19324 KB |
Output is correct |
38 |
Correct |
521 ms |
17336 KB |
Output is correct |
39 |
Correct |
517 ms |
18444 KB |
Output is correct |
40 |
Correct |
523 ms |
18304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
255 ms |
12740 KB |
Output is correct |
2 |
Correct |
258 ms |
12732 KB |
Output is correct |
3 |
Correct |
268 ms |
12728 KB |
Output is correct |
4 |
Correct |
261 ms |
12788 KB |
Output is correct |
5 |
Correct |
267 ms |
12832 KB |
Output is correct |
6 |
Correct |
260 ms |
12860 KB |
Output is correct |
7 |
Correct |
279 ms |
12824 KB |
Output is correct |
8 |
Correct |
264 ms |
12792 KB |
Output is correct |
9 |
Correct |
265 ms |
12900 KB |
Output is correct |
10 |
Correct |
271 ms |
12724 KB |
Output is correct |
11 |
Correct |
934 ms |
20028 KB |
Output is correct |
12 |
Correct |
917 ms |
20940 KB |
Output is correct |
13 |
Correct |
920 ms |
20952 KB |
Output is correct |
14 |
Correct |
949 ms |
20940 KB |
Output is correct |
15 |
Correct |
935 ms |
21148 KB |
Output is correct |
16 |
Correct |
933 ms |
21028 KB |
Output is correct |
17 |
Correct |
917 ms |
20980 KB |
Output is correct |
18 |
Correct |
905 ms |
20940 KB |
Output is correct |
19 |
Correct |
933 ms |
20860 KB |
Output is correct |
20 |
Correct |
920 ms |
21056 KB |
Output is correct |
21 |
Correct |
946 ms |
21148 KB |
Output is correct |
22 |
Correct |
935 ms |
20972 KB |
Output is correct |
23 |
Correct |
946 ms |
20924 KB |
Output is correct |
24 |
Correct |
911 ms |
20912 KB |
Output is correct |
25 |
Correct |
920 ms |
20912 KB |
Output is correct |
26 |
Correct |
921 ms |
20980 KB |
Output is correct |
27 |
Correct |
907 ms |
20912 KB |
Output is correct |
28 |
Correct |
911 ms |
20812 KB |
Output is correct |
29 |
Correct |
948 ms |
20928 KB |
Output is correct |
30 |
Correct |
923 ms |
21052 KB |
Output is correct |
31 |
Correct |
910 ms |
20864 KB |
Output is correct |
32 |
Correct |
922 ms |
20984 KB |
Output is correct |
33 |
Correct |
921 ms |
21012 KB |
Output is correct |
34 |
Correct |
901 ms |
20684 KB |
Output is correct |
35 |
Correct |
936 ms |
20940 KB |
Output is correct |
36 |
Correct |
983 ms |
21004 KB |
Output is correct |
37 |
Correct |
939 ms |
20924 KB |
Output is correct |
38 |
Correct |
927 ms |
21044 KB |
Output is correct |
39 |
Correct |
932 ms |
20928 KB |
Output is correct |
40 |
Correct |
935 ms |
20972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
255 ms |
12740 KB |
Output is correct |
2 |
Correct |
258 ms |
12732 KB |
Output is correct |
3 |
Correct |
268 ms |
12728 KB |
Output is correct |
4 |
Correct |
261 ms |
12788 KB |
Output is correct |
5 |
Correct |
267 ms |
12832 KB |
Output is correct |
6 |
Correct |
260 ms |
12860 KB |
Output is correct |
7 |
Correct |
279 ms |
12824 KB |
Output is correct |
8 |
Correct |
264 ms |
12792 KB |
Output is correct |
9 |
Correct |
265 ms |
12900 KB |
Output is correct |
10 |
Correct |
271 ms |
12724 KB |
Output is correct |
11 |
Correct |
474 ms |
18884 KB |
Output is correct |
12 |
Correct |
492 ms |
18556 KB |
Output is correct |
13 |
Correct |
487 ms |
17664 KB |
Output is correct |
14 |
Correct |
481 ms |
17936 KB |
Output is correct |
15 |
Correct |
466 ms |
18876 KB |
Output is correct |
16 |
Correct |
478 ms |
18064 KB |
Output is correct |
17 |
Correct |
487 ms |
18016 KB |
Output is correct |
18 |
Correct |
475 ms |
19968 KB |
Output is correct |
19 |
Correct |
483 ms |
16956 KB |
Output is correct |
20 |
Correct |
460 ms |
18676 KB |
Output is correct |
21 |
Correct |
475 ms |
19424 KB |
Output is correct |
22 |
Correct |
523 ms |
19668 KB |
Output is correct |
23 |
Correct |
555 ms |
18636 KB |
Output is correct |
24 |
Correct |
523 ms |
18312 KB |
Output is correct |
25 |
Correct |
513 ms |
20452 KB |
Output is correct |
26 |
Correct |
529 ms |
18904 KB |
Output is correct |
27 |
Correct |
548 ms |
18824 KB |
Output is correct |
28 |
Correct |
463 ms |
21308 KB |
Output is correct |
29 |
Correct |
503 ms |
17300 KB |
Output is correct |
30 |
Correct |
477 ms |
19496 KB |
Output is correct |
31 |
Correct |
526 ms |
19312 KB |
Output is correct |
32 |
Correct |
538 ms |
19256 KB |
Output is correct |
33 |
Correct |
494 ms |
18228 KB |
Output is correct |
34 |
Correct |
528 ms |
18288 KB |
Output is correct |
35 |
Correct |
538 ms |
18736 KB |
Output is correct |
36 |
Correct |
457 ms |
17444 KB |
Output is correct |
37 |
Correct |
517 ms |
19324 KB |
Output is correct |
38 |
Correct |
521 ms |
17336 KB |
Output is correct |
39 |
Correct |
517 ms |
18444 KB |
Output is correct |
40 |
Correct |
523 ms |
18304 KB |
Output is correct |
41 |
Correct |
934 ms |
20028 KB |
Output is correct |
42 |
Correct |
917 ms |
20940 KB |
Output is correct |
43 |
Correct |
920 ms |
20952 KB |
Output is correct |
44 |
Correct |
949 ms |
20940 KB |
Output is correct |
45 |
Correct |
935 ms |
21148 KB |
Output is correct |
46 |
Correct |
933 ms |
21028 KB |
Output is correct |
47 |
Correct |
917 ms |
20980 KB |
Output is correct |
48 |
Correct |
905 ms |
20940 KB |
Output is correct |
49 |
Correct |
933 ms |
20860 KB |
Output is correct |
50 |
Correct |
920 ms |
21056 KB |
Output is correct |
51 |
Correct |
946 ms |
21148 KB |
Output is correct |
52 |
Correct |
935 ms |
20972 KB |
Output is correct |
53 |
Correct |
946 ms |
20924 KB |
Output is correct |
54 |
Correct |
911 ms |
20912 KB |
Output is correct |
55 |
Correct |
920 ms |
20912 KB |
Output is correct |
56 |
Correct |
921 ms |
20980 KB |
Output is correct |
57 |
Correct |
907 ms |
20912 KB |
Output is correct |
58 |
Correct |
911 ms |
20812 KB |
Output is correct |
59 |
Correct |
948 ms |
20928 KB |
Output is correct |
60 |
Correct |
923 ms |
21052 KB |
Output is correct |
61 |
Correct |
910 ms |
20864 KB |
Output is correct |
62 |
Correct |
922 ms |
20984 KB |
Output is correct |
63 |
Correct |
921 ms |
21012 KB |
Output is correct |
64 |
Correct |
901 ms |
20684 KB |
Output is correct |
65 |
Correct |
936 ms |
20940 KB |
Output is correct |
66 |
Correct |
983 ms |
21004 KB |
Output is correct |
67 |
Correct |
939 ms |
20924 KB |
Output is correct |
68 |
Correct |
927 ms |
21044 KB |
Output is correct |
69 |
Correct |
932 ms |
20928 KB |
Output is correct |
70 |
Correct |
935 ms |
20972 KB |
Output is correct |
71 |
Correct |
1426 ms |
38592 KB |
Output is correct |
72 |
Correct |
1520 ms |
34976 KB |
Output is correct |
73 |
Correct |
1593 ms |
37464 KB |
Output is correct |
74 |
Correct |
1826 ms |
37632 KB |
Output is correct |
75 |
Execution timed out |
2053 ms |
61192 KB |
Time limit exceeded |
76 |
Halted |
0 ms |
0 KB |
- |