#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define F first
#define S second
#define debug(x) cerr<<#x<<" :"<<x<<"\n"
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define FAST ios_base::sync_with_stdio(false), cin.tie(), cout.tie();
//#define int long long
typedef long long ll;
typedef long double ld;
const int maxn = 20;
const int mod = 1e9 + 7;
const int INF = 1e9 + 7;
const int mlog = 20;
const int SQ = 400;
int sos[1 << 20], rsos[1 << 20];
string s, k;
int l, q;
int a, b, c;
int solve_a()
{
int ans = 0, base = 0, mask = 0;
for(int i = 0;i < l; i++)
{
if (k[i] == '?') mask += 1 << i;
else if (k[i] == '1') base += 1 << i;
}
for (int i = mask; i; i = mask & (i - 1)) ans += s[i | base] - '0';
ans += s[base] - '0';
return ans;
}
int solve_b()
{
int ans = 0, base = 0, mask = 0;
for(int i = 0;i < l; i++)
{
if (k[i] == '0') mask += 1 << i;
else if (k[i] == '?') base += 1 << i;
}
for (int i = mask; i; i = mask & (i - 1)) ans += ((b - __builtin_popcount(i)) & 1 ? -1 : 1) * rsos[i | base];
ans += (b & 1 ? -1 : 1) * rsos[base];
return ans;
}
int solve_c()
{
int ans = 0, base = 0, mask = 0;
for(int i = 0;i < l; i++)
{
if (k[i] == '1') mask += 1 << i;
else if (k[i] == '?') base += 1 << i;
}
for (int i = mask; i; i = mask & (i - 1)) ans += ((c - __builtin_popcount(i)) & 1 ? -1 : 1) * sos[i | base];
ans += (c & 1 ? -1 : 1) * sos[base];
return ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> l >> q >> s;
for(int i = 0; i < (1 << l); i++)
sos[i] = s[i] - '0', rsos[i] = s[(1 << l) - i - 1] - '0';
for(int i = 0; i < l; i++)
for(int mask = 0; mask < (1 << l); mask++)
if(mask & (1 << i))
sos[mask] += sos[mask ^ (1 << i)], rsos[mask] += rsos[mask ^ (1 << i)];
while(q--)
{
cin >> k;
reverse(all(k));
a = b = c = 0;
int ans = 0, base = 0, mask = 0;
for(int i = 0; i < l; i++)
{
if(k[i] == '?') a++;
if(k[i] == '0') b++;
if(k[i] == '1') c++;
}
if(a < 7) cout << solve_a() << "\n";
else if(b < 7) cout << solve_b() << "\n";
else cout << solve_c() << "\n";
}
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:95:13: warning: unused variable 'ans' [-Wunused-variable]
95 | int ans = 0, base = 0, mask = 0;
| ^~~
snake_escaping.cpp:95:22: warning: unused variable 'base' [-Wunused-variable]
95 | int ans = 0, base = 0, mask = 0;
| ^~~~
snake_escaping.cpp:95:32: warning: unused variable 'mask' [-Wunused-variable]
95 | int ans = 0, base = 0, mask = 0;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
348 ms |
4580 KB |
Output is correct |
12 |
Correct |
335 ms |
4160 KB |
Output is correct |
13 |
Correct |
325 ms |
3392 KB |
Output is correct |
14 |
Correct |
315 ms |
3568 KB |
Output is correct |
15 |
Correct |
411 ms |
4484 KB |
Output is correct |
16 |
Correct |
356 ms |
3692 KB |
Output is correct |
17 |
Correct |
368 ms |
3684 KB |
Output is correct |
18 |
Correct |
230 ms |
5548 KB |
Output is correct |
19 |
Correct |
254 ms |
2560 KB |
Output is correct |
20 |
Correct |
336 ms |
4292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
348 ms |
4580 KB |
Output is correct |
12 |
Correct |
335 ms |
4160 KB |
Output is correct |
13 |
Correct |
325 ms |
3392 KB |
Output is correct |
14 |
Correct |
315 ms |
3568 KB |
Output is correct |
15 |
Correct |
411 ms |
4484 KB |
Output is correct |
16 |
Correct |
356 ms |
3692 KB |
Output is correct |
17 |
Correct |
368 ms |
3684 KB |
Output is correct |
18 |
Correct |
230 ms |
5548 KB |
Output is correct |
19 |
Correct |
254 ms |
2560 KB |
Output is correct |
20 |
Correct |
336 ms |
4292 KB |
Output is correct |
21 |
Correct |
302 ms |
4600 KB |
Output is correct |
22 |
Correct |
473 ms |
4716 KB |
Output is correct |
23 |
Correct |
410 ms |
10756 KB |
Output is correct |
24 |
Correct |
409 ms |
10476 KB |
Output is correct |
25 |
Correct |
391 ms |
12488 KB |
Output is correct |
26 |
Correct |
432 ms |
10952 KB |
Output is correct |
27 |
Correct |
429 ms |
10948 KB |
Output is correct |
28 |
Correct |
265 ms |
13504 KB |
Output is correct |
29 |
Correct |
294 ms |
9428 KB |
Output is correct |
30 |
Correct |
370 ms |
11636 KB |
Output is correct |
31 |
Correct |
473 ms |
11560 KB |
Output is correct |
32 |
Correct |
428 ms |
11484 KB |
Output is correct |
33 |
Correct |
347 ms |
10392 KB |
Output is correct |
34 |
Correct |
405 ms |
10524 KB |
Output is correct |
35 |
Correct |
425 ms |
11064 KB |
Output is correct |
36 |
Correct |
249 ms |
9444 KB |
Output is correct |
37 |
Correct |
426 ms |
11556 KB |
Output is correct |
38 |
Correct |
304 ms |
9536 KB |
Output is correct |
39 |
Correct |
391 ms |
10644 KB |
Output is correct |
40 |
Correct |
437 ms |
10564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
54 ms |
9924 KB |
Output is correct |
12 |
Correct |
59 ms |
9804 KB |
Output is correct |
13 |
Correct |
61 ms |
9796 KB |
Output is correct |
14 |
Correct |
87 ms |
9952 KB |
Output is correct |
15 |
Correct |
61 ms |
9940 KB |
Output is correct |
16 |
Correct |
83 ms |
9804 KB |
Output is correct |
17 |
Correct |
78 ms |
9856 KB |
Output is correct |
18 |
Correct |
51 ms |
10024 KB |
Output is correct |
19 |
Correct |
53 ms |
9684 KB |
Output is correct |
20 |
Correct |
54 ms |
9812 KB |
Output is correct |
21 |
Correct |
72 ms |
9796 KB |
Output is correct |
22 |
Correct |
112 ms |
9788 KB |
Output is correct |
23 |
Correct |
66 ms |
9828 KB |
Output is correct |
24 |
Correct |
64 ms |
9808 KB |
Output is correct |
25 |
Correct |
75 ms |
9808 KB |
Output is correct |
26 |
Correct |
46 ms |
9704 KB |
Output is correct |
27 |
Correct |
54 ms |
9828 KB |
Output is correct |
28 |
Correct |
55 ms |
9700 KB |
Output is correct |
29 |
Correct |
60 ms |
9808 KB |
Output is correct |
30 |
Correct |
63 ms |
9828 KB |
Output is correct |
31 |
Correct |
54 ms |
9700 KB |
Output is correct |
32 |
Correct |
88 ms |
9804 KB |
Output is correct |
33 |
Correct |
78 ms |
9832 KB |
Output is correct |
34 |
Correct |
47 ms |
9684 KB |
Output is correct |
35 |
Correct |
69 ms |
9932 KB |
Output is correct |
36 |
Correct |
70 ms |
9796 KB |
Output is correct |
37 |
Correct |
68 ms |
9828 KB |
Output is correct |
38 |
Correct |
74 ms |
9792 KB |
Output is correct |
39 |
Correct |
73 ms |
9804 KB |
Output is correct |
40 |
Correct |
69 ms |
9808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
348 ms |
4580 KB |
Output is correct |
12 |
Correct |
335 ms |
4160 KB |
Output is correct |
13 |
Correct |
325 ms |
3392 KB |
Output is correct |
14 |
Correct |
315 ms |
3568 KB |
Output is correct |
15 |
Correct |
411 ms |
4484 KB |
Output is correct |
16 |
Correct |
356 ms |
3692 KB |
Output is correct |
17 |
Correct |
368 ms |
3684 KB |
Output is correct |
18 |
Correct |
230 ms |
5548 KB |
Output is correct |
19 |
Correct |
254 ms |
2560 KB |
Output is correct |
20 |
Correct |
336 ms |
4292 KB |
Output is correct |
21 |
Correct |
302 ms |
4600 KB |
Output is correct |
22 |
Correct |
473 ms |
4716 KB |
Output is correct |
23 |
Correct |
410 ms |
10756 KB |
Output is correct |
24 |
Correct |
409 ms |
10476 KB |
Output is correct |
25 |
Correct |
391 ms |
12488 KB |
Output is correct |
26 |
Correct |
432 ms |
10952 KB |
Output is correct |
27 |
Correct |
429 ms |
10948 KB |
Output is correct |
28 |
Correct |
265 ms |
13504 KB |
Output is correct |
29 |
Correct |
294 ms |
9428 KB |
Output is correct |
30 |
Correct |
370 ms |
11636 KB |
Output is correct |
31 |
Correct |
473 ms |
11560 KB |
Output is correct |
32 |
Correct |
428 ms |
11484 KB |
Output is correct |
33 |
Correct |
347 ms |
10392 KB |
Output is correct |
34 |
Correct |
405 ms |
10524 KB |
Output is correct |
35 |
Correct |
425 ms |
11064 KB |
Output is correct |
36 |
Correct |
249 ms |
9444 KB |
Output is correct |
37 |
Correct |
426 ms |
11556 KB |
Output is correct |
38 |
Correct |
304 ms |
9536 KB |
Output is correct |
39 |
Correct |
391 ms |
10644 KB |
Output is correct |
40 |
Correct |
437 ms |
10564 KB |
Output is correct |
41 |
Correct |
54 ms |
9924 KB |
Output is correct |
42 |
Correct |
59 ms |
9804 KB |
Output is correct |
43 |
Correct |
61 ms |
9796 KB |
Output is correct |
44 |
Correct |
87 ms |
9952 KB |
Output is correct |
45 |
Correct |
61 ms |
9940 KB |
Output is correct |
46 |
Correct |
83 ms |
9804 KB |
Output is correct |
47 |
Correct |
78 ms |
9856 KB |
Output is correct |
48 |
Correct |
51 ms |
10024 KB |
Output is correct |
49 |
Correct |
53 ms |
9684 KB |
Output is correct |
50 |
Correct |
54 ms |
9812 KB |
Output is correct |
51 |
Correct |
72 ms |
9796 KB |
Output is correct |
52 |
Correct |
112 ms |
9788 KB |
Output is correct |
53 |
Correct |
66 ms |
9828 KB |
Output is correct |
54 |
Correct |
64 ms |
9808 KB |
Output is correct |
55 |
Correct |
75 ms |
9808 KB |
Output is correct |
56 |
Correct |
46 ms |
9704 KB |
Output is correct |
57 |
Correct |
54 ms |
9828 KB |
Output is correct |
58 |
Correct |
55 ms |
9700 KB |
Output is correct |
59 |
Correct |
60 ms |
9808 KB |
Output is correct |
60 |
Correct |
63 ms |
9828 KB |
Output is correct |
61 |
Correct |
54 ms |
9700 KB |
Output is correct |
62 |
Correct |
88 ms |
9804 KB |
Output is correct |
63 |
Correct |
78 ms |
9832 KB |
Output is correct |
64 |
Correct |
47 ms |
9684 KB |
Output is correct |
65 |
Correct |
69 ms |
9932 KB |
Output is correct |
66 |
Correct |
70 ms |
9796 KB |
Output is correct |
67 |
Correct |
68 ms |
9828 KB |
Output is correct |
68 |
Correct |
74 ms |
9792 KB |
Output is correct |
69 |
Correct |
73 ms |
9804 KB |
Output is correct |
70 |
Correct |
69 ms |
9808 KB |
Output is correct |
71 |
Correct |
439 ms |
16492 KB |
Output is correct |
72 |
Correct |
517 ms |
16572 KB |
Output is correct |
73 |
Correct |
557 ms |
15204 KB |
Output is correct |
74 |
Correct |
1190 ms |
15488 KB |
Output is correct |
75 |
Correct |
619 ms |
17560 KB |
Output is correct |
76 |
Correct |
1074 ms |
15896 KB |
Output is correct |
77 |
Correct |
877 ms |
15664 KB |
Output is correct |
78 |
Correct |
333 ms |
19524 KB |
Output is correct |
79 |
Correct |
403 ms |
13500 KB |
Output is correct |
80 |
Correct |
492 ms |
16612 KB |
Output is correct |
81 |
Correct |
804 ms |
16612 KB |
Output is correct |
82 |
Correct |
1038 ms |
15408 KB |
Output is correct |
83 |
Correct |
474 ms |
14568 KB |
Output is correct |
84 |
Correct |
643 ms |
15472 KB |
Output is correct |
85 |
Correct |
902 ms |
15628 KB |
Output is correct |
86 |
Correct |
325 ms |
13428 KB |
Output is correct |
87 |
Correct |
456 ms |
16436 KB |
Output is correct |
88 |
Correct |
428 ms |
13332 KB |
Output is correct |
89 |
Correct |
556 ms |
15036 KB |
Output is correct |
90 |
Correct |
634 ms |
15332 KB |
Output is correct |
91 |
Correct |
462 ms |
14564 KB |
Output is correct |
92 |
Correct |
1158 ms |
15776 KB |
Output is correct |
93 |
Correct |
919 ms |
15596 KB |
Output is correct |
94 |
Correct |
347 ms |
13348 KB |
Output is correct |
95 |
Correct |
751 ms |
15456 KB |
Output is correct |
96 |
Correct |
752 ms |
15364 KB |
Output is correct |
97 |
Correct |
777 ms |
15388 KB |
Output is correct |
98 |
Correct |
777 ms |
15332 KB |
Output is correct |
99 |
Correct |
761 ms |
15272 KB |
Output is correct |
100 |
Correct |
751 ms |
14292 KB |
Output is correct |