#include <bits/stdc++.h>
using namespace std;
int n,q,C[(1<<21)],M[20],dp[2594323],ANS[1000006],Y[1000006],vs;
string S[1000006];
main () {
ios::sync_with_stdio(false);
cin>>n>>q;
M[0]=1;
for (int i=1; i<=13; i++)
M[i]=M[i-1]*3;
if (n<=13) {
for (int i=0; i<(1<<n); i++) {
char c;
cin>>c;
C[i]=c-'0';
int x=0;
for (int j=0; j<n; j++)
if (i&(1<<j)) x+=M[j];
dp[x]+=C[i];
}
for (int i=0; i<M[n]; i++) {
int x=i;
for (int j=n-1; j>=0; j--)
if (x-2*M[j]>=0) x-=2*M[j],dp[i]=(dp[i-2*M[j]]+dp[i-M[j]]);
else
if (x-M[j]>=0) x-=M[j];
}
for (int i=1; i<=q; i++) {
string s;
int x=0;
cin>>s;
for (int j=0; j<s.size(); j++)
if (s[s.size()-j-1]=='?') x+=2*M[j];
else
if (s[s.size()-j-1]=='1') x+=M[j];
printf("%d\n",dp[x]);
}
}
else {
for (int i=0; i<(1<<n); i++) {
char c;
cin>>c;
C[i]=c-'0';
}
int K=(n-13);
for (int i=1; i<=q; i++) {
cin>>S[i];
for (int k=S[i].size()-1; k>=K; k--) {
if (S[i][k]=='?') Y[i]+=2*M[k-S[i].size()+1];
else
if (S[i][k]=='1') Y[i]+=M[k-S[i].size()+1];
}
/* for (int k=0; k<K; k++) {
}*/
}
for (int i=0; i<(1<<K); i++) {
for (int j=0; j<M[13]; j++)
dp[j]=0;
for (int j=0; j<(1<<13); j++) {
int x=0;
for (int k=0; k<13; k++)
if (i&(1<<k)) x+=M[k];
dp[x]=C[i*(1<<13)+j];
}
for (int j=0; j<M[13]; j++) {
int x=j;
for (int k=12; k>=0; k--)
if (x-2*M[k]>=0) x-=2*M[k],dp[j]=(dp[j-2*M[k]]+dp[j-M[k]]);
else
if (x-M[k]>=0) x-=M[k];
}
for (int j=1; j<=q; j++) {
int ko=0;
for (int k=0; k<K; k++) {
if (
((S[j][k]-'0')==1 && (1<<k)&i) ||
((S[j][k]-'0')==0 && !((1<<k)&i)) ||
S[j][k]=='?'
) continue;
ko=1;
}
if (ko) continue;
ANS[j]+=dp[Y[i]];
}
}
for (int i=1; i<=q; i++)
printf("%d\n",ANS[i]);
}
}
Compilation message
snake_escaping.cpp:5:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main () {
^
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:39:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j=0; j<s.size(); j++)
~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
31864 KB |
Output is correct |
2 |
Correct |
32 ms |
31992 KB |
Output is correct |
3 |
Correct |
30 ms |
31992 KB |
Output is correct |
4 |
Correct |
30 ms |
31736 KB |
Output is correct |
5 |
Correct |
30 ms |
31864 KB |
Output is correct |
6 |
Correct |
31 ms |
31992 KB |
Output is correct |
7 |
Correct |
31 ms |
31860 KB |
Output is correct |
8 |
Correct |
30 ms |
31992 KB |
Output is correct |
9 |
Correct |
30 ms |
31864 KB |
Output is correct |
10 |
Correct |
31 ms |
31864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
31864 KB |
Output is correct |
2 |
Correct |
32 ms |
31992 KB |
Output is correct |
3 |
Correct |
30 ms |
31992 KB |
Output is correct |
4 |
Correct |
30 ms |
31736 KB |
Output is correct |
5 |
Correct |
30 ms |
31864 KB |
Output is correct |
6 |
Correct |
31 ms |
31992 KB |
Output is correct |
7 |
Correct |
31 ms |
31860 KB |
Output is correct |
8 |
Correct |
30 ms |
31992 KB |
Output is correct |
9 |
Correct |
30 ms |
31864 KB |
Output is correct |
10 |
Correct |
31 ms |
31864 KB |
Output is correct |
11 |
Correct |
277 ms |
36108 KB |
Output is correct |
12 |
Correct |
298 ms |
35724 KB |
Output is correct |
13 |
Correct |
305 ms |
34808 KB |
Output is correct |
14 |
Correct |
306 ms |
34968 KB |
Output is correct |
15 |
Correct |
306 ms |
35952 KB |
Output is correct |
16 |
Correct |
319 ms |
35184 KB |
Output is correct |
17 |
Correct |
320 ms |
35172 KB |
Output is correct |
18 |
Correct |
232 ms |
36928 KB |
Output is correct |
19 |
Correct |
276 ms |
34040 KB |
Output is correct |
20 |
Correct |
307 ms |
35832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
31864 KB |
Output is correct |
2 |
Correct |
32 ms |
31992 KB |
Output is correct |
3 |
Correct |
30 ms |
31992 KB |
Output is correct |
4 |
Correct |
30 ms |
31736 KB |
Output is correct |
5 |
Correct |
30 ms |
31864 KB |
Output is correct |
6 |
Correct |
31 ms |
31992 KB |
Output is correct |
7 |
Correct |
31 ms |
31860 KB |
Output is correct |
8 |
Correct |
30 ms |
31992 KB |
Output is correct |
9 |
Correct |
30 ms |
31864 KB |
Output is correct |
10 |
Correct |
31 ms |
31864 KB |
Output is correct |
11 |
Correct |
277 ms |
36108 KB |
Output is correct |
12 |
Correct |
298 ms |
35724 KB |
Output is correct |
13 |
Correct |
305 ms |
34808 KB |
Output is correct |
14 |
Correct |
306 ms |
34968 KB |
Output is correct |
15 |
Correct |
306 ms |
35952 KB |
Output is correct |
16 |
Correct |
319 ms |
35184 KB |
Output is correct |
17 |
Correct |
320 ms |
35172 KB |
Output is correct |
18 |
Correct |
232 ms |
36928 KB |
Output is correct |
19 |
Correct |
276 ms |
34040 KB |
Output is correct |
20 |
Correct |
307 ms |
35832 KB |
Output is correct |
21 |
Correct |
404 ms |
42088 KB |
Output is correct |
22 |
Correct |
382 ms |
42104 KB |
Output is correct |
23 |
Correct |
457 ms |
41208 KB |
Output is correct |
24 |
Correct |
433 ms |
40952 KB |
Output is correct |
25 |
Correct |
450 ms |
43108 KB |
Output is correct |
26 |
Correct |
456 ms |
41632 KB |
Output is correct |
27 |
Correct |
487 ms |
41624 KB |
Output is correct |
28 |
Correct |
455 ms |
44032 KB |
Output is correct |
29 |
Correct |
359 ms |
40084 KB |
Output is correct |
30 |
Correct |
428 ms |
42304 KB |
Output is correct |
31 |
Correct |
445 ms |
41980 KB |
Output is correct |
32 |
Correct |
455 ms |
41996 KB |
Output is correct |
33 |
Correct |
378 ms |
40820 KB |
Output is correct |
34 |
Correct |
455 ms |
40952 KB |
Output is correct |
35 |
Correct |
478 ms |
41444 KB |
Output is correct |
36 |
Correct |
284 ms |
40020 KB |
Output is correct |
37 |
Correct |
366 ms |
41996 KB |
Output is correct |
38 |
Correct |
365 ms |
39932 KB |
Output is correct |
39 |
Correct |
444 ms |
41308 KB |
Output is correct |
40 |
Correct |
437 ms |
40952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
31864 KB |
Output is correct |
2 |
Correct |
32 ms |
31992 KB |
Output is correct |
3 |
Correct |
30 ms |
31992 KB |
Output is correct |
4 |
Correct |
30 ms |
31736 KB |
Output is correct |
5 |
Correct |
30 ms |
31864 KB |
Output is correct |
6 |
Correct |
31 ms |
31992 KB |
Output is correct |
7 |
Correct |
31 ms |
31860 KB |
Output is correct |
8 |
Correct |
30 ms |
31992 KB |
Output is correct |
9 |
Correct |
30 ms |
31864 KB |
Output is correct |
10 |
Correct |
31 ms |
31864 KB |
Output is correct |
11 |
Execution timed out |
2056 ms |
44772 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
31864 KB |
Output is correct |
2 |
Correct |
32 ms |
31992 KB |
Output is correct |
3 |
Correct |
30 ms |
31992 KB |
Output is correct |
4 |
Correct |
30 ms |
31736 KB |
Output is correct |
5 |
Correct |
30 ms |
31864 KB |
Output is correct |
6 |
Correct |
31 ms |
31992 KB |
Output is correct |
7 |
Correct |
31 ms |
31860 KB |
Output is correct |
8 |
Correct |
30 ms |
31992 KB |
Output is correct |
9 |
Correct |
30 ms |
31864 KB |
Output is correct |
10 |
Correct |
31 ms |
31864 KB |
Output is correct |
11 |
Correct |
277 ms |
36108 KB |
Output is correct |
12 |
Correct |
298 ms |
35724 KB |
Output is correct |
13 |
Correct |
305 ms |
34808 KB |
Output is correct |
14 |
Correct |
306 ms |
34968 KB |
Output is correct |
15 |
Correct |
306 ms |
35952 KB |
Output is correct |
16 |
Correct |
319 ms |
35184 KB |
Output is correct |
17 |
Correct |
320 ms |
35172 KB |
Output is correct |
18 |
Correct |
232 ms |
36928 KB |
Output is correct |
19 |
Correct |
276 ms |
34040 KB |
Output is correct |
20 |
Correct |
307 ms |
35832 KB |
Output is correct |
21 |
Correct |
404 ms |
42088 KB |
Output is correct |
22 |
Correct |
382 ms |
42104 KB |
Output is correct |
23 |
Correct |
457 ms |
41208 KB |
Output is correct |
24 |
Correct |
433 ms |
40952 KB |
Output is correct |
25 |
Correct |
450 ms |
43108 KB |
Output is correct |
26 |
Correct |
456 ms |
41632 KB |
Output is correct |
27 |
Correct |
487 ms |
41624 KB |
Output is correct |
28 |
Correct |
455 ms |
44032 KB |
Output is correct |
29 |
Correct |
359 ms |
40084 KB |
Output is correct |
30 |
Correct |
428 ms |
42304 KB |
Output is correct |
31 |
Correct |
445 ms |
41980 KB |
Output is correct |
32 |
Correct |
455 ms |
41996 KB |
Output is correct |
33 |
Correct |
378 ms |
40820 KB |
Output is correct |
34 |
Correct |
455 ms |
40952 KB |
Output is correct |
35 |
Correct |
478 ms |
41444 KB |
Output is correct |
36 |
Correct |
284 ms |
40020 KB |
Output is correct |
37 |
Correct |
366 ms |
41996 KB |
Output is correct |
38 |
Correct |
365 ms |
39932 KB |
Output is correct |
39 |
Correct |
444 ms |
41308 KB |
Output is correct |
40 |
Correct |
437 ms |
40952 KB |
Output is correct |
41 |
Execution timed out |
2056 ms |
44772 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |