#include <bits/stdc++.h>
#define int long long
using namespace std;
namespace std {
template <typename T> ostream &operator<<(ostream &out, const vector<T> &vec) {
out << "[";
for (int i = 0; i < (int)vec.size(); ++i) {
out << vec[i];
if (i + 1 < (int)vec.size())
out << ", ";
}
return out << "]";
}
} // namespace std
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << H;
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int L, Q;
cin >> L >> Q;
string T;
cin >> T;
vector<int> dpSub(1 << L), dpOver(1 << L);
int sommeTox = 0;
for (char c : T)
sommeTox += c - '0';
for (int i = 0; i < (1 << L); ++i)
dpSub[i] = dpOver[i] = T[i] - '0';
for (int bit = 0; bit < L; ++bit)
for (int msk = 0; msk < (1 << L); ++msk) {
if (msk & (1 << bit))
dpSub[msk] += dpSub[msk ^ (1 << bit)];
else
dpOver[msk] += dpOver[msk ^ (1 << bit)];
}
auto brute0 = [&](int msk0, int msk1, int mskFree) {
int sol = 0;
for (int m = msk0;; m = (m - 1) & msk0) {
int sign = __builtin_popcount(m) % 2 ? -1 : 1;
sol += sign * dpOver[m | msk1];
if (!m)
break;
}
return sol;
};
auto brute1 = [&](int msk0, int msk1, int mskFree) {
int sol = 0;
for (int m = msk1;; m = (m - 1) & msk1) {
int sign = __builtin_popcount(m ^ msk1) % 2 ? -1 : 1;
sol += sign * dpSub[m | mskFree];
if (!m)
break;
}
return sol;
};
auto bruteFree = [&](int msk0, int msk1, int mskFree) {
int sol = 0;
for (int m = mskFree;; m = (m - 1) & mskFree) {
sol += T[msk1 | m] - '0';
if (!m)
break;
}
return sol;
};
for (int iRequete = 0; iRequete < Q; ++iRequete) {
string s;
cin >> s;
reverse(s.begin(), s.end());
int msk0 = 0, msk1 = 0, mskFree = 0;
for (int i = 0; i < L; ++i) {
if (s[i] == '0')
msk0 |= 1 << i;
else if (s[i] == '1')
msk1 |= 1 << i;
else
mskFree |= 1 << i;
}
if (__builtin_popcount(msk0) * 3 <= L) {
cout << brute0(msk0, msk1, mskFree) << '\n';
} else if (__builtin_popcount(msk1) * 3 <= L) {
cout << brute1(msk0, msk1, mskFree) << '\n';
} else {
cout << bruteFree(msk0, msk1, mskFree) << '\n';
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
189 ms |
15008 KB |
Output is correct |
12 |
Correct |
203 ms |
14668 KB |
Output is correct |
13 |
Correct |
243 ms |
13884 KB |
Output is correct |
14 |
Correct |
248 ms |
14144 KB |
Output is correct |
15 |
Correct |
224 ms |
14964 KB |
Output is correct |
16 |
Correct |
242 ms |
14228 KB |
Output is correct |
17 |
Correct |
241 ms |
14240 KB |
Output is correct |
18 |
Correct |
157 ms |
16004 KB |
Output is correct |
19 |
Correct |
189 ms |
13056 KB |
Output is correct |
20 |
Correct |
200 ms |
14692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
189 ms |
15008 KB |
Output is correct |
12 |
Correct |
203 ms |
14668 KB |
Output is correct |
13 |
Correct |
243 ms |
13884 KB |
Output is correct |
14 |
Correct |
248 ms |
14144 KB |
Output is correct |
15 |
Correct |
224 ms |
14964 KB |
Output is correct |
16 |
Correct |
242 ms |
14228 KB |
Output is correct |
17 |
Correct |
241 ms |
14240 KB |
Output is correct |
18 |
Correct |
157 ms |
16004 KB |
Output is correct |
19 |
Correct |
189 ms |
13056 KB |
Output is correct |
20 |
Correct |
200 ms |
14692 KB |
Output is correct |
21 |
Correct |
210 ms |
18064 KB |
Output is correct |
22 |
Correct |
227 ms |
18252 KB |
Output is correct |
23 |
Correct |
283 ms |
17240 KB |
Output is correct |
24 |
Correct |
314 ms |
17068 KB |
Output is correct |
25 |
Correct |
235 ms |
19128 KB |
Output is correct |
26 |
Correct |
303 ms |
17536 KB |
Output is correct |
27 |
Correct |
291 ms |
17512 KB |
Output is correct |
28 |
Correct |
206 ms |
20116 KB |
Output is correct |
29 |
Correct |
216 ms |
16144 KB |
Output is correct |
30 |
Correct |
221 ms |
18224 KB |
Output is correct |
31 |
Correct |
295 ms |
18072 KB |
Output is correct |
32 |
Correct |
282 ms |
18052 KB |
Output is correct |
33 |
Correct |
242 ms |
16972 KB |
Output is correct |
34 |
Correct |
296 ms |
17240 KB |
Output is correct |
35 |
Correct |
287 ms |
17712 KB |
Output is correct |
36 |
Correct |
177 ms |
16076 KB |
Output is correct |
37 |
Correct |
221 ms |
18092 KB |
Output is correct |
38 |
Correct |
234 ms |
16128 KB |
Output is correct |
39 |
Correct |
256 ms |
17272 KB |
Output is correct |
40 |
Correct |
285 ms |
17064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
60 ms |
20172 KB |
Output is correct |
12 |
Correct |
62 ms |
20204 KB |
Output is correct |
13 |
Correct |
77 ms |
20052 KB |
Output is correct |
14 |
Correct |
88 ms |
20080 KB |
Output is correct |
15 |
Correct |
66 ms |
20252 KB |
Output is correct |
16 |
Correct |
90 ms |
20136 KB |
Output is correct |
17 |
Correct |
86 ms |
20068 KB |
Output is correct |
18 |
Correct |
54 ms |
20300 KB |
Output is correct |
19 |
Correct |
58 ms |
20008 KB |
Output is correct |
20 |
Correct |
62 ms |
20176 KB |
Output is correct |
21 |
Correct |
71 ms |
20184 KB |
Output is correct |
22 |
Correct |
95 ms |
20136 KB |
Output is correct |
23 |
Correct |
61 ms |
20076 KB |
Output is correct |
24 |
Correct |
108 ms |
20048 KB |
Output is correct |
25 |
Correct |
87 ms |
20076 KB |
Output is correct |
26 |
Correct |
56 ms |
19992 KB |
Output is correct |
27 |
Correct |
59 ms |
20236 KB |
Output is correct |
28 |
Correct |
61 ms |
19948 KB |
Output is correct |
29 |
Correct |
71 ms |
20060 KB |
Output is correct |
30 |
Correct |
66 ms |
20136 KB |
Output is correct |
31 |
Correct |
63 ms |
19988 KB |
Output is correct |
32 |
Correct |
102 ms |
20076 KB |
Output is correct |
33 |
Correct |
86 ms |
20148 KB |
Output is correct |
34 |
Correct |
54 ms |
19996 KB |
Output is correct |
35 |
Correct |
75 ms |
20124 KB |
Output is correct |
36 |
Correct |
79 ms |
20044 KB |
Output is correct |
37 |
Correct |
78 ms |
20048 KB |
Output is correct |
38 |
Correct |
79 ms |
20068 KB |
Output is correct |
39 |
Correct |
75 ms |
20060 KB |
Output is correct |
40 |
Correct |
78 ms |
20052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
189 ms |
15008 KB |
Output is correct |
12 |
Correct |
203 ms |
14668 KB |
Output is correct |
13 |
Correct |
243 ms |
13884 KB |
Output is correct |
14 |
Correct |
248 ms |
14144 KB |
Output is correct |
15 |
Correct |
224 ms |
14964 KB |
Output is correct |
16 |
Correct |
242 ms |
14228 KB |
Output is correct |
17 |
Correct |
241 ms |
14240 KB |
Output is correct |
18 |
Correct |
157 ms |
16004 KB |
Output is correct |
19 |
Correct |
189 ms |
13056 KB |
Output is correct |
20 |
Correct |
200 ms |
14692 KB |
Output is correct |
21 |
Correct |
210 ms |
18064 KB |
Output is correct |
22 |
Correct |
227 ms |
18252 KB |
Output is correct |
23 |
Correct |
283 ms |
17240 KB |
Output is correct |
24 |
Correct |
314 ms |
17068 KB |
Output is correct |
25 |
Correct |
235 ms |
19128 KB |
Output is correct |
26 |
Correct |
303 ms |
17536 KB |
Output is correct |
27 |
Correct |
291 ms |
17512 KB |
Output is correct |
28 |
Correct |
206 ms |
20116 KB |
Output is correct |
29 |
Correct |
216 ms |
16144 KB |
Output is correct |
30 |
Correct |
221 ms |
18224 KB |
Output is correct |
31 |
Correct |
295 ms |
18072 KB |
Output is correct |
32 |
Correct |
282 ms |
18052 KB |
Output is correct |
33 |
Correct |
242 ms |
16972 KB |
Output is correct |
34 |
Correct |
296 ms |
17240 KB |
Output is correct |
35 |
Correct |
287 ms |
17712 KB |
Output is correct |
36 |
Correct |
177 ms |
16076 KB |
Output is correct |
37 |
Correct |
221 ms |
18092 KB |
Output is correct |
38 |
Correct |
234 ms |
16128 KB |
Output is correct |
39 |
Correct |
256 ms |
17272 KB |
Output is correct |
40 |
Correct |
285 ms |
17064 KB |
Output is correct |
41 |
Correct |
60 ms |
20172 KB |
Output is correct |
42 |
Correct |
62 ms |
20204 KB |
Output is correct |
43 |
Correct |
77 ms |
20052 KB |
Output is correct |
44 |
Correct |
88 ms |
20080 KB |
Output is correct |
45 |
Correct |
66 ms |
20252 KB |
Output is correct |
46 |
Correct |
90 ms |
20136 KB |
Output is correct |
47 |
Correct |
86 ms |
20068 KB |
Output is correct |
48 |
Correct |
54 ms |
20300 KB |
Output is correct |
49 |
Correct |
58 ms |
20008 KB |
Output is correct |
50 |
Correct |
62 ms |
20176 KB |
Output is correct |
51 |
Correct |
71 ms |
20184 KB |
Output is correct |
52 |
Correct |
95 ms |
20136 KB |
Output is correct |
53 |
Correct |
61 ms |
20076 KB |
Output is correct |
54 |
Correct |
108 ms |
20048 KB |
Output is correct |
55 |
Correct |
87 ms |
20076 KB |
Output is correct |
56 |
Correct |
56 ms |
19992 KB |
Output is correct |
57 |
Correct |
59 ms |
20236 KB |
Output is correct |
58 |
Correct |
61 ms |
19948 KB |
Output is correct |
59 |
Correct |
71 ms |
20060 KB |
Output is correct |
60 |
Correct |
66 ms |
20136 KB |
Output is correct |
61 |
Correct |
63 ms |
19988 KB |
Output is correct |
62 |
Correct |
102 ms |
20076 KB |
Output is correct |
63 |
Correct |
86 ms |
20148 KB |
Output is correct |
64 |
Correct |
54 ms |
19996 KB |
Output is correct |
65 |
Correct |
75 ms |
20124 KB |
Output is correct |
66 |
Correct |
79 ms |
20044 KB |
Output is correct |
67 |
Correct |
78 ms |
20048 KB |
Output is correct |
68 |
Correct |
79 ms |
20068 KB |
Output is correct |
69 |
Correct |
75 ms |
20060 KB |
Output is correct |
70 |
Correct |
78 ms |
20052 KB |
Output is correct |
71 |
Correct |
375 ms |
44428 KB |
Output is correct |
72 |
Correct |
399 ms |
44524 KB |
Output is correct |
73 |
Correct |
603 ms |
43060 KB |
Output is correct |
74 |
Correct |
939 ms |
43520 KB |
Output is correct |
75 |
Correct |
426 ms |
45332 KB |
Output is correct |
76 |
Correct |
913 ms |
43968 KB |
Output is correct |
77 |
Correct |
937 ms |
43612 KB |
Output is correct |
78 |
Correct |
269 ms |
47348 KB |
Output is correct |
79 |
Correct |
349 ms |
41456 KB |
Output is correct |
80 |
Correct |
408 ms |
44468 KB |
Output is correct |
81 |
Correct |
602 ms |
44520 KB |
Output is correct |
82 |
Correct |
962 ms |
43372 KB |
Output is correct |
83 |
Correct |
409 ms |
42592 KB |
Output is correct |
84 |
Correct |
968 ms |
43436 KB |
Output is correct |
85 |
Correct |
937 ms |
43584 KB |
Output is correct |
86 |
Correct |
281 ms |
41320 KB |
Output is correct |
87 |
Correct |
371 ms |
44336 KB |
Output is correct |
88 |
Correct |
397 ms |
41388 KB |
Output is correct |
89 |
Correct |
580 ms |
43112 KB |
Output is correct |
90 |
Correct |
497 ms |
43436 KB |
Output is correct |
91 |
Correct |
473 ms |
42692 KB |
Output is correct |
92 |
Correct |
1144 ms |
43804 KB |
Output is correct |
93 |
Correct |
905 ms |
43640 KB |
Output is correct |
94 |
Correct |
247 ms |
41364 KB |
Output is correct |
95 |
Correct |
825 ms |
43712 KB |
Output is correct |
96 |
Correct |
738 ms |
43540 KB |
Output is correct |
97 |
Correct |
751 ms |
43476 KB |
Output is correct |
98 |
Correct |
766 ms |
43492 KB |
Output is correct |
99 |
Correct |
758 ms |
43480 KB |
Output is correct |
100 |
Correct |
738 ms |
43456 KB |
Output is correct |