#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
int main() {
cin.tie(0)->sync_with_stdio(0);
int L, Q;
cin >> L >> Q;
vector<int> t(1 << L);
for (int &i : t) {
char c;
cin >> c;
i = c - '0';
}
auto r = t;
REP (i, 1 << L) {
if (i < (i ^ ((1 << L) - 1))) {
swap(r[i], r[i ^ ((1 << L) - 1)]);
}
}
auto SOS = [&](vector<int> &v) {
REP (i, L) {
REP (j, 1 << L) {
if (j & (1 << i)) {
v[j] += v[j ^ (1 << i)];
}
}
}
};
auto v = t;
SOS(v);
SOS(r);
map<char, int> mp;
mp['0'] = 0;
mp['1'] = 1;
mp['?'] = 2;
REP (i, Q) {
vector<char> w(L);
vector<int> z(3);
for (char &j : w) {
cin >> j;
++z[mp[j]];
}
reverse(w.begin(), w.end());
if (z[0] <= z[1] && z[0] <= z[2]) {
int odp = 0, bas = 0, msk = 0;
REP (j, L) {
if (w[j] == '?') {
bas ^= 1 << j;
}
else if (w[j] == '0') {
msk ^= 1 << j;
}
}
for (int j = msk; j; j = (j - 1) & msk) {
if (__builtin_parity(msk ^ j)) {
odp -= r[j ^ bas];
}
else {
odp += r[j ^ bas];
}
}
cout << odp + (__builtin_parity(msk) ? -r[bas] : r[bas]) << '\n';
}
else if (z[1] <= z[0] && z[1] <= z[2]) {
int odp = 0, bas = 0, msk = 0;
REP (j, L) {
if (w[j] == '?') {
bas ^= 1 << j;
}
else if (w[j] == '1') {
msk ^= 1 << j;
}
}
for (int j = msk; j; j = (j - 1) & msk) {
if (__builtin_parity(msk ^ j)) {
odp -= v[j ^ bas];
}
else {
odp += v[j ^ bas];
}
}
cout << odp + (__builtin_parity(msk) ? -v[bas] : v[bas]) << '\n';
}
else {
int odp = 0, bas = 0, msk = 0;
REP (j, L) {
if (w[j] == '?') {
msk ^= 1 << j;
}
else if (w[j] == '1') {
bas ^= 1 << j;
}
}
for (int j = msk; j; j = (j - 1) & msk) {
odp += t[bas ^ j];
}
cout << odp + t[bas] << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
399 ms |
15052 KB |
Output is correct |
12 |
Correct |
407 ms |
14672 KB |
Output is correct |
13 |
Correct |
378 ms |
13900 KB |
Output is correct |
14 |
Correct |
420 ms |
14100 KB |
Output is correct |
15 |
Correct |
435 ms |
14996 KB |
Output is correct |
16 |
Correct |
437 ms |
14268 KB |
Output is correct |
17 |
Correct |
480 ms |
14368 KB |
Output is correct |
18 |
Correct |
353 ms |
16104 KB |
Output is correct |
19 |
Correct |
366 ms |
13136 KB |
Output is correct |
20 |
Correct |
382 ms |
14664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
399 ms |
15052 KB |
Output is correct |
12 |
Correct |
407 ms |
14672 KB |
Output is correct |
13 |
Correct |
378 ms |
13900 KB |
Output is correct |
14 |
Correct |
420 ms |
14100 KB |
Output is correct |
15 |
Correct |
435 ms |
14996 KB |
Output is correct |
16 |
Correct |
437 ms |
14268 KB |
Output is correct |
17 |
Correct |
480 ms |
14368 KB |
Output is correct |
18 |
Correct |
353 ms |
16104 KB |
Output is correct |
19 |
Correct |
366 ms |
13136 KB |
Output is correct |
20 |
Correct |
382 ms |
14664 KB |
Output is correct |
21 |
Correct |
475 ms |
18096 KB |
Output is correct |
22 |
Correct |
506 ms |
18284 KB |
Output is correct |
23 |
Correct |
470 ms |
17328 KB |
Output is correct |
24 |
Correct |
517 ms |
17076 KB |
Output is correct |
25 |
Correct |
532 ms |
19116 KB |
Output is correct |
26 |
Correct |
521 ms |
17628 KB |
Output is correct |
27 |
Correct |
497 ms |
17468 KB |
Output is correct |
28 |
Correct |
395 ms |
20136 KB |
Output is correct |
29 |
Correct |
436 ms |
16036 KB |
Output is correct |
30 |
Correct |
478 ms |
18240 KB |
Output is correct |
31 |
Correct |
537 ms |
18044 KB |
Output is correct |
32 |
Correct |
530 ms |
18036 KB |
Output is correct |
33 |
Correct |
446 ms |
17000 KB |
Output is correct |
34 |
Correct |
513 ms |
17012 KB |
Output is correct |
35 |
Correct |
547 ms |
17596 KB |
Output is correct |
36 |
Correct |
338 ms |
16064 KB |
Output is correct |
37 |
Correct |
469 ms |
18136 KB |
Output is correct |
38 |
Correct |
457 ms |
16008 KB |
Output is correct |
39 |
Correct |
475 ms |
17308 KB |
Output is correct |
40 |
Correct |
532 ms |
17136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
129 ms |
14864 KB |
Output is correct |
12 |
Correct |
130 ms |
14928 KB |
Output is correct |
13 |
Correct |
128 ms |
14844 KB |
Output is correct |
14 |
Correct |
135 ms |
14788 KB |
Output is correct |
15 |
Correct |
130 ms |
14928 KB |
Output is correct |
16 |
Correct |
149 ms |
14796 KB |
Output is correct |
17 |
Correct |
144 ms |
14776 KB |
Output is correct |
18 |
Correct |
117 ms |
15060 KB |
Output is correct |
19 |
Correct |
124 ms |
14676 KB |
Output is correct |
20 |
Correct |
126 ms |
14916 KB |
Output is correct |
21 |
Correct |
134 ms |
14928 KB |
Output is correct |
22 |
Correct |
139 ms |
14892 KB |
Output is correct |
23 |
Correct |
121 ms |
14800 KB |
Output is correct |
24 |
Correct |
138 ms |
14788 KB |
Output is correct |
25 |
Correct |
141 ms |
14928 KB |
Output is correct |
26 |
Correct |
115 ms |
14796 KB |
Output is correct |
27 |
Correct |
132 ms |
14924 KB |
Output is correct |
28 |
Correct |
120 ms |
14672 KB |
Output is correct |
29 |
Correct |
130 ms |
14804 KB |
Output is correct |
30 |
Correct |
137 ms |
14800 KB |
Output is correct |
31 |
Correct |
121 ms |
14796 KB |
Output is correct |
32 |
Correct |
140 ms |
14888 KB |
Output is correct |
33 |
Correct |
141 ms |
14852 KB |
Output is correct |
34 |
Correct |
115 ms |
14676 KB |
Output is correct |
35 |
Correct |
131 ms |
14896 KB |
Output is correct |
36 |
Correct |
142 ms |
14884 KB |
Output is correct |
37 |
Correct |
138 ms |
14808 KB |
Output is correct |
38 |
Correct |
136 ms |
14780 KB |
Output is correct |
39 |
Correct |
133 ms |
14824 KB |
Output is correct |
40 |
Correct |
133 ms |
14828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
399 ms |
15052 KB |
Output is correct |
12 |
Correct |
407 ms |
14672 KB |
Output is correct |
13 |
Correct |
378 ms |
13900 KB |
Output is correct |
14 |
Correct |
420 ms |
14100 KB |
Output is correct |
15 |
Correct |
435 ms |
14996 KB |
Output is correct |
16 |
Correct |
437 ms |
14268 KB |
Output is correct |
17 |
Correct |
480 ms |
14368 KB |
Output is correct |
18 |
Correct |
353 ms |
16104 KB |
Output is correct |
19 |
Correct |
366 ms |
13136 KB |
Output is correct |
20 |
Correct |
382 ms |
14664 KB |
Output is correct |
21 |
Correct |
475 ms |
18096 KB |
Output is correct |
22 |
Correct |
506 ms |
18284 KB |
Output is correct |
23 |
Correct |
470 ms |
17328 KB |
Output is correct |
24 |
Correct |
517 ms |
17076 KB |
Output is correct |
25 |
Correct |
532 ms |
19116 KB |
Output is correct |
26 |
Correct |
521 ms |
17628 KB |
Output is correct |
27 |
Correct |
497 ms |
17468 KB |
Output is correct |
28 |
Correct |
395 ms |
20136 KB |
Output is correct |
29 |
Correct |
436 ms |
16036 KB |
Output is correct |
30 |
Correct |
478 ms |
18240 KB |
Output is correct |
31 |
Correct |
537 ms |
18044 KB |
Output is correct |
32 |
Correct |
530 ms |
18036 KB |
Output is correct |
33 |
Correct |
446 ms |
17000 KB |
Output is correct |
34 |
Correct |
513 ms |
17012 KB |
Output is correct |
35 |
Correct |
547 ms |
17596 KB |
Output is correct |
36 |
Correct |
338 ms |
16064 KB |
Output is correct |
37 |
Correct |
469 ms |
18136 KB |
Output is correct |
38 |
Correct |
457 ms |
16008 KB |
Output is correct |
39 |
Correct |
475 ms |
17308 KB |
Output is correct |
40 |
Correct |
532 ms |
17136 KB |
Output is correct |
41 |
Correct |
129 ms |
14864 KB |
Output is correct |
42 |
Correct |
130 ms |
14928 KB |
Output is correct |
43 |
Correct |
128 ms |
14844 KB |
Output is correct |
44 |
Correct |
135 ms |
14788 KB |
Output is correct |
45 |
Correct |
130 ms |
14928 KB |
Output is correct |
46 |
Correct |
149 ms |
14796 KB |
Output is correct |
47 |
Correct |
144 ms |
14776 KB |
Output is correct |
48 |
Correct |
117 ms |
15060 KB |
Output is correct |
49 |
Correct |
124 ms |
14676 KB |
Output is correct |
50 |
Correct |
126 ms |
14916 KB |
Output is correct |
51 |
Correct |
134 ms |
14928 KB |
Output is correct |
52 |
Correct |
139 ms |
14892 KB |
Output is correct |
53 |
Correct |
121 ms |
14800 KB |
Output is correct |
54 |
Correct |
138 ms |
14788 KB |
Output is correct |
55 |
Correct |
141 ms |
14928 KB |
Output is correct |
56 |
Correct |
115 ms |
14796 KB |
Output is correct |
57 |
Correct |
132 ms |
14924 KB |
Output is correct |
58 |
Correct |
120 ms |
14672 KB |
Output is correct |
59 |
Correct |
130 ms |
14804 KB |
Output is correct |
60 |
Correct |
137 ms |
14800 KB |
Output is correct |
61 |
Correct |
121 ms |
14796 KB |
Output is correct |
62 |
Correct |
140 ms |
14888 KB |
Output is correct |
63 |
Correct |
141 ms |
14852 KB |
Output is correct |
64 |
Correct |
115 ms |
14676 KB |
Output is correct |
65 |
Correct |
131 ms |
14896 KB |
Output is correct |
66 |
Correct |
142 ms |
14884 KB |
Output is correct |
67 |
Correct |
138 ms |
14808 KB |
Output is correct |
68 |
Correct |
136 ms |
14780 KB |
Output is correct |
69 |
Correct |
133 ms |
14824 KB |
Output is correct |
70 |
Correct |
133 ms |
14828 KB |
Output is correct |
71 |
Correct |
764 ms |
39216 KB |
Output is correct |
72 |
Correct |
852 ms |
39356 KB |
Output is correct |
73 |
Correct |
940 ms |
37840 KB |
Output is correct |
74 |
Correct |
1160 ms |
38156 KB |
Output is correct |
75 |
Correct |
800 ms |
40200 KB |
Output is correct |
76 |
Correct |
1063 ms |
38648 KB |
Output is correct |
77 |
Correct |
1113 ms |
38316 KB |
Output is correct |
78 |
Correct |
631 ms |
42168 KB |
Output is correct |
79 |
Correct |
688 ms |
36240 KB |
Output is correct |
80 |
Correct |
767 ms |
39372 KB |
Output is correct |
81 |
Correct |
935 ms |
39156 KB |
Output is correct |
82 |
Correct |
936 ms |
38276 KB |
Output is correct |
83 |
Correct |
698 ms |
37296 KB |
Output is correct |
84 |
Correct |
1112 ms |
38192 KB |
Output is correct |
85 |
Correct |
1029 ms |
38380 KB |
Output is correct |
86 |
Correct |
508 ms |
36188 KB |
Output is correct |
87 |
Correct |
768 ms |
39232 KB |
Output is correct |
88 |
Correct |
690 ms |
36224 KB |
Output is correct |
89 |
Correct |
808 ms |
37900 KB |
Output is correct |
90 |
Correct |
840 ms |
38172 KB |
Output is correct |
91 |
Correct |
690 ms |
37304 KB |
Output is correct |
92 |
Correct |
1085 ms |
38460 KB |
Output is correct |
93 |
Correct |
1024 ms |
38356 KB |
Output is correct |
94 |
Correct |
560 ms |
36188 KB |
Output is correct |
95 |
Correct |
917 ms |
38328 KB |
Output is correct |
96 |
Correct |
912 ms |
38244 KB |
Output is correct |
97 |
Correct |
933 ms |
38232 KB |
Output is correct |
98 |
Correct |
918 ms |
38348 KB |
Output is correct |
99 |
Correct |
917 ms |
38312 KB |
Output is correct |
100 |
Correct |
915 ms |
38268 KB |
Output is correct |