#include <bits/stdc++.h>
//#include <bits/debug.hpp>
#define ll long long
#define all(x) x.begin(), x.end()
#define Neco "Snake Escaping"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 2e6 + 7
using namespace std;
const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
int N, q;
int a[maxn], f[2][maxn];
int CASE(int type, int P, int L) {
int ans = 0;
for(int x = P; ; x = (x - 1) & P)
{
int w = (cntbit(P) - cntbit(x)) & 1 ? -1 : 1;
ans += w * f[type][x | L];
if(x == 0) break;
}
return ans;
}
void solve()
{
cin >> N >> q;
fi(i, 0, (1 << N) - 1) {
char x; cin >> x;
a[i] = x - '0';
}
fi(i, 0, (1 << N) - 1) {
f[0][(1 << N) - 1 - i] = a[i];
f[1][i] = a[i];
}
fi(j, 0, N - 1) fi(x, 0, (1 << N) - 1) {
if(getbit(x, j))
{
f[0][x] += f[0][x ^ (1 << j)];
f[1][x] += f[1][x ^ (1 << j)];
}
}
fi(_, 1, q)
{
string s; cin >> s;
int A = 0, B = 0, C = 0;
int ans = 0;
fi(i, 0, N - 1) {
if(s[N - 1 - i] == '0') A += (1 << i);
else if(s[N - 1 - i] == '1') B += (1 << i);
else C += (1 << i);
}
int cA = cntbit(A), cB = cntbit(B), cC = cntbit(C);
if(cA <= min(cB, cC)) ans = CASE(0, A, C);
else if(cB <= min(cA, cC)) ans = CASE(1, B, C);
else
{
for(int x = C; ; x = (x - 1) & C) {
ans += a[x | B];
if(x == 0) break;
}
}
cout << ans << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest--)
{
solve();
}
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | freopen(Neco".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4460 KB |
Output is correct |
11 |
Correct |
147 ms |
19160 KB |
Output is correct |
12 |
Correct |
163 ms |
18768 KB |
Output is correct |
13 |
Correct |
169 ms |
18256 KB |
Output is correct |
14 |
Correct |
180 ms |
18424 KB |
Output is correct |
15 |
Correct |
159 ms |
19280 KB |
Output is correct |
16 |
Correct |
233 ms |
18300 KB |
Output is correct |
17 |
Correct |
195 ms |
18260 KB |
Output is correct |
18 |
Correct |
113 ms |
20184 KB |
Output is correct |
19 |
Correct |
146 ms |
17236 KB |
Output is correct |
20 |
Correct |
167 ms |
18768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4460 KB |
Output is correct |
11 |
Correct |
147 ms |
19160 KB |
Output is correct |
12 |
Correct |
163 ms |
18768 KB |
Output is correct |
13 |
Correct |
169 ms |
18256 KB |
Output is correct |
14 |
Correct |
180 ms |
18424 KB |
Output is correct |
15 |
Correct |
159 ms |
19280 KB |
Output is correct |
16 |
Correct |
233 ms |
18300 KB |
Output is correct |
17 |
Correct |
195 ms |
18260 KB |
Output is correct |
18 |
Correct |
113 ms |
20184 KB |
Output is correct |
19 |
Correct |
146 ms |
17236 KB |
Output is correct |
20 |
Correct |
167 ms |
18768 KB |
Output is correct |
21 |
Correct |
163 ms |
22144 KB |
Output is correct |
22 |
Correct |
177 ms |
22356 KB |
Output is correct |
23 |
Correct |
211 ms |
21332 KB |
Output is correct |
24 |
Correct |
243 ms |
21184 KB |
Output is correct |
25 |
Correct |
187 ms |
23124 KB |
Output is correct |
26 |
Correct |
270 ms |
21588 KB |
Output is correct |
27 |
Correct |
225 ms |
21588 KB |
Output is correct |
28 |
Correct |
123 ms |
24144 KB |
Output is correct |
29 |
Correct |
160 ms |
20308 KB |
Output is correct |
30 |
Correct |
210 ms |
22232 KB |
Output is correct |
31 |
Correct |
208 ms |
22100 KB |
Output is correct |
32 |
Correct |
237 ms |
22240 KB |
Output is correct |
33 |
Correct |
184 ms |
21104 KB |
Output is correct |
34 |
Correct |
235 ms |
21272 KB |
Output is correct |
35 |
Correct |
221 ms |
21684 KB |
Output is correct |
36 |
Correct |
120 ms |
20068 KB |
Output is correct |
37 |
Correct |
160 ms |
22224 KB |
Output is correct |
38 |
Correct |
172 ms |
20164 KB |
Output is correct |
39 |
Correct |
209 ms |
21328 KB |
Output is correct |
40 |
Correct |
234 ms |
21072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4460 KB |
Output is correct |
11 |
Correct |
40 ms |
19340 KB |
Output is correct |
12 |
Correct |
38 ms |
19340 KB |
Output is correct |
13 |
Correct |
48 ms |
19024 KB |
Output is correct |
14 |
Correct |
56 ms |
19280 KB |
Output is correct |
15 |
Correct |
50 ms |
19272 KB |
Output is correct |
16 |
Correct |
52 ms |
19288 KB |
Output is correct |
17 |
Correct |
49 ms |
19284 KB |
Output is correct |
18 |
Correct |
37 ms |
19336 KB |
Output is correct |
19 |
Correct |
38 ms |
19084 KB |
Output is correct |
20 |
Correct |
41 ms |
19136 KB |
Output is correct |
21 |
Correct |
45 ms |
19340 KB |
Output is correct |
22 |
Correct |
61 ms |
19288 KB |
Output is correct |
23 |
Correct |
40 ms |
19024 KB |
Output is correct |
24 |
Correct |
49 ms |
19284 KB |
Output is correct |
25 |
Correct |
48 ms |
19232 KB |
Output is correct |
26 |
Correct |
36 ms |
19028 KB |
Output is correct |
27 |
Correct |
40 ms |
19280 KB |
Output is correct |
28 |
Correct |
47 ms |
19024 KB |
Output is correct |
29 |
Correct |
43 ms |
19028 KB |
Output is correct |
30 |
Correct |
45 ms |
19284 KB |
Output is correct |
31 |
Correct |
41 ms |
19024 KB |
Output is correct |
32 |
Correct |
54 ms |
19088 KB |
Output is correct |
33 |
Correct |
47 ms |
19280 KB |
Output is correct |
34 |
Correct |
34 ms |
19028 KB |
Output is correct |
35 |
Correct |
43 ms |
19084 KB |
Output is correct |
36 |
Correct |
44 ms |
19276 KB |
Output is correct |
37 |
Correct |
45 ms |
19280 KB |
Output is correct |
38 |
Correct |
47 ms |
19280 KB |
Output is correct |
39 |
Correct |
44 ms |
19224 KB |
Output is correct |
40 |
Correct |
50 ms |
19280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4460 KB |
Output is correct |
11 |
Correct |
147 ms |
19160 KB |
Output is correct |
12 |
Correct |
163 ms |
18768 KB |
Output is correct |
13 |
Correct |
169 ms |
18256 KB |
Output is correct |
14 |
Correct |
180 ms |
18424 KB |
Output is correct |
15 |
Correct |
159 ms |
19280 KB |
Output is correct |
16 |
Correct |
233 ms |
18300 KB |
Output is correct |
17 |
Correct |
195 ms |
18260 KB |
Output is correct |
18 |
Correct |
113 ms |
20184 KB |
Output is correct |
19 |
Correct |
146 ms |
17236 KB |
Output is correct |
20 |
Correct |
167 ms |
18768 KB |
Output is correct |
21 |
Correct |
163 ms |
22144 KB |
Output is correct |
22 |
Correct |
177 ms |
22356 KB |
Output is correct |
23 |
Correct |
211 ms |
21332 KB |
Output is correct |
24 |
Correct |
243 ms |
21184 KB |
Output is correct |
25 |
Correct |
187 ms |
23124 KB |
Output is correct |
26 |
Correct |
270 ms |
21588 KB |
Output is correct |
27 |
Correct |
225 ms |
21588 KB |
Output is correct |
28 |
Correct |
123 ms |
24144 KB |
Output is correct |
29 |
Correct |
160 ms |
20308 KB |
Output is correct |
30 |
Correct |
210 ms |
22232 KB |
Output is correct |
31 |
Correct |
208 ms |
22100 KB |
Output is correct |
32 |
Correct |
237 ms |
22240 KB |
Output is correct |
33 |
Correct |
184 ms |
21104 KB |
Output is correct |
34 |
Correct |
235 ms |
21272 KB |
Output is correct |
35 |
Correct |
221 ms |
21684 KB |
Output is correct |
36 |
Correct |
120 ms |
20068 KB |
Output is correct |
37 |
Correct |
160 ms |
22224 KB |
Output is correct |
38 |
Correct |
172 ms |
20164 KB |
Output is correct |
39 |
Correct |
209 ms |
21328 KB |
Output is correct |
40 |
Correct |
234 ms |
21072 KB |
Output is correct |
41 |
Correct |
40 ms |
19340 KB |
Output is correct |
42 |
Correct |
38 ms |
19340 KB |
Output is correct |
43 |
Correct |
48 ms |
19024 KB |
Output is correct |
44 |
Correct |
56 ms |
19280 KB |
Output is correct |
45 |
Correct |
50 ms |
19272 KB |
Output is correct |
46 |
Correct |
52 ms |
19288 KB |
Output is correct |
47 |
Correct |
49 ms |
19284 KB |
Output is correct |
48 |
Correct |
37 ms |
19336 KB |
Output is correct |
49 |
Correct |
38 ms |
19084 KB |
Output is correct |
50 |
Correct |
41 ms |
19136 KB |
Output is correct |
51 |
Correct |
45 ms |
19340 KB |
Output is correct |
52 |
Correct |
61 ms |
19288 KB |
Output is correct |
53 |
Correct |
40 ms |
19024 KB |
Output is correct |
54 |
Correct |
49 ms |
19284 KB |
Output is correct |
55 |
Correct |
48 ms |
19232 KB |
Output is correct |
56 |
Correct |
36 ms |
19028 KB |
Output is correct |
57 |
Correct |
40 ms |
19280 KB |
Output is correct |
58 |
Correct |
47 ms |
19024 KB |
Output is correct |
59 |
Correct |
43 ms |
19028 KB |
Output is correct |
60 |
Correct |
45 ms |
19284 KB |
Output is correct |
61 |
Correct |
41 ms |
19024 KB |
Output is correct |
62 |
Correct |
54 ms |
19088 KB |
Output is correct |
63 |
Correct |
47 ms |
19280 KB |
Output is correct |
64 |
Correct |
34 ms |
19028 KB |
Output is correct |
65 |
Correct |
43 ms |
19084 KB |
Output is correct |
66 |
Correct |
44 ms |
19276 KB |
Output is correct |
67 |
Correct |
45 ms |
19280 KB |
Output is correct |
68 |
Correct |
47 ms |
19280 KB |
Output is correct |
69 |
Correct |
44 ms |
19224 KB |
Output is correct |
70 |
Correct |
50 ms |
19280 KB |
Output is correct |
71 |
Correct |
315 ms |
43604 KB |
Output is correct |
72 |
Correct |
307 ms |
43604 KB |
Output is correct |
73 |
Correct |
366 ms |
42068 KB |
Output is correct |
74 |
Correct |
585 ms |
42476 KB |
Output is correct |
75 |
Correct |
314 ms |
44424 KB |
Output is correct |
76 |
Correct |
532 ms |
42832 KB |
Output is correct |
77 |
Correct |
487 ms |
42832 KB |
Output is correct |
78 |
Correct |
192 ms |
46564 KB |
Output is correct |
79 |
Correct |
254 ms |
40588 KB |
Output is correct |
80 |
Correct |
280 ms |
43696 KB |
Output is correct |
81 |
Correct |
449 ms |
43628 KB |
Output is correct |
82 |
Correct |
593 ms |
42584 KB |
Output is correct |
83 |
Correct |
303 ms |
41668 KB |
Output is correct |
84 |
Correct |
487 ms |
42436 KB |
Output is correct |
85 |
Correct |
463 ms |
42836 KB |
Output is correct |
86 |
Correct |
177 ms |
40532 KB |
Output is correct |
87 |
Correct |
261 ms |
43496 KB |
Output is correct |
88 |
Correct |
263 ms |
40596 KB |
Output is correct |
89 |
Correct |
361 ms |
42064 KB |
Output is correct |
90 |
Correct |
416 ms |
42580 KB |
Output is correct |
91 |
Correct |
327 ms |
41636 KB |
Output is correct |
92 |
Correct |
562 ms |
42848 KB |
Output is correct |
93 |
Correct |
417 ms |
42600 KB |
Output is correct |
94 |
Correct |
162 ms |
40460 KB |
Output is correct |
95 |
Correct |
375 ms |
42836 KB |
Output is correct |
96 |
Correct |
376 ms |
42724 KB |
Output is correct |
97 |
Correct |
371 ms |
42580 KB |
Output is correct |
98 |
Correct |
385 ms |
42696 KB |
Output is correct |
99 |
Correct |
365 ms |
42488 KB |
Output is correct |
100 |
Correct |
363 ms |
42576 KB |
Output is correct |