#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=(1<<20)+5;
const int M=1594327;
const int Q=1000050;
int l,n,q,a[N],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();
for(int i=0;i<n;i++)a[i]=(int)(s[i]-'0');
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));
}
int nsz=(1<<(l-7));
for(int i=0;i<128;i++){
memset(dp,0,M*sizeof *dp);
for(int j=0;j<nsz;j++)dp[msk[(i<<(l-7))+j]]+=a[(i<<(l-7))+j];
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:51:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
51 | 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:57:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | scanf("%s",qs);
| ~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
276 ms |
12768 KB |
Output is correct |
2 |
Correct |
279 ms |
12704 KB |
Output is correct |
3 |
Correct |
287 ms |
12724 KB |
Output is correct |
4 |
Correct |
278 ms |
12688 KB |
Output is correct |
5 |
Correct |
289 ms |
12716 KB |
Output is correct |
6 |
Correct |
276 ms |
12760 KB |
Output is correct |
7 |
Correct |
294 ms |
12876 KB |
Output is correct |
8 |
Correct |
284 ms |
12728 KB |
Output is correct |
9 |
Correct |
278 ms |
12720 KB |
Output is correct |
10 |
Correct |
286 ms |
12760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
276 ms |
12768 KB |
Output is correct |
2 |
Correct |
279 ms |
12704 KB |
Output is correct |
3 |
Correct |
287 ms |
12724 KB |
Output is correct |
4 |
Correct |
278 ms |
12688 KB |
Output is correct |
5 |
Correct |
289 ms |
12716 KB |
Output is correct |
6 |
Correct |
276 ms |
12760 KB |
Output is correct |
7 |
Correct |
294 ms |
12876 KB |
Output is correct |
8 |
Correct |
284 ms |
12728 KB |
Output is correct |
9 |
Correct |
278 ms |
12720 KB |
Output is correct |
10 |
Correct |
286 ms |
12760 KB |
Output is correct |
11 |
Correct |
502 ms |
18508 KB |
Output is correct |
12 |
Correct |
550 ms |
18212 KB |
Output is correct |
13 |
Correct |
485 ms |
17580 KB |
Output is correct |
14 |
Correct |
488 ms |
17588 KB |
Output is correct |
15 |
Correct |
478 ms |
18488 KB |
Output is correct |
16 |
Correct |
472 ms |
17688 KB |
Output is correct |
17 |
Correct |
488 ms |
17392 KB |
Output is correct |
18 |
Correct |
461 ms |
19260 KB |
Output is correct |
19 |
Correct |
488 ms |
16280 KB |
Output is correct |
20 |
Correct |
450 ms |
17944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
276 ms |
12768 KB |
Output is correct |
2 |
Correct |
279 ms |
12704 KB |
Output is correct |
3 |
Correct |
287 ms |
12724 KB |
Output is correct |
4 |
Correct |
278 ms |
12688 KB |
Output is correct |
5 |
Correct |
289 ms |
12716 KB |
Output is correct |
6 |
Correct |
276 ms |
12760 KB |
Output is correct |
7 |
Correct |
294 ms |
12876 KB |
Output is correct |
8 |
Correct |
284 ms |
12728 KB |
Output is correct |
9 |
Correct |
278 ms |
12720 KB |
Output is correct |
10 |
Correct |
286 ms |
12760 KB |
Output is correct |
11 |
Correct |
502 ms |
18508 KB |
Output is correct |
12 |
Correct |
550 ms |
18212 KB |
Output is correct |
13 |
Correct |
485 ms |
17580 KB |
Output is correct |
14 |
Correct |
488 ms |
17588 KB |
Output is correct |
15 |
Correct |
478 ms |
18488 KB |
Output is correct |
16 |
Correct |
472 ms |
17688 KB |
Output is correct |
17 |
Correct |
488 ms |
17392 KB |
Output is correct |
18 |
Correct |
461 ms |
19260 KB |
Output is correct |
19 |
Correct |
488 ms |
16280 KB |
Output is correct |
20 |
Correct |
450 ms |
17944 KB |
Output is correct |
21 |
Correct |
489 ms |
18656 KB |
Output is correct |
22 |
Correct |
511 ms |
18048 KB |
Output is correct |
23 |
Correct |
523 ms |
16972 KB |
Output is correct |
24 |
Correct |
523 ms |
16896 KB |
Output is correct |
25 |
Correct |
496 ms |
18832 KB |
Output is correct |
26 |
Correct |
529 ms |
17272 KB |
Output is correct |
27 |
Correct |
545 ms |
17384 KB |
Output is correct |
28 |
Correct |
465 ms |
19912 KB |
Output is correct |
29 |
Correct |
516 ms |
15776 KB |
Output is correct |
30 |
Correct |
465 ms |
17996 KB |
Output is correct |
31 |
Correct |
505 ms |
17704 KB |
Output is correct |
32 |
Correct |
529 ms |
17636 KB |
Output is correct |
33 |
Correct |
479 ms |
16620 KB |
Output is correct |
34 |
Correct |
533 ms |
16672 KB |
Output is correct |
35 |
Correct |
548 ms |
16956 KB |
Output is correct |
36 |
Correct |
470 ms |
15636 KB |
Output is correct |
37 |
Correct |
509 ms |
17596 KB |
Output is correct |
38 |
Correct |
513 ms |
15712 KB |
Output is correct |
39 |
Correct |
520 ms |
16972 KB |
Output is correct |
40 |
Correct |
511 ms |
16728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
276 ms |
12768 KB |
Output is correct |
2 |
Correct |
279 ms |
12704 KB |
Output is correct |
3 |
Correct |
287 ms |
12724 KB |
Output is correct |
4 |
Correct |
278 ms |
12688 KB |
Output is correct |
5 |
Correct |
289 ms |
12716 KB |
Output is correct |
6 |
Correct |
276 ms |
12760 KB |
Output is correct |
7 |
Correct |
294 ms |
12876 KB |
Output is correct |
8 |
Correct |
284 ms |
12728 KB |
Output is correct |
9 |
Correct |
278 ms |
12720 KB |
Output is correct |
10 |
Correct |
286 ms |
12760 KB |
Output is correct |
11 |
Correct |
933 ms |
23416 KB |
Output is correct |
12 |
Correct |
966 ms |
23040 KB |
Output is correct |
13 |
Correct |
929 ms |
23244 KB |
Output is correct |
14 |
Correct |
935 ms |
23332 KB |
Output is correct |
15 |
Correct |
1008 ms |
23376 KB |
Output is correct |
16 |
Correct |
1025 ms |
23320 KB |
Output is correct |
17 |
Correct |
1020 ms |
23244 KB |
Output is correct |
18 |
Correct |
987 ms |
23252 KB |
Output is correct |
19 |
Correct |
1061 ms |
23140 KB |
Output is correct |
20 |
Correct |
1059 ms |
23324 KB |
Output is correct |
21 |
Correct |
1136 ms |
23324 KB |
Output is correct |
22 |
Correct |
1048 ms |
23196 KB |
Output is correct |
23 |
Correct |
1126 ms |
23192 KB |
Output is correct |
24 |
Correct |
1037 ms |
23196 KB |
Output is correct |
25 |
Correct |
1014 ms |
23200 KB |
Output is correct |
26 |
Correct |
920 ms |
23212 KB |
Output is correct |
27 |
Correct |
937 ms |
23240 KB |
Output is correct |
28 |
Correct |
959 ms |
23072 KB |
Output is correct |
29 |
Correct |
965 ms |
23400 KB |
Output is correct |
30 |
Correct |
941 ms |
23204 KB |
Output is correct |
31 |
Correct |
1018 ms |
23148 KB |
Output is correct |
32 |
Correct |
960 ms |
23176 KB |
Output is correct |
33 |
Correct |
950 ms |
22980 KB |
Output is correct |
34 |
Correct |
918 ms |
22732 KB |
Output is correct |
35 |
Correct |
958 ms |
22908 KB |
Output is correct |
36 |
Correct |
943 ms |
23012 KB |
Output is correct |
37 |
Correct |
947 ms |
22952 KB |
Output is correct |
38 |
Correct |
996 ms |
23056 KB |
Output is correct |
39 |
Correct |
951 ms |
23096 KB |
Output is correct |
40 |
Correct |
960 ms |
23024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
276 ms |
12768 KB |
Output is correct |
2 |
Correct |
279 ms |
12704 KB |
Output is correct |
3 |
Correct |
287 ms |
12724 KB |
Output is correct |
4 |
Correct |
278 ms |
12688 KB |
Output is correct |
5 |
Correct |
289 ms |
12716 KB |
Output is correct |
6 |
Correct |
276 ms |
12760 KB |
Output is correct |
7 |
Correct |
294 ms |
12876 KB |
Output is correct |
8 |
Correct |
284 ms |
12728 KB |
Output is correct |
9 |
Correct |
278 ms |
12720 KB |
Output is correct |
10 |
Correct |
286 ms |
12760 KB |
Output is correct |
11 |
Correct |
502 ms |
18508 KB |
Output is correct |
12 |
Correct |
550 ms |
18212 KB |
Output is correct |
13 |
Correct |
485 ms |
17580 KB |
Output is correct |
14 |
Correct |
488 ms |
17588 KB |
Output is correct |
15 |
Correct |
478 ms |
18488 KB |
Output is correct |
16 |
Correct |
472 ms |
17688 KB |
Output is correct |
17 |
Correct |
488 ms |
17392 KB |
Output is correct |
18 |
Correct |
461 ms |
19260 KB |
Output is correct |
19 |
Correct |
488 ms |
16280 KB |
Output is correct |
20 |
Correct |
450 ms |
17944 KB |
Output is correct |
21 |
Correct |
489 ms |
18656 KB |
Output is correct |
22 |
Correct |
511 ms |
18048 KB |
Output is correct |
23 |
Correct |
523 ms |
16972 KB |
Output is correct |
24 |
Correct |
523 ms |
16896 KB |
Output is correct |
25 |
Correct |
496 ms |
18832 KB |
Output is correct |
26 |
Correct |
529 ms |
17272 KB |
Output is correct |
27 |
Correct |
545 ms |
17384 KB |
Output is correct |
28 |
Correct |
465 ms |
19912 KB |
Output is correct |
29 |
Correct |
516 ms |
15776 KB |
Output is correct |
30 |
Correct |
465 ms |
17996 KB |
Output is correct |
31 |
Correct |
505 ms |
17704 KB |
Output is correct |
32 |
Correct |
529 ms |
17636 KB |
Output is correct |
33 |
Correct |
479 ms |
16620 KB |
Output is correct |
34 |
Correct |
533 ms |
16672 KB |
Output is correct |
35 |
Correct |
548 ms |
16956 KB |
Output is correct |
36 |
Correct |
470 ms |
15636 KB |
Output is correct |
37 |
Correct |
509 ms |
17596 KB |
Output is correct |
38 |
Correct |
513 ms |
15712 KB |
Output is correct |
39 |
Correct |
520 ms |
16972 KB |
Output is correct |
40 |
Correct |
511 ms |
16728 KB |
Output is correct |
41 |
Correct |
933 ms |
23416 KB |
Output is correct |
42 |
Correct |
966 ms |
23040 KB |
Output is correct |
43 |
Correct |
929 ms |
23244 KB |
Output is correct |
44 |
Correct |
935 ms |
23332 KB |
Output is correct |
45 |
Correct |
1008 ms |
23376 KB |
Output is correct |
46 |
Correct |
1025 ms |
23320 KB |
Output is correct |
47 |
Correct |
1020 ms |
23244 KB |
Output is correct |
48 |
Correct |
987 ms |
23252 KB |
Output is correct |
49 |
Correct |
1061 ms |
23140 KB |
Output is correct |
50 |
Correct |
1059 ms |
23324 KB |
Output is correct |
51 |
Correct |
1136 ms |
23324 KB |
Output is correct |
52 |
Correct |
1048 ms |
23196 KB |
Output is correct |
53 |
Correct |
1126 ms |
23192 KB |
Output is correct |
54 |
Correct |
1037 ms |
23196 KB |
Output is correct |
55 |
Correct |
1014 ms |
23200 KB |
Output is correct |
56 |
Correct |
920 ms |
23212 KB |
Output is correct |
57 |
Correct |
937 ms |
23240 KB |
Output is correct |
58 |
Correct |
959 ms |
23072 KB |
Output is correct |
59 |
Correct |
965 ms |
23400 KB |
Output is correct |
60 |
Correct |
941 ms |
23204 KB |
Output is correct |
61 |
Correct |
1018 ms |
23148 KB |
Output is correct |
62 |
Correct |
960 ms |
23176 KB |
Output is correct |
63 |
Correct |
950 ms |
22980 KB |
Output is correct |
64 |
Correct |
918 ms |
22732 KB |
Output is correct |
65 |
Correct |
958 ms |
22908 KB |
Output is correct |
66 |
Correct |
943 ms |
23012 KB |
Output is correct |
67 |
Correct |
947 ms |
22952 KB |
Output is correct |
68 |
Correct |
996 ms |
23056 KB |
Output is correct |
69 |
Correct |
951 ms |
23096 KB |
Output is correct |
70 |
Correct |
960 ms |
23024 KB |
Output is correct |
71 |
Correct |
1507 ms |
42664 KB |
Output is correct |
72 |
Correct |
1522 ms |
39060 KB |
Output is correct |
73 |
Correct |
1633 ms |
41228 KB |
Output is correct |
74 |
Correct |
1845 ms |
41620 KB |
Output is correct |
75 |
Execution timed out |
2079 ms |
42260 KB |
Time limit exceeded |
76 |
Halted |
0 ms |
0 KB |
- |