/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 2e6+100, M = 1e5+10, K = 13, MX = 30;
int n, q, dp[N], dp2[N];
string s;
void solve(){
cin >> n >> q >> s;
for(int i = 0; i < (1<<n); ++i) dp[i] = dp2[i] = s[i] - '0';
for(int i = 0; i < n; ++i){
for(int mask = 0; mask < (1<<n); ++mask){
if(((1<<i)&mask) == 0) dp[mask] += dp[(1<<i) ^ mask];
if(((1<<i)&mask)) dp2[mask] += dp2[(1<<i) ^ mask];
}
}
for(int i = 0; i < q; ++i){
string t; cin >> t;
int A = 0, B = 0, C = 0;
for(int j = 0; j < n; ++j){
if(t[n-j-1] == '0') A += (1<<j);
if(t[n-j-1] == '1') B += (1<<j);
if(t[n-j-1] == '?') C += (1<<j);
}
if(__builtin_popcount(C) <= 6){
int ans = 0;
for(int S = C; S > 0; S = (S-1)&C){
ans += s[S | B] - '0';
}
ans += s[B] - '0';
cout << ans << '\n';
}else if(__builtin_popcount(A) <= 6){
int ans = 0;
for(int S = A; S > 0; S = (S-1)&A){
ans += dp[B | S] * (__builtin_popcount(S) % 2 == 0 ? 1 : -1);
}
ans += dp[B];
cout << ans << '\n';
}else{
int ans = 0;
for(int S = B; S > 0; S = (S-1)&B){
ans += dp2[S | C] * ((__builtin_popcount(B) - (int)__builtin_popcount(S)) % 2 == 0 ? 1 : -1);
}
ans += dp2[C] * ((__builtin_popcount(B)) % 2 == 0 ? 1 : -1);
cout << ans << '\n';
}
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// cin >> tt;
while(tt--){
solve();
// en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:61:15: warning: unused variable 'aa' [-Wunused-variable]
61 | int tt = 1, aa;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
195 ms |
6408 KB |
Output is correct |
12 |
Correct |
203 ms |
5972 KB |
Output is correct |
13 |
Correct |
200 ms |
5328 KB |
Output is correct |
14 |
Correct |
187 ms |
5456 KB |
Output is correct |
15 |
Correct |
246 ms |
6992 KB |
Output is correct |
16 |
Correct |
213 ms |
5456 KB |
Output is correct |
17 |
Correct |
213 ms |
5724 KB |
Output is correct |
18 |
Correct |
144 ms |
7508 KB |
Output is correct |
19 |
Correct |
164 ms |
4432 KB |
Output is correct |
20 |
Correct |
203 ms |
5972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
195 ms |
6408 KB |
Output is correct |
12 |
Correct |
203 ms |
5972 KB |
Output is correct |
13 |
Correct |
200 ms |
5328 KB |
Output is correct |
14 |
Correct |
187 ms |
5456 KB |
Output is correct |
15 |
Correct |
246 ms |
6992 KB |
Output is correct |
16 |
Correct |
213 ms |
5456 KB |
Output is correct |
17 |
Correct |
213 ms |
5724 KB |
Output is correct |
18 |
Correct |
144 ms |
7508 KB |
Output is correct |
19 |
Correct |
164 ms |
4432 KB |
Output is correct |
20 |
Correct |
203 ms |
5972 KB |
Output is correct |
21 |
Correct |
189 ms |
6428 KB |
Output is correct |
22 |
Correct |
276 ms |
6736 KB |
Output is correct |
23 |
Correct |
236 ms |
5712 KB |
Output is correct |
24 |
Correct |
218 ms |
5492 KB |
Output is correct |
25 |
Correct |
214 ms |
7496 KB |
Output is correct |
26 |
Correct |
244 ms |
5948 KB |
Output is correct |
27 |
Correct |
243 ms |
5968 KB |
Output is correct |
28 |
Correct |
145 ms |
8528 KB |
Output is correct |
29 |
Correct |
180 ms |
4448 KB |
Output is correct |
30 |
Correct |
226 ms |
6516 KB |
Output is correct |
31 |
Correct |
273 ms |
6484 KB |
Output is correct |
32 |
Correct |
232 ms |
6416 KB |
Output is correct |
33 |
Correct |
199 ms |
5260 KB |
Output is correct |
34 |
Correct |
221 ms |
5548 KB |
Output is correct |
35 |
Correct |
239 ms |
5968 KB |
Output is correct |
36 |
Correct |
138 ms |
4492 KB |
Output is correct |
37 |
Correct |
256 ms |
6536 KB |
Output is correct |
38 |
Correct |
190 ms |
4688 KB |
Output is correct |
39 |
Correct |
220 ms |
5856 KB |
Output is correct |
40 |
Correct |
215 ms |
5428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
32 ms |
12272 KB |
Output is correct |
12 |
Correct |
33 ms |
12272 KB |
Output is correct |
13 |
Correct |
37 ms |
13304 KB |
Output is correct |
14 |
Correct |
48 ms |
13280 KB |
Output is correct |
15 |
Correct |
37 ms |
13556 KB |
Output is correct |
16 |
Correct |
47 ms |
13304 KB |
Output is correct |
17 |
Correct |
40 ms |
13304 KB |
Output is correct |
18 |
Correct |
30 ms |
13560 KB |
Output is correct |
19 |
Correct |
33 ms |
13304 KB |
Output is correct |
20 |
Correct |
33 ms |
13552 KB |
Output is correct |
21 |
Correct |
39 ms |
13468 KB |
Output is correct |
22 |
Correct |
50 ms |
13300 KB |
Output is correct |
23 |
Correct |
34 ms |
13308 KB |
Output is correct |
24 |
Correct |
38 ms |
13304 KB |
Output is correct |
25 |
Correct |
42 ms |
13304 KB |
Output is correct |
26 |
Correct |
36 ms |
13412 KB |
Output is correct |
27 |
Correct |
35 ms |
13552 KB |
Output is correct |
28 |
Correct |
34 ms |
13224 KB |
Output is correct |
29 |
Correct |
36 ms |
13300 KB |
Output is correct |
30 |
Correct |
38 ms |
13296 KB |
Output is correct |
31 |
Correct |
43 ms |
13292 KB |
Output is correct |
32 |
Correct |
43 ms |
13568 KB |
Output is correct |
33 |
Correct |
41 ms |
13304 KB |
Output is correct |
34 |
Correct |
29 ms |
13304 KB |
Output is correct |
35 |
Correct |
48 ms |
13424 KB |
Output is correct |
36 |
Correct |
45 ms |
13380 KB |
Output is correct |
37 |
Correct |
42 ms |
13444 KB |
Output is correct |
38 |
Correct |
40 ms |
13484 KB |
Output is correct |
39 |
Correct |
39 ms |
13300 KB |
Output is correct |
40 |
Correct |
45 ms |
13304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
195 ms |
6408 KB |
Output is correct |
12 |
Correct |
203 ms |
5972 KB |
Output is correct |
13 |
Correct |
200 ms |
5328 KB |
Output is correct |
14 |
Correct |
187 ms |
5456 KB |
Output is correct |
15 |
Correct |
246 ms |
6992 KB |
Output is correct |
16 |
Correct |
213 ms |
5456 KB |
Output is correct |
17 |
Correct |
213 ms |
5724 KB |
Output is correct |
18 |
Correct |
144 ms |
7508 KB |
Output is correct |
19 |
Correct |
164 ms |
4432 KB |
Output is correct |
20 |
Correct |
203 ms |
5972 KB |
Output is correct |
21 |
Correct |
189 ms |
6428 KB |
Output is correct |
22 |
Correct |
276 ms |
6736 KB |
Output is correct |
23 |
Correct |
236 ms |
5712 KB |
Output is correct |
24 |
Correct |
218 ms |
5492 KB |
Output is correct |
25 |
Correct |
214 ms |
7496 KB |
Output is correct |
26 |
Correct |
244 ms |
5948 KB |
Output is correct |
27 |
Correct |
243 ms |
5968 KB |
Output is correct |
28 |
Correct |
145 ms |
8528 KB |
Output is correct |
29 |
Correct |
180 ms |
4448 KB |
Output is correct |
30 |
Correct |
226 ms |
6516 KB |
Output is correct |
31 |
Correct |
273 ms |
6484 KB |
Output is correct |
32 |
Correct |
232 ms |
6416 KB |
Output is correct |
33 |
Correct |
199 ms |
5260 KB |
Output is correct |
34 |
Correct |
221 ms |
5548 KB |
Output is correct |
35 |
Correct |
239 ms |
5968 KB |
Output is correct |
36 |
Correct |
138 ms |
4492 KB |
Output is correct |
37 |
Correct |
256 ms |
6536 KB |
Output is correct |
38 |
Correct |
190 ms |
4688 KB |
Output is correct |
39 |
Correct |
220 ms |
5856 KB |
Output is correct |
40 |
Correct |
215 ms |
5428 KB |
Output is correct |
41 |
Correct |
32 ms |
12272 KB |
Output is correct |
42 |
Correct |
33 ms |
12272 KB |
Output is correct |
43 |
Correct |
37 ms |
13304 KB |
Output is correct |
44 |
Correct |
48 ms |
13280 KB |
Output is correct |
45 |
Correct |
37 ms |
13556 KB |
Output is correct |
46 |
Correct |
47 ms |
13304 KB |
Output is correct |
47 |
Correct |
40 ms |
13304 KB |
Output is correct |
48 |
Correct |
30 ms |
13560 KB |
Output is correct |
49 |
Correct |
33 ms |
13304 KB |
Output is correct |
50 |
Correct |
33 ms |
13552 KB |
Output is correct |
51 |
Correct |
39 ms |
13468 KB |
Output is correct |
52 |
Correct |
50 ms |
13300 KB |
Output is correct |
53 |
Correct |
34 ms |
13308 KB |
Output is correct |
54 |
Correct |
38 ms |
13304 KB |
Output is correct |
55 |
Correct |
42 ms |
13304 KB |
Output is correct |
56 |
Correct |
36 ms |
13412 KB |
Output is correct |
57 |
Correct |
35 ms |
13552 KB |
Output is correct |
58 |
Correct |
34 ms |
13224 KB |
Output is correct |
59 |
Correct |
36 ms |
13300 KB |
Output is correct |
60 |
Correct |
38 ms |
13296 KB |
Output is correct |
61 |
Correct |
43 ms |
13292 KB |
Output is correct |
62 |
Correct |
43 ms |
13568 KB |
Output is correct |
63 |
Correct |
41 ms |
13304 KB |
Output is correct |
64 |
Correct |
29 ms |
13304 KB |
Output is correct |
65 |
Correct |
48 ms |
13424 KB |
Output is correct |
66 |
Correct |
45 ms |
13380 KB |
Output is correct |
67 |
Correct |
42 ms |
13444 KB |
Output is correct |
68 |
Correct |
40 ms |
13484 KB |
Output is correct |
69 |
Correct |
39 ms |
13300 KB |
Output is correct |
70 |
Correct |
45 ms |
13304 KB |
Output is correct |
71 |
Correct |
288 ms |
28236 KB |
Output is correct |
72 |
Correct |
315 ms |
34292 KB |
Output is correct |
73 |
Correct |
365 ms |
37120 KB |
Output is correct |
74 |
Correct |
599 ms |
37564 KB |
Output is correct |
75 |
Correct |
341 ms |
39856 KB |
Output is correct |
76 |
Correct |
480 ms |
37744 KB |
Output is correct |
77 |
Correct |
432 ms |
37604 KB |
Output is correct |
78 |
Correct |
221 ms |
41484 KB |
Output is correct |
79 |
Correct |
280 ms |
35404 KB |
Output is correct |
80 |
Correct |
314 ms |
38664 KB |
Output is correct |
81 |
Correct |
434 ms |
38576 KB |
Output is correct |
82 |
Correct |
545 ms |
37576 KB |
Output is correct |
83 |
Correct |
287 ms |
36848 KB |
Output is correct |
84 |
Correct |
385 ms |
37572 KB |
Output is correct |
85 |
Correct |
463 ms |
37972 KB |
Output is correct |
86 |
Correct |
201 ms |
35632 KB |
Output is correct |
87 |
Correct |
292 ms |
38532 KB |
Output is correct |
88 |
Correct |
307 ms |
35572 KB |
Output is correct |
89 |
Correct |
335 ms |
37108 KB |
Output is correct |
90 |
Correct |
385 ms |
37620 KB |
Output is correct |
91 |
Correct |
316 ms |
36860 KB |
Output is correct |
92 |
Correct |
519 ms |
37924 KB |
Output is correct |
93 |
Correct |
426 ms |
37788 KB |
Output is correct |
94 |
Correct |
211 ms |
35400 KB |
Output is correct |
95 |
Correct |
391 ms |
37628 KB |
Output is correct |
96 |
Correct |
467 ms |
37616 KB |
Output is correct |
97 |
Correct |
424 ms |
37624 KB |
Output is correct |
98 |
Correct |
418 ms |
37792 KB |
Output is correct |
99 |
Correct |
404 ms |
37616 KB |
Output is correct |
100 |
Correct |
403 ms |
37620 KB |
Output is correct |