#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x) cerr<< #x <<" = "<< x<<endl;
#define BLOK 13
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int n,q;
const int N=(1<<20)+5;
int arr[N];
int dp[N*2],pw[22],ans[N];
char s[N],t[22];
vector<PII>adj[22];
bitset<N>ok[130];
void rec(int pos,int mask,int bit,int last){
if(pos==min(n,BLOK)){
adj[bit].pb(mp(mask,last));
return;
}
rec(pos+1,mask*3,bit,last);
rec(pos+1,mask*3+1,bit,last);
rec(pos+1,mask*3+2,bit+1,min(n,BLOK)-pos-1);
}
int two(int x){
int bit=0,res=0;
while(x>=1){
assert(x%3<2);
if(x%3)
res+=(1<<bit);
x/=3;bit++;
}
return res;
}
void f(int l,int r,int mask,int ind){
if(l==r){
ok[mask][ind]=1;
//~ query[mask].pb(mp(suf,ind));
return;
}
if(t[l]=='0')
f(l+1,r,mask*2,ind);
if(t[l]=='1')
f(l+1,r,mask*2+1,ind);
if(t[l]=='?'){
f(l+1,r,mask*2,ind);
f(l+1,r,mask*2+1,ind);
}
}
int main(){
//~ freopen("file.in", "r", stdin);
scanf("%d%d",&n,&q);
scanf("%s",s);
pw[0]=1;
for(int i=1;i<=BLOK;i++)
pw[i]=pw[i-1]*3;
if(n<=BLOK){
rec(0,0,0,-1);
tr(it,adj[0])
dp[it->ff]=s[two(it->ff)]-'0';
for(int j=1;j<=n;j++)
tr(it,adj[j])
dp[it->ff]=dp[it->ff-pw[it->ss]]+dp[it->ff-2*pw[it->ss]];
for(int i=1;i<=q;i++){
scanf("%s",t);
int base=0;
for(int j=0;j<n;j++){
base=base*3;
if(t[j]=='1')
base++;
if(t[j]=='?')
base+=2;
}
printf("%d\n",dp[base]);
}
}
else{
rec(0,0,0,-1);
for(int i=1;i<=q;i++){
scanf("%s",t);
int base=0;
for(int j=n-BLOK;j<n;j++){
base=base*3;
if(t[j]=='1')
base++;
if(t[j]=='?')
base+=2;
}
f(0,n-BLOK,0,i);arr[i]=base;
}
int lek=(1<<BLOK);
for(int mask=0;mask<(1<<(n-BLOK));mask++){
tr(it,adj[0])
dp[it->ff]=s[lek*mask+two(it->ff)]-'0';
for(int i=1;i<=BLOK;i++)
tr(it,adj[i])
dp[it->ff]=dp[it->ff-pw[it->ss]]+dp[it->ff-2*pw[it->ss]];
for(int i=1;i<=q;i++)
if(ok[mask][i])
ans[i]+=dp[arr[i]];
}
for(int i=1;i<=q;i++)
printf("%d\n",ans[i]);
}
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:70:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&q);
^
snake_escaping.cpp:71:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s",s);
^
snake_escaping.cpp:83:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s",t);
^
snake_escaping.cpp:98:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s",t);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1144 KB |
Output is correct |
2 |
Correct |
3 ms |
1260 KB |
Output is correct |
3 |
Correct |
4 ms |
1368 KB |
Output is correct |
4 |
Correct |
3 ms |
1408 KB |
Output is correct |
5 |
Correct |
3 ms |
1420 KB |
Output is correct |
6 |
Correct |
3 ms |
1432 KB |
Output is correct |
7 |
Correct |
4 ms |
1444 KB |
Output is correct |
8 |
Correct |
3 ms |
1512 KB |
Output is correct |
9 |
Correct |
4 ms |
1524 KB |
Output is correct |
10 |
Correct |
4 ms |
1552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1144 KB |
Output is correct |
2 |
Correct |
3 ms |
1260 KB |
Output is correct |
3 |
Correct |
4 ms |
1368 KB |
Output is correct |
4 |
Correct |
3 ms |
1408 KB |
Output is correct |
5 |
Correct |
3 ms |
1420 KB |
Output is correct |
6 |
Correct |
3 ms |
1432 KB |
Output is correct |
7 |
Correct |
4 ms |
1444 KB |
Output is correct |
8 |
Correct |
3 ms |
1512 KB |
Output is correct |
9 |
Correct |
4 ms |
1524 KB |
Output is correct |
10 |
Correct |
4 ms |
1552 KB |
Output is correct |
11 |
Correct |
272 ms |
16156 KB |
Output is correct |
12 |
Correct |
243 ms |
26660 KB |
Output is correct |
13 |
Correct |
256 ms |
36652 KB |
Output is correct |
14 |
Correct |
247 ms |
47744 KB |
Output is correct |
15 |
Correct |
253 ms |
59292 KB |
Output is correct |
16 |
Correct |
269 ms |
65536 KB |
Output is correct |
17 |
Correct |
274 ms |
65536 KB |
Output is correct |
18 |
Correct |
224 ms |
65536 KB |
Output is correct |
19 |
Correct |
253 ms |
65536 KB |
Output is correct |
20 |
Correct |
277 ms |
65536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1144 KB |
Output is correct |
2 |
Correct |
3 ms |
1260 KB |
Output is correct |
3 |
Correct |
4 ms |
1368 KB |
Output is correct |
4 |
Correct |
3 ms |
1408 KB |
Output is correct |
5 |
Correct |
3 ms |
1420 KB |
Output is correct |
6 |
Correct |
3 ms |
1432 KB |
Output is correct |
7 |
Correct |
4 ms |
1444 KB |
Output is correct |
8 |
Correct |
3 ms |
1512 KB |
Output is correct |
9 |
Correct |
4 ms |
1524 KB |
Output is correct |
10 |
Correct |
4 ms |
1552 KB |
Output is correct |
11 |
Correct |
272 ms |
16156 KB |
Output is correct |
12 |
Correct |
243 ms |
26660 KB |
Output is correct |
13 |
Correct |
256 ms |
36652 KB |
Output is correct |
14 |
Correct |
247 ms |
47744 KB |
Output is correct |
15 |
Correct |
253 ms |
59292 KB |
Output is correct |
16 |
Correct |
269 ms |
65536 KB |
Output is correct |
17 |
Correct |
274 ms |
65536 KB |
Output is correct |
18 |
Correct |
224 ms |
65536 KB |
Output is correct |
19 |
Correct |
253 ms |
65536 KB |
Output is correct |
20 |
Correct |
277 ms |
65536 KB |
Output is correct |
21 |
Correct |
385 ms |
65536 KB |
Output is correct |
22 |
Correct |
307 ms |
65536 KB |
Output is correct |
23 |
Correct |
469 ms |
65536 KB |
Output is correct |
24 |
Correct |
410 ms |
65536 KB |
Output is correct |
25 |
Correct |
376 ms |
65536 KB |
Output is correct |
26 |
Correct |
477 ms |
65536 KB |
Output is correct |
27 |
Correct |
477 ms |
65536 KB |
Output is correct |
28 |
Correct |
341 ms |
65536 KB |
Output is correct |
29 |
Correct |
376 ms |
65536 KB |
Output is correct |
30 |
Correct |
395 ms |
65536 KB |
Output is correct |
31 |
Correct |
419 ms |
65536 KB |
Output is correct |
32 |
Correct |
406 ms |
65536 KB |
Output is correct |
33 |
Correct |
413 ms |
65536 KB |
Output is correct |
34 |
Correct |
461 ms |
65536 KB |
Output is correct |
35 |
Correct |
469 ms |
65536 KB |
Output is correct |
36 |
Correct |
287 ms |
65536 KB |
Output is correct |
37 |
Correct |
283 ms |
65536 KB |
Output is correct |
38 |
Correct |
384 ms |
65536 KB |
Output is correct |
39 |
Correct |
419 ms |
65536 KB |
Output is correct |
40 |
Correct |
431 ms |
65536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1144 KB |
Output is correct |
2 |
Correct |
3 ms |
1260 KB |
Output is correct |
3 |
Correct |
4 ms |
1368 KB |
Output is correct |
4 |
Correct |
3 ms |
1408 KB |
Output is correct |
5 |
Correct |
3 ms |
1420 KB |
Output is correct |
6 |
Correct |
3 ms |
1432 KB |
Output is correct |
7 |
Correct |
4 ms |
1444 KB |
Output is correct |
8 |
Correct |
3 ms |
1512 KB |
Output is correct |
9 |
Correct |
4 ms |
1524 KB |
Output is correct |
10 |
Correct |
4 ms |
1552 KB |
Output is correct |
11 |
Correct |
649 ms |
65536 KB |
Output is correct |
12 |
Correct |
699 ms |
65536 KB |
Output is correct |
13 |
Correct |
679 ms |
65536 KB |
Output is correct |
14 |
Correct |
684 ms |
65536 KB |
Output is correct |
15 |
Correct |
684 ms |
65536 KB |
Output is correct |
16 |
Correct |
724 ms |
65536 KB |
Output is correct |
17 |
Correct |
678 ms |
65536 KB |
Output is correct |
18 |
Correct |
684 ms |
65536 KB |
Output is correct |
19 |
Correct |
681 ms |
65536 KB |
Output is correct |
20 |
Correct |
720 ms |
65536 KB |
Output is correct |
21 |
Correct |
718 ms |
65536 KB |
Output is correct |
22 |
Correct |
703 ms |
65536 KB |
Output is correct |
23 |
Correct |
683 ms |
65536 KB |
Output is correct |
24 |
Correct |
708 ms |
65536 KB |
Output is correct |
25 |
Correct |
715 ms |
65536 KB |
Output is correct |
26 |
Correct |
674 ms |
65536 KB |
Output is correct |
27 |
Correct |
695 ms |
65536 KB |
Output is correct |
28 |
Correct |
700 ms |
65536 KB |
Output is correct |
29 |
Correct |
727 ms |
65536 KB |
Output is correct |
30 |
Correct |
761 ms |
65536 KB |
Output is correct |
31 |
Correct |
719 ms |
65536 KB |
Output is correct |
32 |
Correct |
730 ms |
65536 KB |
Output is correct |
33 |
Correct |
728 ms |
65536 KB |
Output is correct |
34 |
Correct |
717 ms |
65536 KB |
Output is correct |
35 |
Correct |
775 ms |
65536 KB |
Output is correct |
36 |
Correct |
716 ms |
65536 KB |
Output is correct |
37 |
Correct |
736 ms |
65536 KB |
Output is correct |
38 |
Correct |
709 ms |
65536 KB |
Output is correct |
39 |
Correct |
691 ms |
65536 KB |
Output is correct |
40 |
Correct |
700 ms |
65536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1144 KB |
Output is correct |
2 |
Correct |
3 ms |
1260 KB |
Output is correct |
3 |
Correct |
4 ms |
1368 KB |
Output is correct |
4 |
Correct |
3 ms |
1408 KB |
Output is correct |
5 |
Correct |
3 ms |
1420 KB |
Output is correct |
6 |
Correct |
3 ms |
1432 KB |
Output is correct |
7 |
Correct |
4 ms |
1444 KB |
Output is correct |
8 |
Correct |
3 ms |
1512 KB |
Output is correct |
9 |
Correct |
4 ms |
1524 KB |
Output is correct |
10 |
Correct |
4 ms |
1552 KB |
Output is correct |
11 |
Correct |
272 ms |
16156 KB |
Output is correct |
12 |
Correct |
243 ms |
26660 KB |
Output is correct |
13 |
Correct |
256 ms |
36652 KB |
Output is correct |
14 |
Correct |
247 ms |
47744 KB |
Output is correct |
15 |
Correct |
253 ms |
59292 KB |
Output is correct |
16 |
Correct |
269 ms |
65536 KB |
Output is correct |
17 |
Correct |
274 ms |
65536 KB |
Output is correct |
18 |
Correct |
224 ms |
65536 KB |
Output is correct |
19 |
Correct |
253 ms |
65536 KB |
Output is correct |
20 |
Correct |
277 ms |
65536 KB |
Output is correct |
21 |
Correct |
385 ms |
65536 KB |
Output is correct |
22 |
Correct |
307 ms |
65536 KB |
Output is correct |
23 |
Correct |
469 ms |
65536 KB |
Output is correct |
24 |
Correct |
410 ms |
65536 KB |
Output is correct |
25 |
Correct |
376 ms |
65536 KB |
Output is correct |
26 |
Correct |
477 ms |
65536 KB |
Output is correct |
27 |
Correct |
477 ms |
65536 KB |
Output is correct |
28 |
Correct |
341 ms |
65536 KB |
Output is correct |
29 |
Correct |
376 ms |
65536 KB |
Output is correct |
30 |
Correct |
395 ms |
65536 KB |
Output is correct |
31 |
Correct |
419 ms |
65536 KB |
Output is correct |
32 |
Correct |
406 ms |
65536 KB |
Output is correct |
33 |
Correct |
413 ms |
65536 KB |
Output is correct |
34 |
Correct |
461 ms |
65536 KB |
Output is correct |
35 |
Correct |
469 ms |
65536 KB |
Output is correct |
36 |
Correct |
287 ms |
65536 KB |
Output is correct |
37 |
Correct |
283 ms |
65536 KB |
Output is correct |
38 |
Correct |
384 ms |
65536 KB |
Output is correct |
39 |
Correct |
419 ms |
65536 KB |
Output is correct |
40 |
Correct |
431 ms |
65536 KB |
Output is correct |
41 |
Correct |
649 ms |
65536 KB |
Output is correct |
42 |
Correct |
699 ms |
65536 KB |
Output is correct |
43 |
Correct |
679 ms |
65536 KB |
Output is correct |
44 |
Correct |
684 ms |
65536 KB |
Output is correct |
45 |
Correct |
684 ms |
65536 KB |
Output is correct |
46 |
Correct |
724 ms |
65536 KB |
Output is correct |
47 |
Correct |
678 ms |
65536 KB |
Output is correct |
48 |
Correct |
684 ms |
65536 KB |
Output is correct |
49 |
Correct |
681 ms |
65536 KB |
Output is correct |
50 |
Correct |
720 ms |
65536 KB |
Output is correct |
51 |
Correct |
718 ms |
65536 KB |
Output is correct |
52 |
Correct |
703 ms |
65536 KB |
Output is correct |
53 |
Correct |
683 ms |
65536 KB |
Output is correct |
54 |
Correct |
708 ms |
65536 KB |
Output is correct |
55 |
Correct |
715 ms |
65536 KB |
Output is correct |
56 |
Correct |
674 ms |
65536 KB |
Output is correct |
57 |
Correct |
695 ms |
65536 KB |
Output is correct |
58 |
Correct |
700 ms |
65536 KB |
Output is correct |
59 |
Correct |
727 ms |
65536 KB |
Output is correct |
60 |
Correct |
761 ms |
65536 KB |
Output is correct |
61 |
Correct |
719 ms |
65536 KB |
Output is correct |
62 |
Correct |
730 ms |
65536 KB |
Output is correct |
63 |
Correct |
728 ms |
65536 KB |
Output is correct |
64 |
Correct |
717 ms |
65536 KB |
Output is correct |
65 |
Correct |
775 ms |
65536 KB |
Output is correct |
66 |
Correct |
716 ms |
65536 KB |
Output is correct |
67 |
Correct |
736 ms |
65536 KB |
Output is correct |
68 |
Correct |
709 ms |
65536 KB |
Output is correct |
69 |
Correct |
691 ms |
65536 KB |
Output is correct |
70 |
Correct |
700 ms |
65536 KB |
Output is correct |
71 |
Correct |
1842 ms |
65536 KB |
Output is correct |
72 |
Correct |
1803 ms |
65536 KB |
Output is correct |
73 |
Correct |
1874 ms |
65536 KB |
Output is correct |
74 |
Execution timed out |
2036 ms |
65536 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |