// In the name of God
// Hope is last to die
#include <bits/stdc++.h>
using namespace std;
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2")
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
// #define int long long
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
const int maxn = 1ll << 20;
const int inf = 1e9 + 7;
int n, q, dp[maxn], pd[maxn], res[100];
string s;
vector<int> pos;
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> q;
cin >> s;
for(int i = 0; i < (1ll << n); i++){
dp[i] = pd[(1ll << n) - 1 - i] = s[i] - '0';
}
for(int i = 0; i < n; i++){
for(int mask = 0; mask < (1ll << n); mask++){
if(!(mask & (1ll << i))){
dp[mask] += dp[mask + (1ll << i)];
pd[mask] += pd[mask + (1ll << i)];
}
}
}
// for(int i = 0; i < (1ll << n); i++) cout << pd[i] << ' ';
// cout << '\n';
while(q--){
string t; cin >> t;
reverse(all(t));
int one = count(all(t), '1');
int zero = count(all(t), '0');
int ques = count(all(t), '?');
int ans = 0;
pos.clear();
if(ques < 7){
int mask = 0;
for(int i = 0; i < n; i++){
if(t[i] == '1') mask += 1ll << i;
if(t[i] == '?') pos.pb(i);
}
res[0] = mask;
ans = s[mask] - '0';
for(int i = 1; i < (1ll << ques); i++){
int b = __builtin_ctz(i);
res[i] = res[i - (1ll << b)] + (1ll << pos[b]);
ans += s[res[i]] - '0';
}
}
else if(zero < 7){
int mask = 0;
for(int i = 0; i < n; i++){
if(t[i] == '1') mask += 1ll << i;
if(t[i] == '0') pos.pb(i);
}
res[0] = mask;
ans = dp[mask];
for(int i = 1; i < (1ll << zero); i++){
int b = __builtin_ctz(i);
res[i] = res[i - (1ll << b)] + (1ll << pos[b]);
if(__builtin_parity(i)) ans -= dp[res[i]];
else ans += dp[res[i]];
}
}
else{
int mask = 0;
for(int i = 0; i < n; i++){
if(t[i] == '0') mask += 1ll << i;
if(t[i] == '1') pos.pb(i);
}
res[0] = mask;
ans = pd[mask];
for(int i = 1; i < (1ll << one); i++){
int b = __builtin_ctz(i);
res[i] = res[i - (1ll << b)] + (1ll << pos[b]);
if(__builtin_parity(i)) ans -= pd[res[i]];
else ans += pd[res[i]];
}
}
cout << ans << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2528 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 |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2528 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 |
2392 KB |
Output is correct |
11 |
Correct |
243 ms |
14860 KB |
Output is correct |
12 |
Correct |
249 ms |
14416 KB |
Output is correct |
13 |
Correct |
234 ms |
13764 KB |
Output is correct |
14 |
Correct |
230 ms |
13736 KB |
Output is correct |
15 |
Correct |
305 ms |
14928 KB |
Output is correct |
16 |
Correct |
251 ms |
13908 KB |
Output is correct |
17 |
Correct |
256 ms |
14192 KB |
Output is correct |
18 |
Correct |
161 ms |
15952 KB |
Output is correct |
19 |
Correct |
199 ms |
12984 KB |
Output is correct |
20 |
Correct |
253 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2528 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 |
2392 KB |
Output is correct |
11 |
Correct |
243 ms |
14860 KB |
Output is correct |
12 |
Correct |
249 ms |
14416 KB |
Output is correct |
13 |
Correct |
234 ms |
13764 KB |
Output is correct |
14 |
Correct |
230 ms |
13736 KB |
Output is correct |
15 |
Correct |
305 ms |
14928 KB |
Output is correct |
16 |
Correct |
251 ms |
13908 KB |
Output is correct |
17 |
Correct |
256 ms |
14192 KB |
Output is correct |
18 |
Correct |
161 ms |
15952 KB |
Output is correct |
19 |
Correct |
199 ms |
12984 KB |
Output is correct |
20 |
Correct |
253 ms |
14420 KB |
Output is correct |
21 |
Correct |
243 ms |
18312 KB |
Output is correct |
22 |
Correct |
328 ms |
20308 KB |
Output is correct |
23 |
Correct |
288 ms |
19280 KB |
Output is correct |
24 |
Correct |
282 ms |
19280 KB |
Output is correct |
25 |
Correct |
267 ms |
21136 KB |
Output is correct |
26 |
Correct |
313 ms |
19792 KB |
Output is correct |
27 |
Correct |
309 ms |
19540 KB |
Output is correct |
28 |
Correct |
195 ms |
22272 KB |
Output is correct |
29 |
Correct |
232 ms |
18256 KB |
Output is correct |
30 |
Correct |
290 ms |
20464 KB |
Output is correct |
31 |
Correct |
359 ms |
20216 KB |
Output is correct |
32 |
Correct |
318 ms |
20048 KB |
Output is correct |
33 |
Correct |
277 ms |
19472 KB |
Output is correct |
34 |
Correct |
285 ms |
19032 KB |
Output is correct |
35 |
Correct |
319 ms |
19676 KB |
Output is correct |
36 |
Correct |
189 ms |
18068 KB |
Output is correct |
37 |
Correct |
340 ms |
20440 KB |
Output is correct |
38 |
Correct |
252 ms |
18260 KB |
Output is correct |
39 |
Correct |
299 ms |
19416 KB |
Output is correct |
40 |
Correct |
286 ms |
19284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2528 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 |
2392 KB |
Output is correct |
11 |
Correct |
38 ms |
12016 KB |
Output is correct |
12 |
Correct |
39 ms |
12016 KB |
Output is correct |
13 |
Correct |
43 ms |
12020 KB |
Output is correct |
14 |
Correct |
52 ms |
11960 KB |
Output is correct |
15 |
Correct |
42 ms |
12108 KB |
Output is correct |
16 |
Correct |
50 ms |
12016 KB |
Output is correct |
17 |
Correct |
47 ms |
12016 KB |
Output is correct |
18 |
Correct |
35 ms |
12104 KB |
Output is correct |
19 |
Correct |
39 ms |
11956 KB |
Output is correct |
20 |
Correct |
44 ms |
12108 KB |
Output is correct |
21 |
Correct |
45 ms |
12020 KB |
Output is correct |
22 |
Correct |
51 ms |
11884 KB |
Output is correct |
23 |
Correct |
40 ms |
12016 KB |
Output is correct |
24 |
Correct |
48 ms |
11904 KB |
Output is correct |
25 |
Correct |
49 ms |
12016 KB |
Output is correct |
26 |
Correct |
34 ms |
11952 KB |
Output is correct |
27 |
Correct |
38 ms |
12020 KB |
Output is correct |
28 |
Correct |
39 ms |
11760 KB |
Output is correct |
29 |
Correct |
42 ms |
12016 KB |
Output is correct |
30 |
Correct |
45 ms |
12020 KB |
Output is correct |
31 |
Correct |
40 ms |
11780 KB |
Output is correct |
32 |
Correct |
47 ms |
11852 KB |
Output is correct |
33 |
Correct |
46 ms |
12020 KB |
Output is correct |
34 |
Correct |
33 ms |
11712 KB |
Output is correct |
35 |
Correct |
43 ms |
12016 KB |
Output is correct |
36 |
Correct |
48 ms |
12304 KB |
Output is correct |
37 |
Correct |
44 ms |
12020 KB |
Output is correct |
38 |
Correct |
46 ms |
11852 KB |
Output is correct |
39 |
Correct |
44 ms |
12016 KB |
Output is correct |
40 |
Correct |
44 ms |
12016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2528 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 |
2392 KB |
Output is correct |
11 |
Correct |
243 ms |
14860 KB |
Output is correct |
12 |
Correct |
249 ms |
14416 KB |
Output is correct |
13 |
Correct |
234 ms |
13764 KB |
Output is correct |
14 |
Correct |
230 ms |
13736 KB |
Output is correct |
15 |
Correct |
305 ms |
14928 KB |
Output is correct |
16 |
Correct |
251 ms |
13908 KB |
Output is correct |
17 |
Correct |
256 ms |
14192 KB |
Output is correct |
18 |
Correct |
161 ms |
15952 KB |
Output is correct |
19 |
Correct |
199 ms |
12984 KB |
Output is correct |
20 |
Correct |
253 ms |
14420 KB |
Output is correct |
21 |
Correct |
243 ms |
18312 KB |
Output is correct |
22 |
Correct |
328 ms |
20308 KB |
Output is correct |
23 |
Correct |
288 ms |
19280 KB |
Output is correct |
24 |
Correct |
282 ms |
19280 KB |
Output is correct |
25 |
Correct |
267 ms |
21136 KB |
Output is correct |
26 |
Correct |
313 ms |
19792 KB |
Output is correct |
27 |
Correct |
309 ms |
19540 KB |
Output is correct |
28 |
Correct |
195 ms |
22272 KB |
Output is correct |
29 |
Correct |
232 ms |
18256 KB |
Output is correct |
30 |
Correct |
290 ms |
20464 KB |
Output is correct |
31 |
Correct |
359 ms |
20216 KB |
Output is correct |
32 |
Correct |
318 ms |
20048 KB |
Output is correct |
33 |
Correct |
277 ms |
19472 KB |
Output is correct |
34 |
Correct |
285 ms |
19032 KB |
Output is correct |
35 |
Correct |
319 ms |
19676 KB |
Output is correct |
36 |
Correct |
189 ms |
18068 KB |
Output is correct |
37 |
Correct |
340 ms |
20440 KB |
Output is correct |
38 |
Correct |
252 ms |
18260 KB |
Output is correct |
39 |
Correct |
299 ms |
19416 KB |
Output is correct |
40 |
Correct |
286 ms |
19284 KB |
Output is correct |
41 |
Correct |
38 ms |
12016 KB |
Output is correct |
42 |
Correct |
39 ms |
12016 KB |
Output is correct |
43 |
Correct |
43 ms |
12020 KB |
Output is correct |
44 |
Correct |
52 ms |
11960 KB |
Output is correct |
45 |
Correct |
42 ms |
12108 KB |
Output is correct |
46 |
Correct |
50 ms |
12016 KB |
Output is correct |
47 |
Correct |
47 ms |
12016 KB |
Output is correct |
48 |
Correct |
35 ms |
12104 KB |
Output is correct |
49 |
Correct |
39 ms |
11956 KB |
Output is correct |
50 |
Correct |
44 ms |
12108 KB |
Output is correct |
51 |
Correct |
45 ms |
12020 KB |
Output is correct |
52 |
Correct |
51 ms |
11884 KB |
Output is correct |
53 |
Correct |
40 ms |
12016 KB |
Output is correct |
54 |
Correct |
48 ms |
11904 KB |
Output is correct |
55 |
Correct |
49 ms |
12016 KB |
Output is correct |
56 |
Correct |
34 ms |
11952 KB |
Output is correct |
57 |
Correct |
38 ms |
12020 KB |
Output is correct |
58 |
Correct |
39 ms |
11760 KB |
Output is correct |
59 |
Correct |
42 ms |
12016 KB |
Output is correct |
60 |
Correct |
45 ms |
12020 KB |
Output is correct |
61 |
Correct |
40 ms |
11780 KB |
Output is correct |
62 |
Correct |
47 ms |
11852 KB |
Output is correct |
63 |
Correct |
46 ms |
12020 KB |
Output is correct |
64 |
Correct |
33 ms |
11712 KB |
Output is correct |
65 |
Correct |
43 ms |
12016 KB |
Output is correct |
66 |
Correct |
48 ms |
12304 KB |
Output is correct |
67 |
Correct |
44 ms |
12020 KB |
Output is correct |
68 |
Correct |
46 ms |
11852 KB |
Output is correct |
69 |
Correct |
44 ms |
12016 KB |
Output is correct |
70 |
Correct |
44 ms |
12016 KB |
Output is correct |
71 |
Correct |
366 ms |
36172 KB |
Output is correct |
72 |
Correct |
395 ms |
36608 KB |
Output is correct |
73 |
Correct |
438 ms |
35060 KB |
Output is correct |
74 |
Correct |
599 ms |
35224 KB |
Output is correct |
75 |
Correct |
417 ms |
37192 KB |
Output is correct |
76 |
Correct |
568 ms |
35536 KB |
Output is correct |
77 |
Correct |
526 ms |
35568 KB |
Output is correct |
78 |
Correct |
295 ms |
39412 KB |
Output is correct |
79 |
Correct |
357 ms |
33360 KB |
Output is correct |
80 |
Correct |
411 ms |
36476 KB |
Output is correct |
81 |
Correct |
476 ms |
36340 KB |
Output is correct |
82 |
Correct |
606 ms |
35316 KB |
Output is correct |
83 |
Correct |
418 ms |
34548 KB |
Output is correct |
84 |
Correct |
534 ms |
35256 KB |
Output is correct |
85 |
Correct |
510 ms |
35568 KB |
Output is correct |
86 |
Correct |
272 ms |
33264 KB |
Output is correct |
87 |
Correct |
367 ms |
36176 KB |
Output is correct |
88 |
Correct |
367 ms |
33336 KB |
Output is correct |
89 |
Correct |
456 ms |
35016 KB |
Output is correct |
90 |
Correct |
515 ms |
35400 KB |
Output is correct |
91 |
Correct |
397 ms |
34748 KB |
Output is correct |
92 |
Correct |
548 ms |
35724 KB |
Output is correct |
93 |
Correct |
513 ms |
35396 KB |
Output is correct |
94 |
Correct |
272 ms |
33336 KB |
Output is correct |
95 |
Correct |
460 ms |
35572 KB |
Output is correct |
96 |
Correct |
475 ms |
35824 KB |
Output is correct |
97 |
Correct |
466 ms |
35324 KB |
Output is correct |
98 |
Correct |
462 ms |
35436 KB |
Output is correct |
99 |
Correct |
507 ms |
35400 KB |
Output is correct |
100 |
Correct |
474 ms |
35492 KB |
Output is correct |