#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++){
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);
| ~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
286 ms |
12876 KB |
Output is correct |
2 |
Correct |
265 ms |
12768 KB |
Output is correct |
3 |
Correct |
347 ms |
12720 KB |
Output is correct |
4 |
Correct |
259 ms |
12880 KB |
Output is correct |
5 |
Correct |
301 ms |
12764 KB |
Output is correct |
6 |
Correct |
289 ms |
12736 KB |
Output is correct |
7 |
Correct |
268 ms |
12788 KB |
Output is correct |
8 |
Correct |
296 ms |
12768 KB |
Output is correct |
9 |
Correct |
264 ms |
12688 KB |
Output is correct |
10 |
Correct |
272 ms |
12696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
286 ms |
12876 KB |
Output is correct |
2 |
Correct |
265 ms |
12768 KB |
Output is correct |
3 |
Correct |
347 ms |
12720 KB |
Output is correct |
4 |
Correct |
259 ms |
12880 KB |
Output is correct |
5 |
Correct |
301 ms |
12764 KB |
Output is correct |
6 |
Correct |
289 ms |
12736 KB |
Output is correct |
7 |
Correct |
268 ms |
12788 KB |
Output is correct |
8 |
Correct |
296 ms |
12768 KB |
Output is correct |
9 |
Correct |
264 ms |
12688 KB |
Output is correct |
10 |
Correct |
272 ms |
12696 KB |
Output is correct |
11 |
Correct |
473 ms |
25904 KB |
Output is correct |
12 |
Correct |
504 ms |
25796 KB |
Output is correct |
13 |
Correct |
521 ms |
24828 KB |
Output is correct |
14 |
Correct |
559 ms |
24896 KB |
Output is correct |
15 |
Correct |
546 ms |
25864 KB |
Output is correct |
16 |
Correct |
510 ms |
25032 KB |
Output is correct |
17 |
Correct |
529 ms |
25068 KB |
Output is correct |
18 |
Correct |
541 ms |
26940 KB |
Output is correct |
19 |
Correct |
563 ms |
23912 KB |
Output is correct |
20 |
Correct |
459 ms |
25508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
286 ms |
12876 KB |
Output is correct |
2 |
Correct |
265 ms |
12768 KB |
Output is correct |
3 |
Correct |
347 ms |
12720 KB |
Output is correct |
4 |
Correct |
259 ms |
12880 KB |
Output is correct |
5 |
Correct |
301 ms |
12764 KB |
Output is correct |
6 |
Correct |
289 ms |
12736 KB |
Output is correct |
7 |
Correct |
268 ms |
12788 KB |
Output is correct |
8 |
Correct |
296 ms |
12768 KB |
Output is correct |
9 |
Correct |
264 ms |
12688 KB |
Output is correct |
10 |
Correct |
272 ms |
12696 KB |
Output is correct |
11 |
Correct |
473 ms |
25904 KB |
Output is correct |
12 |
Correct |
504 ms |
25796 KB |
Output is correct |
13 |
Correct |
521 ms |
24828 KB |
Output is correct |
14 |
Correct |
559 ms |
24896 KB |
Output is correct |
15 |
Correct |
546 ms |
25864 KB |
Output is correct |
16 |
Correct |
510 ms |
25032 KB |
Output is correct |
17 |
Correct |
529 ms |
25068 KB |
Output is correct |
18 |
Correct |
541 ms |
26940 KB |
Output is correct |
19 |
Correct |
563 ms |
23912 KB |
Output is correct |
20 |
Correct |
459 ms |
25508 KB |
Output is correct |
21 |
Correct |
492 ms |
28400 KB |
Output is correct |
22 |
Correct |
580 ms |
28564 KB |
Output is correct |
23 |
Correct |
581 ms |
27596 KB |
Output is correct |
24 |
Correct |
634 ms |
27440 KB |
Output is correct |
25 |
Correct |
508 ms |
29216 KB |
Output is correct |
26 |
Correct |
602 ms |
27672 KB |
Output is correct |
27 |
Correct |
576 ms |
27524 KB |
Output is correct |
28 |
Correct |
527 ms |
29920 KB |
Output is correct |
29 |
Correct |
552 ms |
25976 KB |
Output is correct |
30 |
Correct |
491 ms |
27792 KB |
Output is correct |
31 |
Correct |
618 ms |
27292 KB |
Output is correct |
32 |
Correct |
614 ms |
27308 KB |
Output is correct |
33 |
Correct |
550 ms |
26080 KB |
Output is correct |
34 |
Correct |
627 ms |
25976 KB |
Output is correct |
35 |
Correct |
610 ms |
26216 KB |
Output is correct |
36 |
Correct |
522 ms |
24992 KB |
Output is correct |
37 |
Correct |
532 ms |
26936 KB |
Output is correct |
38 |
Correct |
583 ms |
25208 KB |
Output is correct |
39 |
Correct |
576 ms |
26348 KB |
Output is correct |
40 |
Correct |
585 ms |
26496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
286 ms |
12876 KB |
Output is correct |
2 |
Correct |
265 ms |
12768 KB |
Output is correct |
3 |
Correct |
347 ms |
12720 KB |
Output is correct |
4 |
Correct |
259 ms |
12880 KB |
Output is correct |
5 |
Correct |
301 ms |
12764 KB |
Output is correct |
6 |
Correct |
289 ms |
12736 KB |
Output is correct |
7 |
Correct |
268 ms |
12788 KB |
Output is correct |
8 |
Correct |
296 ms |
12768 KB |
Output is correct |
9 |
Correct |
264 ms |
12688 KB |
Output is correct |
10 |
Correct |
272 ms |
12696 KB |
Output is correct |
11 |
Incorrect |
974 ms |
25380 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
286 ms |
12876 KB |
Output is correct |
2 |
Correct |
265 ms |
12768 KB |
Output is correct |
3 |
Correct |
347 ms |
12720 KB |
Output is correct |
4 |
Correct |
259 ms |
12880 KB |
Output is correct |
5 |
Correct |
301 ms |
12764 KB |
Output is correct |
6 |
Correct |
289 ms |
12736 KB |
Output is correct |
7 |
Correct |
268 ms |
12788 KB |
Output is correct |
8 |
Correct |
296 ms |
12768 KB |
Output is correct |
9 |
Correct |
264 ms |
12688 KB |
Output is correct |
10 |
Correct |
272 ms |
12696 KB |
Output is correct |
11 |
Correct |
473 ms |
25904 KB |
Output is correct |
12 |
Correct |
504 ms |
25796 KB |
Output is correct |
13 |
Correct |
521 ms |
24828 KB |
Output is correct |
14 |
Correct |
559 ms |
24896 KB |
Output is correct |
15 |
Correct |
546 ms |
25864 KB |
Output is correct |
16 |
Correct |
510 ms |
25032 KB |
Output is correct |
17 |
Correct |
529 ms |
25068 KB |
Output is correct |
18 |
Correct |
541 ms |
26940 KB |
Output is correct |
19 |
Correct |
563 ms |
23912 KB |
Output is correct |
20 |
Correct |
459 ms |
25508 KB |
Output is correct |
21 |
Correct |
492 ms |
28400 KB |
Output is correct |
22 |
Correct |
580 ms |
28564 KB |
Output is correct |
23 |
Correct |
581 ms |
27596 KB |
Output is correct |
24 |
Correct |
634 ms |
27440 KB |
Output is correct |
25 |
Correct |
508 ms |
29216 KB |
Output is correct |
26 |
Correct |
602 ms |
27672 KB |
Output is correct |
27 |
Correct |
576 ms |
27524 KB |
Output is correct |
28 |
Correct |
527 ms |
29920 KB |
Output is correct |
29 |
Correct |
552 ms |
25976 KB |
Output is correct |
30 |
Correct |
491 ms |
27792 KB |
Output is correct |
31 |
Correct |
618 ms |
27292 KB |
Output is correct |
32 |
Correct |
614 ms |
27308 KB |
Output is correct |
33 |
Correct |
550 ms |
26080 KB |
Output is correct |
34 |
Correct |
627 ms |
25976 KB |
Output is correct |
35 |
Correct |
610 ms |
26216 KB |
Output is correct |
36 |
Correct |
522 ms |
24992 KB |
Output is correct |
37 |
Correct |
532 ms |
26936 KB |
Output is correct |
38 |
Correct |
583 ms |
25208 KB |
Output is correct |
39 |
Correct |
576 ms |
26348 KB |
Output is correct |
40 |
Correct |
585 ms |
26496 KB |
Output is correct |
41 |
Incorrect |
974 ms |
25380 KB |
Output isn't correct |
42 |
Halted |
0 ms |
0 KB |
- |