#include <bits/stdc++.h>
using namespace std;
//#define FILE_IO
int N, Q, ans;
int i, j, q, msk;
int cnt[3];
int val[1100005], dp[1100005], dp2[1100005];
int bts[1100005];
int B, b[25];
char s[1100005];
void bck0(int pas, int msk, int smn)
{
if(pas >= B)
{
ans += smn * dp[msk];
return;
}
bck0(pas + 1, msk, smn);
bck0(pas + 1, msk ^ (1 << b[pas]), -smn);
}
void bck1(int pas, int msk, int smn)
{
if(pas >= B)
{
ans += smn * dp2[msk];
return;
}
bck1(pas + 1, msk, smn);
bck1(pas + 1, msk ^ (1 << b[pas]), -smn);
}
void bckq(int pas, int msk)
{
if(pas >= B)
{
ans += val[msk];
return;
}
bckq(pas + 1, msk);
bckq(pas + 1, msk ^ (1 << b[pas]));
}
int main()
{
#ifdef FILE_IO
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
scanf("%d%d\n", &N, &Q);
scanf("%s\n", s);
for(i = 0; i < (1 << N); i++)
val[i] = dp[i] = dp2[i] = s[i] - '0';
for(j = 0; j < N; j++)
for(i = (1 << N) - 1; i >= 0; i--)
if( (i & (1 << j)) == 0 )
dp[i] += dp[i ^ (1 << j)];
for(j = 0; j < N; j++)
for(i = 0; i < (1 << N); i++)
if( (i & (1 << j)) != 0 )
dp2[i] += dp2[i ^ (1 << j)];
for(q = 1; q <= Q; q++)
{
scanf("%s\n", s);
reverse(s, s + N);
cnt[0] = cnt[1] = cnt[2] = 0;
for(j = 0; j < N; j++)
{
if(s[j] == '0') cnt[0]++;
else if(s[j] == '1') cnt[1]++;
else if(s[j] == '?') cnt[2]++;
}
if(cnt[0] < cnt[1] && cnt[0] < cnt[2])
{
msk = 0;
B = 0;
for(i = 0; i < N; i++)
{
if(s[i] == '1') msk |= (1 << i);
if(s[i] == '0') b[B++] = i;
}
ans = 0;
bck0(0, msk, 1);
printf("%d\n", ans);
}
else if(cnt[1] < cnt[2])
{
msk = 0;
B = 0;
for(i = 0; i < N; i++)
{
if(s[i] == '1')
{
msk |= (1 << i);
b[B++] = i;
}
if(s[i] == '?')
msk |= (1 << i);
}
ans = 0;
bck1(0, msk, 1);
printf("%d\n", ans);
}
else
{
msk = 0;
B = 0;
for(i = 0; i < N; i++)
{
if(s[i] == '?') b[B++] = i;
else msk |= ((s[i] - '0') << i);
}
ans = 0;
bckq(0, msk);
printf("%d\n", ans);
}
}
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:58:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d\n", &N, &Q);
~~~~~^~~~~~~~~~~~~~~~~~
snake_escaping.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s\n", s);
~~~~~^~~~~~~~~~~
snake_escaping.cpp:73:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s\n", s);
~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
472 KB |
Output is correct |
4 |
Correct |
3 ms |
500 KB |
Output is correct |
5 |
Correct |
2 ms |
700 KB |
Output is correct |
6 |
Correct |
2 ms |
716 KB |
Output is correct |
7 |
Correct |
2 ms |
716 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
2 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
472 KB |
Output is correct |
4 |
Correct |
3 ms |
500 KB |
Output is correct |
5 |
Correct |
2 ms |
700 KB |
Output is correct |
6 |
Correct |
2 ms |
716 KB |
Output is correct |
7 |
Correct |
2 ms |
716 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
2 ms |
716 KB |
Output is correct |
11 |
Correct |
359 ms |
6276 KB |
Output is correct |
12 |
Correct |
405 ms |
6316 KB |
Output is correct |
13 |
Correct |
422 ms |
7200 KB |
Output is correct |
14 |
Correct |
414 ms |
8616 KB |
Output is correct |
15 |
Correct |
452 ms |
9712 KB |
Output is correct |
16 |
Correct |
464 ms |
9712 KB |
Output is correct |
17 |
Correct |
490 ms |
10396 KB |
Output is correct |
18 |
Correct |
285 ms |
12504 KB |
Output is correct |
19 |
Correct |
313 ms |
12504 KB |
Output is correct |
20 |
Correct |
396 ms |
12644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
472 KB |
Output is correct |
4 |
Correct |
3 ms |
500 KB |
Output is correct |
5 |
Correct |
2 ms |
700 KB |
Output is correct |
6 |
Correct |
2 ms |
716 KB |
Output is correct |
7 |
Correct |
2 ms |
716 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
2 ms |
716 KB |
Output is correct |
11 |
Correct |
359 ms |
6276 KB |
Output is correct |
12 |
Correct |
405 ms |
6316 KB |
Output is correct |
13 |
Correct |
422 ms |
7200 KB |
Output is correct |
14 |
Correct |
414 ms |
8616 KB |
Output is correct |
15 |
Correct |
452 ms |
9712 KB |
Output is correct |
16 |
Correct |
464 ms |
9712 KB |
Output is correct |
17 |
Correct |
490 ms |
10396 KB |
Output is correct |
18 |
Correct |
285 ms |
12504 KB |
Output is correct |
19 |
Correct |
313 ms |
12504 KB |
Output is correct |
20 |
Correct |
396 ms |
12644 KB |
Output is correct |
21 |
Correct |
468 ms |
13032 KB |
Output is correct |
22 |
Correct |
486 ms |
13284 KB |
Output is correct |
23 |
Correct |
528 ms |
13620 KB |
Output is correct |
24 |
Correct |
565 ms |
13620 KB |
Output is correct |
25 |
Correct |
498 ms |
15588 KB |
Output is correct |
26 |
Correct |
595 ms |
17508 KB |
Output is correct |
27 |
Correct |
582 ms |
17732 KB |
Output is correct |
28 |
Correct |
304 ms |
20036 KB |
Output is correct |
29 |
Correct |
468 ms |
20036 KB |
Output is correct |
30 |
Correct |
487 ms |
21148 KB |
Output is correct |
31 |
Correct |
558 ms |
21148 KB |
Output is correct |
32 |
Correct |
604 ms |
22392 KB |
Output is correct |
33 |
Correct |
494 ms |
22732 KB |
Output is correct |
34 |
Correct |
555 ms |
22828 KB |
Output is correct |
35 |
Correct |
568 ms |
23340 KB |
Output is correct |
36 |
Correct |
309 ms |
23340 KB |
Output is correct |
37 |
Correct |
470 ms |
24524 KB |
Output is correct |
38 |
Correct |
396 ms |
24524 KB |
Output is correct |
39 |
Correct |
537 ms |
24524 KB |
Output is correct |
40 |
Correct |
534 ms |
24524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
472 KB |
Output is correct |
4 |
Correct |
3 ms |
500 KB |
Output is correct |
5 |
Correct |
2 ms |
700 KB |
Output is correct |
6 |
Correct |
2 ms |
716 KB |
Output is correct |
7 |
Correct |
2 ms |
716 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
2 ms |
716 KB |
Output is correct |
11 |
Correct |
111 ms |
34092 KB |
Output is correct |
12 |
Correct |
103 ms |
34092 KB |
Output is correct |
13 |
Correct |
169 ms |
34092 KB |
Output is correct |
14 |
Correct |
127 ms |
34092 KB |
Output is correct |
15 |
Correct |
113 ms |
34104 KB |
Output is correct |
16 |
Correct |
137 ms |
34104 KB |
Output is correct |
17 |
Correct |
130 ms |
34108 KB |
Output is correct |
18 |
Correct |
87 ms |
34208 KB |
Output is correct |
19 |
Correct |
95 ms |
34208 KB |
Output is correct |
20 |
Correct |
109 ms |
34208 KB |
Output is correct |
21 |
Correct |
112 ms |
34240 KB |
Output is correct |
22 |
Correct |
130 ms |
34240 KB |
Output is correct |
23 |
Correct |
127 ms |
34748 KB |
Output is correct |
24 |
Correct |
126 ms |
34748 KB |
Output is correct |
25 |
Correct |
127 ms |
34748 KB |
Output is correct |
26 |
Correct |
87 ms |
34748 KB |
Output is correct |
27 |
Correct |
102 ms |
34748 KB |
Output is correct |
28 |
Correct |
99 ms |
34748 KB |
Output is correct |
29 |
Correct |
116 ms |
34748 KB |
Output is correct |
30 |
Correct |
122 ms |
34748 KB |
Output is correct |
31 |
Correct |
100 ms |
34748 KB |
Output is correct |
32 |
Correct |
133 ms |
34748 KB |
Output is correct |
33 |
Correct |
154 ms |
34800 KB |
Output is correct |
34 |
Correct |
84 ms |
34800 KB |
Output is correct |
35 |
Correct |
129 ms |
34800 KB |
Output is correct |
36 |
Correct |
121 ms |
34800 KB |
Output is correct |
37 |
Correct |
115 ms |
34800 KB |
Output is correct |
38 |
Correct |
134 ms |
34852 KB |
Output is correct |
39 |
Correct |
144 ms |
34852 KB |
Output is correct |
40 |
Correct |
115 ms |
34852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
472 KB |
Output is correct |
4 |
Correct |
3 ms |
500 KB |
Output is correct |
5 |
Correct |
2 ms |
700 KB |
Output is correct |
6 |
Correct |
2 ms |
716 KB |
Output is correct |
7 |
Correct |
2 ms |
716 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
2 ms |
716 KB |
Output is correct |
11 |
Correct |
359 ms |
6276 KB |
Output is correct |
12 |
Correct |
405 ms |
6316 KB |
Output is correct |
13 |
Correct |
422 ms |
7200 KB |
Output is correct |
14 |
Correct |
414 ms |
8616 KB |
Output is correct |
15 |
Correct |
452 ms |
9712 KB |
Output is correct |
16 |
Correct |
464 ms |
9712 KB |
Output is correct |
17 |
Correct |
490 ms |
10396 KB |
Output is correct |
18 |
Correct |
285 ms |
12504 KB |
Output is correct |
19 |
Correct |
313 ms |
12504 KB |
Output is correct |
20 |
Correct |
396 ms |
12644 KB |
Output is correct |
21 |
Correct |
468 ms |
13032 KB |
Output is correct |
22 |
Correct |
486 ms |
13284 KB |
Output is correct |
23 |
Correct |
528 ms |
13620 KB |
Output is correct |
24 |
Correct |
565 ms |
13620 KB |
Output is correct |
25 |
Correct |
498 ms |
15588 KB |
Output is correct |
26 |
Correct |
595 ms |
17508 KB |
Output is correct |
27 |
Correct |
582 ms |
17732 KB |
Output is correct |
28 |
Correct |
304 ms |
20036 KB |
Output is correct |
29 |
Correct |
468 ms |
20036 KB |
Output is correct |
30 |
Correct |
487 ms |
21148 KB |
Output is correct |
31 |
Correct |
558 ms |
21148 KB |
Output is correct |
32 |
Correct |
604 ms |
22392 KB |
Output is correct |
33 |
Correct |
494 ms |
22732 KB |
Output is correct |
34 |
Correct |
555 ms |
22828 KB |
Output is correct |
35 |
Correct |
568 ms |
23340 KB |
Output is correct |
36 |
Correct |
309 ms |
23340 KB |
Output is correct |
37 |
Correct |
470 ms |
24524 KB |
Output is correct |
38 |
Correct |
396 ms |
24524 KB |
Output is correct |
39 |
Correct |
537 ms |
24524 KB |
Output is correct |
40 |
Correct |
534 ms |
24524 KB |
Output is correct |
41 |
Correct |
111 ms |
34092 KB |
Output is correct |
42 |
Correct |
103 ms |
34092 KB |
Output is correct |
43 |
Correct |
169 ms |
34092 KB |
Output is correct |
44 |
Correct |
127 ms |
34092 KB |
Output is correct |
45 |
Correct |
113 ms |
34104 KB |
Output is correct |
46 |
Correct |
137 ms |
34104 KB |
Output is correct |
47 |
Correct |
130 ms |
34108 KB |
Output is correct |
48 |
Correct |
87 ms |
34208 KB |
Output is correct |
49 |
Correct |
95 ms |
34208 KB |
Output is correct |
50 |
Correct |
109 ms |
34208 KB |
Output is correct |
51 |
Correct |
112 ms |
34240 KB |
Output is correct |
52 |
Correct |
130 ms |
34240 KB |
Output is correct |
53 |
Correct |
127 ms |
34748 KB |
Output is correct |
54 |
Correct |
126 ms |
34748 KB |
Output is correct |
55 |
Correct |
127 ms |
34748 KB |
Output is correct |
56 |
Correct |
87 ms |
34748 KB |
Output is correct |
57 |
Correct |
102 ms |
34748 KB |
Output is correct |
58 |
Correct |
99 ms |
34748 KB |
Output is correct |
59 |
Correct |
116 ms |
34748 KB |
Output is correct |
60 |
Correct |
122 ms |
34748 KB |
Output is correct |
61 |
Correct |
100 ms |
34748 KB |
Output is correct |
62 |
Correct |
133 ms |
34748 KB |
Output is correct |
63 |
Correct |
154 ms |
34800 KB |
Output is correct |
64 |
Correct |
84 ms |
34800 KB |
Output is correct |
65 |
Correct |
129 ms |
34800 KB |
Output is correct |
66 |
Correct |
121 ms |
34800 KB |
Output is correct |
67 |
Correct |
115 ms |
34800 KB |
Output is correct |
68 |
Correct |
134 ms |
34852 KB |
Output is correct |
69 |
Correct |
144 ms |
34852 KB |
Output is correct |
70 |
Correct |
115 ms |
34852 KB |
Output is correct |
71 |
Correct |
812 ms |
40196 KB |
Output is correct |
72 |
Correct |
776 ms |
40272 KB |
Output is correct |
73 |
Correct |
942 ms |
40272 KB |
Output is correct |
74 |
Correct |
1267 ms |
40272 KB |
Output is correct |
75 |
Correct |
832 ms |
42324 KB |
Output is correct |
76 |
Correct |
1515 ms |
42324 KB |
Output is correct |
77 |
Correct |
1371 ms |
42324 KB |
Output is correct |
78 |
Correct |
465 ms |
44980 KB |
Output is correct |
79 |
Correct |
645 ms |
44980 KB |
Output is correct |
80 |
Correct |
814 ms |
44980 KB |
Output is correct |
81 |
Correct |
1084 ms |
44980 KB |
Output is correct |
82 |
Correct |
1378 ms |
44980 KB |
Output is correct |
83 |
Correct |
740 ms |
44980 KB |
Output is correct |
84 |
Correct |
1426 ms |
44980 KB |
Output is correct |
85 |
Correct |
1350 ms |
44980 KB |
Output is correct |
86 |
Correct |
460 ms |
44980 KB |
Output is correct |
87 |
Correct |
765 ms |
44980 KB |
Output is correct |
88 |
Correct |
662 ms |
44980 KB |
Output is correct |
89 |
Correct |
916 ms |
44980 KB |
Output is correct |
90 |
Correct |
1176 ms |
44980 KB |
Output is correct |
91 |
Correct |
744 ms |
44980 KB |
Output is correct |
92 |
Correct |
1584 ms |
44980 KB |
Output is correct |
93 |
Correct |
1359 ms |
44980 KB |
Output is correct |
94 |
Correct |
446 ms |
44980 KB |
Output is correct |
95 |
Correct |
1093 ms |
44980 KB |
Output is correct |
96 |
Correct |
1116 ms |
44980 KB |
Output is correct |
97 |
Correct |
1100 ms |
44980 KB |
Output is correct |
98 |
Correct |
1108 ms |
45064 KB |
Output is correct |
99 |
Runtime error |
1087 ms |
66368 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
100 |
Halted |
0 ms |
0 KB |
- |