#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)(x).size()
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define sor(x) sort(all(x))
#define ft front()
#define bk back()
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a)
#define EACH(a, x) for (auto& a : x)
const int MOD = 1e9 + 7;
const int MX = 20;
const ll INF = 1e18;
int L, Q; string S; int A, B, C; // A = '0', B = '1', C = '?'
int cntA, cntB, cntC;
int cnt[(1 << MX)], sup[(1 << MX)], sub[(1 << MX)]; // sup - SUM_{mask \in i}, sub - SUM_{i \in mask}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> L >> Q >> S; F0R(i, (1 << L)) sup[i] = sub[i] = S[i] - '0', cnt[i] = __builtin_popcount(i);
F0R(i, L) F0R(j, (1 << L)) {
if (!(j & (1 << i))) sup[j] += sup[j ^ (1 << i)];
if ((j & (1 << i))) sub[j] += sub[j ^ (1 << i)];
}
F0R(i, Q) {
string cur; cin >> cur; A = B = C = cntA = cntB = cntC = 0; ll ans = 0;
F0R(j, L) {
if (cur[j] == '0') A ^= (1 << (L - j - 1)), cntA++;
if (cur[j] == '1') B ^= (1 << (L - j - 1)), cntB++;
if (cur[j] == '?') C ^= (1 << (L - j - 1)), cntC++;
}
if (cntA <= L / 3) {
for (int X = A; X > 0; X = (X - 1) & A) {
ans += (1 - 2 * (cnt[X] & 1)) * sup[B | X];
}
ans += sup[B]; cout << ans << "\n";
}
else if (cntB <= L / 3) {
for (int X = B; X > 0; X = (X - 1) & B) {
ans += (1 - 2 * (cnt[X] & 1)) * sub[C | X];
}
ans += sub[C]; cout << abs(ans) << "\n";
}
else { for (int X = C; X > 0; X = (X - 1) & C) ans += S[B | X] - '0'; ans += S[B] - '0'; cout << ans << "\n"; }
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 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 |
328 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 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 |
328 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 |
244 ms |
15156 KB |
Output is correct |
12 |
Correct |
262 ms |
14660 KB |
Output is correct |
13 |
Correct |
284 ms |
13932 KB |
Output is correct |
14 |
Correct |
282 ms |
14148 KB |
Output is correct |
15 |
Correct |
250 ms |
15044 KB |
Output is correct |
16 |
Correct |
277 ms |
14228 KB |
Output is correct |
17 |
Correct |
296 ms |
14344 KB |
Output is correct |
18 |
Correct |
204 ms |
16032 KB |
Output is correct |
19 |
Correct |
242 ms |
12996 KB |
Output is correct |
20 |
Correct |
259 ms |
14788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 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 |
328 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 |
244 ms |
15156 KB |
Output is correct |
12 |
Correct |
262 ms |
14660 KB |
Output is correct |
13 |
Correct |
284 ms |
13932 KB |
Output is correct |
14 |
Correct |
282 ms |
14148 KB |
Output is correct |
15 |
Correct |
250 ms |
15044 KB |
Output is correct |
16 |
Correct |
277 ms |
14228 KB |
Output is correct |
17 |
Correct |
296 ms |
14344 KB |
Output is correct |
18 |
Correct |
204 ms |
16032 KB |
Output is correct |
19 |
Correct |
242 ms |
12996 KB |
Output is correct |
20 |
Correct |
259 ms |
14788 KB |
Output is correct |
21 |
Correct |
260 ms |
18040 KB |
Output is correct |
22 |
Correct |
305 ms |
18224 KB |
Output is correct |
23 |
Correct |
341 ms |
17300 KB |
Output is correct |
24 |
Correct |
341 ms |
17220 KB |
Output is correct |
25 |
Correct |
287 ms |
19140 KB |
Output is correct |
26 |
Correct |
350 ms |
17692 KB |
Output is correct |
27 |
Correct |
342 ms |
17612 KB |
Output is correct |
28 |
Correct |
217 ms |
20164 KB |
Output is correct |
29 |
Correct |
267 ms |
16152 KB |
Output is correct |
30 |
Correct |
279 ms |
18348 KB |
Output is correct |
31 |
Correct |
312 ms |
18124 KB |
Output is correct |
32 |
Correct |
339 ms |
18120 KB |
Output is correct |
33 |
Correct |
281 ms |
17092 KB |
Output is correct |
34 |
Correct |
339 ms |
17072 KB |
Output is correct |
35 |
Correct |
343 ms |
17612 KB |
Output is correct |
36 |
Correct |
205 ms |
16276 KB |
Output is correct |
37 |
Correct |
272 ms |
18212 KB |
Output is correct |
38 |
Correct |
278 ms |
16068 KB |
Output is correct |
39 |
Correct |
320 ms |
17348 KB |
Output is correct |
40 |
Correct |
328 ms |
17092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 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 |
328 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 |
57 ms |
16056 KB |
Output is correct |
12 |
Correct |
55 ms |
16032 KB |
Output is correct |
13 |
Correct |
73 ms |
15940 KB |
Output is correct |
14 |
Correct |
70 ms |
15976 KB |
Output is correct |
15 |
Correct |
55 ms |
16096 KB |
Output is correct |
16 |
Correct |
74 ms |
15936 KB |
Output is correct |
17 |
Correct |
77 ms |
16068 KB |
Output is correct |
18 |
Correct |
50 ms |
16156 KB |
Output is correct |
19 |
Correct |
56 ms |
15820 KB |
Output is correct |
20 |
Correct |
59 ms |
15980 KB |
Output is correct |
21 |
Correct |
62 ms |
16072 KB |
Output is correct |
22 |
Correct |
86 ms |
15960 KB |
Output is correct |
23 |
Correct |
54 ms |
15972 KB |
Output is correct |
24 |
Correct |
88 ms |
15992 KB |
Output is correct |
25 |
Correct |
82 ms |
15940 KB |
Output is correct |
26 |
Correct |
51 ms |
15924 KB |
Output is correct |
27 |
Correct |
54 ms |
15972 KB |
Output is correct |
28 |
Correct |
59 ms |
15816 KB |
Output is correct |
29 |
Correct |
68 ms |
15912 KB |
Output is correct |
30 |
Correct |
63 ms |
15932 KB |
Output is correct |
31 |
Correct |
57 ms |
15912 KB |
Output is correct |
32 |
Correct |
96 ms |
16036 KB |
Output is correct |
33 |
Correct |
82 ms |
16020 KB |
Output is correct |
34 |
Correct |
50 ms |
15908 KB |
Output is correct |
35 |
Correct |
68 ms |
15932 KB |
Output is correct |
36 |
Correct |
67 ms |
15944 KB |
Output is correct |
37 |
Correct |
69 ms |
15944 KB |
Output is correct |
38 |
Correct |
78 ms |
15948 KB |
Output is correct |
39 |
Correct |
70 ms |
15976 KB |
Output is correct |
40 |
Correct |
75 ms |
15940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 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 |
328 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 |
244 ms |
15156 KB |
Output is correct |
12 |
Correct |
262 ms |
14660 KB |
Output is correct |
13 |
Correct |
284 ms |
13932 KB |
Output is correct |
14 |
Correct |
282 ms |
14148 KB |
Output is correct |
15 |
Correct |
250 ms |
15044 KB |
Output is correct |
16 |
Correct |
277 ms |
14228 KB |
Output is correct |
17 |
Correct |
296 ms |
14344 KB |
Output is correct |
18 |
Correct |
204 ms |
16032 KB |
Output is correct |
19 |
Correct |
242 ms |
12996 KB |
Output is correct |
20 |
Correct |
259 ms |
14788 KB |
Output is correct |
21 |
Correct |
260 ms |
18040 KB |
Output is correct |
22 |
Correct |
305 ms |
18224 KB |
Output is correct |
23 |
Correct |
341 ms |
17300 KB |
Output is correct |
24 |
Correct |
341 ms |
17220 KB |
Output is correct |
25 |
Correct |
287 ms |
19140 KB |
Output is correct |
26 |
Correct |
350 ms |
17692 KB |
Output is correct |
27 |
Correct |
342 ms |
17612 KB |
Output is correct |
28 |
Correct |
217 ms |
20164 KB |
Output is correct |
29 |
Correct |
267 ms |
16152 KB |
Output is correct |
30 |
Correct |
279 ms |
18348 KB |
Output is correct |
31 |
Correct |
312 ms |
18124 KB |
Output is correct |
32 |
Correct |
339 ms |
18120 KB |
Output is correct |
33 |
Correct |
281 ms |
17092 KB |
Output is correct |
34 |
Correct |
339 ms |
17072 KB |
Output is correct |
35 |
Correct |
343 ms |
17612 KB |
Output is correct |
36 |
Correct |
205 ms |
16276 KB |
Output is correct |
37 |
Correct |
272 ms |
18212 KB |
Output is correct |
38 |
Correct |
278 ms |
16068 KB |
Output is correct |
39 |
Correct |
320 ms |
17348 KB |
Output is correct |
40 |
Correct |
328 ms |
17092 KB |
Output is correct |
41 |
Correct |
57 ms |
16056 KB |
Output is correct |
42 |
Correct |
55 ms |
16032 KB |
Output is correct |
43 |
Correct |
73 ms |
15940 KB |
Output is correct |
44 |
Correct |
70 ms |
15976 KB |
Output is correct |
45 |
Correct |
55 ms |
16096 KB |
Output is correct |
46 |
Correct |
74 ms |
15936 KB |
Output is correct |
47 |
Correct |
77 ms |
16068 KB |
Output is correct |
48 |
Correct |
50 ms |
16156 KB |
Output is correct |
49 |
Correct |
56 ms |
15820 KB |
Output is correct |
50 |
Correct |
59 ms |
15980 KB |
Output is correct |
51 |
Correct |
62 ms |
16072 KB |
Output is correct |
52 |
Correct |
86 ms |
15960 KB |
Output is correct |
53 |
Correct |
54 ms |
15972 KB |
Output is correct |
54 |
Correct |
88 ms |
15992 KB |
Output is correct |
55 |
Correct |
82 ms |
15940 KB |
Output is correct |
56 |
Correct |
51 ms |
15924 KB |
Output is correct |
57 |
Correct |
54 ms |
15972 KB |
Output is correct |
58 |
Correct |
59 ms |
15816 KB |
Output is correct |
59 |
Correct |
68 ms |
15912 KB |
Output is correct |
60 |
Correct |
63 ms |
15932 KB |
Output is correct |
61 |
Correct |
57 ms |
15912 KB |
Output is correct |
62 |
Correct |
96 ms |
16036 KB |
Output is correct |
63 |
Correct |
82 ms |
16020 KB |
Output is correct |
64 |
Correct |
50 ms |
15908 KB |
Output is correct |
65 |
Correct |
68 ms |
15932 KB |
Output is correct |
66 |
Correct |
67 ms |
15944 KB |
Output is correct |
67 |
Correct |
69 ms |
15944 KB |
Output is correct |
68 |
Correct |
78 ms |
15948 KB |
Output is correct |
69 |
Correct |
70 ms |
15976 KB |
Output is correct |
70 |
Correct |
75 ms |
15940 KB |
Output is correct |
71 |
Correct |
463 ms |
40508 KB |
Output is correct |
72 |
Correct |
533 ms |
40588 KB |
Output is correct |
73 |
Correct |
717 ms |
39036 KB |
Output is correct |
74 |
Correct |
873 ms |
39396 KB |
Output is correct |
75 |
Correct |
483 ms |
41392 KB |
Output is correct |
76 |
Correct |
829 ms |
39740 KB |
Output is correct |
77 |
Correct |
836 ms |
39520 KB |
Output is correct |
78 |
Correct |
327 ms |
43312 KB |
Output is correct |
79 |
Correct |
438 ms |
37300 KB |
Output is correct |
80 |
Correct |
437 ms |
40548 KB |
Output is correct |
81 |
Correct |
619 ms |
40300 KB |
Output is correct |
82 |
Correct |
809 ms |
39332 KB |
Output is correct |
83 |
Correct |
472 ms |
38500 KB |
Output is correct |
84 |
Correct |
877 ms |
39332 KB |
Output is correct |
85 |
Correct |
839 ms |
39560 KB |
Output is correct |
86 |
Correct |
315 ms |
37272 KB |
Output is correct |
87 |
Correct |
474 ms |
40324 KB |
Output is correct |
88 |
Correct |
481 ms |
37484 KB |
Output is correct |
89 |
Correct |
744 ms |
39012 KB |
Output is correct |
90 |
Correct |
594 ms |
39472 KB |
Output is correct |
91 |
Correct |
540 ms |
38500 KB |
Output is correct |
92 |
Correct |
1132 ms |
39652 KB |
Output is correct |
93 |
Correct |
906 ms |
39764 KB |
Output is correct |
94 |
Correct |
317 ms |
37348 KB |
Output is correct |
95 |
Correct |
777 ms |
39352 KB |
Output is correct |
96 |
Correct |
799 ms |
39496 KB |
Output is correct |
97 |
Correct |
770 ms |
39356 KB |
Output is correct |
98 |
Correct |
826 ms |
39448 KB |
Output is correct |
99 |
Correct |
836 ms |
39436 KB |
Output is correct |
100 |
Correct |
745 ms |
39348 KB |
Output is correct |