#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];
char s[N],qs[25];
vector<pair<int,int>> Qs[128];
vector<int> ids[128];
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);
msk[i]=0;
for(int j=7;j<l;j++)msk[i]+=bits[j]*pw[j-7];
}
for(int i=0;i<n;i++)ids[i&127].pb(i);
for(int i=1;i<=q;i++){
scanf("%s",qs);
int v=0;
for(int j=7;j<l;j++)v+=(qs[j]=='0'?0:(qs[j]=='1'?1:2))*pw[l-j-1];
int mask=0;
for(int b=0;b<7;b++)if(qs[b]!='0')mask+=(1<<b);
for(int j=0;j<128;j++)if((mask&j)==j)Qs[j].pb({v,i});
}
for(int i=0;i<128;i++){
for(int j=0;j<M;j++)dp[j]=0;
for(int j:ids[i])dp[msk[j]]+=(int)(s[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(auto qs:Qs[i])ans[qs.second]+=dp[qs.first];
}
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:19:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | 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:52:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
52 | while(bits.size()<l)bits.pb(0);
| ~~~~~~~~~~~^~
snake_escaping.cpp: In function 'void BruteForce()':
snake_escaping.cpp:34:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | scanf("%s",qs);
| ~~~~~^~~~~~~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:41:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | scanf("%i%i",&l,&q);
| ~~~~~^~~~~~~~~~~~~~
snake_escaping.cpp:42:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | scanf("%s",s);
| ~~~~~^~~~~~~~
snake_escaping.cpp:58:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%s",qs);
| ~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
269 ms |
12736 KB |
Output is correct |
2 |
Correct |
266 ms |
12740 KB |
Output is correct |
3 |
Correct |
280 ms |
12748 KB |
Output is correct |
4 |
Correct |
263 ms |
12800 KB |
Output is correct |
5 |
Correct |
264 ms |
12792 KB |
Output is correct |
6 |
Correct |
279 ms |
12888 KB |
Output is correct |
7 |
Correct |
268 ms |
12920 KB |
Output is correct |
8 |
Correct |
277 ms |
12872 KB |
Output is correct |
9 |
Correct |
281 ms |
12828 KB |
Output is correct |
10 |
Correct |
271 ms |
12928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
269 ms |
12736 KB |
Output is correct |
2 |
Correct |
266 ms |
12740 KB |
Output is correct |
3 |
Correct |
280 ms |
12748 KB |
Output is correct |
4 |
Correct |
263 ms |
12800 KB |
Output is correct |
5 |
Correct |
264 ms |
12792 KB |
Output is correct |
6 |
Correct |
279 ms |
12888 KB |
Output is correct |
7 |
Correct |
268 ms |
12920 KB |
Output is correct |
8 |
Correct |
277 ms |
12872 KB |
Output is correct |
9 |
Correct |
281 ms |
12828 KB |
Output is correct |
10 |
Correct |
271 ms |
12928 KB |
Output is correct |
11 |
Correct |
476 ms |
20564 KB |
Output is correct |
12 |
Correct |
509 ms |
20296 KB |
Output is correct |
13 |
Correct |
478 ms |
19472 KB |
Output is correct |
14 |
Correct |
475 ms |
19732 KB |
Output is correct |
15 |
Correct |
478 ms |
20672 KB |
Output is correct |
16 |
Correct |
479 ms |
19932 KB |
Output is correct |
17 |
Correct |
487 ms |
19864 KB |
Output is correct |
18 |
Correct |
464 ms |
21628 KB |
Output is correct |
19 |
Correct |
489 ms |
18632 KB |
Output is correct |
20 |
Correct |
470 ms |
20228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
269 ms |
12736 KB |
Output is correct |
2 |
Correct |
266 ms |
12740 KB |
Output is correct |
3 |
Correct |
280 ms |
12748 KB |
Output is correct |
4 |
Correct |
263 ms |
12800 KB |
Output is correct |
5 |
Correct |
264 ms |
12792 KB |
Output is correct |
6 |
Correct |
279 ms |
12888 KB |
Output is correct |
7 |
Correct |
268 ms |
12920 KB |
Output is correct |
8 |
Correct |
277 ms |
12872 KB |
Output is correct |
9 |
Correct |
281 ms |
12828 KB |
Output is correct |
10 |
Correct |
271 ms |
12928 KB |
Output is correct |
11 |
Correct |
476 ms |
20564 KB |
Output is correct |
12 |
Correct |
509 ms |
20296 KB |
Output is correct |
13 |
Correct |
478 ms |
19472 KB |
Output is correct |
14 |
Correct |
475 ms |
19732 KB |
Output is correct |
15 |
Correct |
478 ms |
20672 KB |
Output is correct |
16 |
Correct |
479 ms |
19932 KB |
Output is correct |
17 |
Correct |
487 ms |
19864 KB |
Output is correct |
18 |
Correct |
464 ms |
21628 KB |
Output is correct |
19 |
Correct |
489 ms |
18632 KB |
Output is correct |
20 |
Correct |
470 ms |
20228 KB |
Output is correct |
21 |
Correct |
468 ms |
22256 KB |
Output is correct |
22 |
Correct |
526 ms |
22356 KB |
Output is correct |
23 |
Correct |
531 ms |
21468 KB |
Output is correct |
24 |
Correct |
537 ms |
21204 KB |
Output is correct |
25 |
Correct |
495 ms |
23360 KB |
Output is correct |
26 |
Correct |
519 ms |
21952 KB |
Output is correct |
27 |
Correct |
538 ms |
21924 KB |
Output is correct |
28 |
Correct |
476 ms |
24520 KB |
Output is correct |
29 |
Correct |
501 ms |
20320 KB |
Output is correct |
30 |
Correct |
500 ms |
22540 KB |
Output is correct |
31 |
Correct |
512 ms |
22308 KB |
Output is correct |
32 |
Correct |
508 ms |
22428 KB |
Output is correct |
33 |
Correct |
490 ms |
20920 KB |
Output is correct |
34 |
Correct |
538 ms |
20864 KB |
Output is correct |
35 |
Correct |
533 ms |
21240 KB |
Output is correct |
36 |
Correct |
474 ms |
19920 KB |
Output is correct |
37 |
Correct |
504 ms |
21744 KB |
Output is correct |
38 |
Correct |
508 ms |
19808 KB |
Output is correct |
39 |
Correct |
528 ms |
20828 KB |
Output is correct |
40 |
Correct |
524 ms |
20792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
269 ms |
12736 KB |
Output is correct |
2 |
Correct |
266 ms |
12740 KB |
Output is correct |
3 |
Correct |
280 ms |
12748 KB |
Output is correct |
4 |
Correct |
263 ms |
12800 KB |
Output is correct |
5 |
Correct |
264 ms |
12792 KB |
Output is correct |
6 |
Correct |
279 ms |
12888 KB |
Output is correct |
7 |
Correct |
268 ms |
12920 KB |
Output is correct |
8 |
Correct |
277 ms |
12872 KB |
Output is correct |
9 |
Correct |
281 ms |
12828 KB |
Output is correct |
10 |
Correct |
271 ms |
12928 KB |
Output is correct |
11 |
Runtime error |
570 ms |
65536 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
269 ms |
12736 KB |
Output is correct |
2 |
Correct |
266 ms |
12740 KB |
Output is correct |
3 |
Correct |
280 ms |
12748 KB |
Output is correct |
4 |
Correct |
263 ms |
12800 KB |
Output is correct |
5 |
Correct |
264 ms |
12792 KB |
Output is correct |
6 |
Correct |
279 ms |
12888 KB |
Output is correct |
7 |
Correct |
268 ms |
12920 KB |
Output is correct |
8 |
Correct |
277 ms |
12872 KB |
Output is correct |
9 |
Correct |
281 ms |
12828 KB |
Output is correct |
10 |
Correct |
271 ms |
12928 KB |
Output is correct |
11 |
Correct |
476 ms |
20564 KB |
Output is correct |
12 |
Correct |
509 ms |
20296 KB |
Output is correct |
13 |
Correct |
478 ms |
19472 KB |
Output is correct |
14 |
Correct |
475 ms |
19732 KB |
Output is correct |
15 |
Correct |
478 ms |
20672 KB |
Output is correct |
16 |
Correct |
479 ms |
19932 KB |
Output is correct |
17 |
Correct |
487 ms |
19864 KB |
Output is correct |
18 |
Correct |
464 ms |
21628 KB |
Output is correct |
19 |
Correct |
489 ms |
18632 KB |
Output is correct |
20 |
Correct |
470 ms |
20228 KB |
Output is correct |
21 |
Correct |
468 ms |
22256 KB |
Output is correct |
22 |
Correct |
526 ms |
22356 KB |
Output is correct |
23 |
Correct |
531 ms |
21468 KB |
Output is correct |
24 |
Correct |
537 ms |
21204 KB |
Output is correct |
25 |
Correct |
495 ms |
23360 KB |
Output is correct |
26 |
Correct |
519 ms |
21952 KB |
Output is correct |
27 |
Correct |
538 ms |
21924 KB |
Output is correct |
28 |
Correct |
476 ms |
24520 KB |
Output is correct |
29 |
Correct |
501 ms |
20320 KB |
Output is correct |
30 |
Correct |
500 ms |
22540 KB |
Output is correct |
31 |
Correct |
512 ms |
22308 KB |
Output is correct |
32 |
Correct |
508 ms |
22428 KB |
Output is correct |
33 |
Correct |
490 ms |
20920 KB |
Output is correct |
34 |
Correct |
538 ms |
20864 KB |
Output is correct |
35 |
Correct |
533 ms |
21240 KB |
Output is correct |
36 |
Correct |
474 ms |
19920 KB |
Output is correct |
37 |
Correct |
504 ms |
21744 KB |
Output is correct |
38 |
Correct |
508 ms |
19808 KB |
Output is correct |
39 |
Correct |
528 ms |
20828 KB |
Output is correct |
40 |
Correct |
524 ms |
20792 KB |
Output is correct |
41 |
Runtime error |
570 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Halted |
0 ms |
0 KB |
- |