#include <bits/stdc++.h>
#define el "\n"
#define fu(i, a, b) for (long long i = a; i <= b; ++i)
#define fd(i, a, b) for (long long i = a; i >= b; --i)
#define ff first
#define ss second
#define all(v) (v).begin(), (v).end()
#define sz(v) (ll)(v).size()
#define mask(i) (1LL << (i))
#define bit(x, i) ((x) >> (i) & 1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll Rand(ll l, ll r) {
return uniform_int_distribution<ll>(l, r) (rng);
}
ll last(ll msk) {return msk & (-msk);}
ll pop_cnt(ll msk) {return __builtin_popcountll(msk);}
ll ctz(ll msk) {return __builtin_ctzll(msk);}
ll lg(ll msk) {return 63 - __builtin_clzll(msk);}
template<class T1, class T2> bool minimize(T1 &a, T2 b) {
return a > b ? a = b, 1 : 0;
}
template<class T1, class T2> bool maximize(T1 &a, T2 b) {
return a < b ? a = b, 1 : 0;
}
template<class T> void print(T a) {
for (auto x : a) cout << x << " ";
cout << el;
}
template<class T> void compress(T &a) {
sort(all(a));
a.resize(unique(all(a)) - a.begin());
}
const long long N = 4e6 + 27, lim = 17, inf = 2e18, bl = 320, base = 311, mod = 1e9 + 7;
ll l, q;
ll val[N], f[N], dp[N];
signed main() {
// freopen("CAU3.inp", "r", stdin);
// freopen("CAU3.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> l >> q;
fu(i, 0, mask(l) - 1) {
char c;
cin >> c;
f[i] = dp[i] = val[i] = c - '0';
}
fu(i, 0, l - 1) fu(msk, 0, mask(l) - 1) {
if (bit(msk, i)) f[msk] += f[msk ^ mask(i)];
else dp[msk] += dp[msk ^ mask(i)];
}
while (q--) {
string s;
cin >> s;
reverse(all(s));
ll a = 0, b = 0, c = 0, res = 0;
fu(i, 0, l - 1) {
if (s[i] == '0') a += mask(i);
else if (s[i] == '1') b += mask(i);
else c += mask(i);
}
if (pop_cnt(a) <= pop_cnt(b) && pop_cnt(a) <= pop_cnt(c)) {
fd(msk, a, 0) {
msk &= a;
if (pop_cnt(msk) & 1) res -= dp[msk | b];
else res += dp[msk | b];
}
}
else if (pop_cnt(b) <= pop_cnt(a) && pop_cnt(b) <= pop_cnt(c)) {
fd(msk, b, 0) {
msk &= b;
if (pop_cnt(msk) & 1) res -= f[(b ^ msk) | c];
else res += f[(b ^ msk) | c];
}
}
else {
fd(msk, c, 0) {
msk &= c;
res += val[msk | b];
}
}
cout << res << el;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
141 ms |
15184 KB |
Output is correct |
12 |
Correct |
146 ms |
14672 KB |
Output is correct |
13 |
Correct |
175 ms |
13908 KB |
Output is correct |
14 |
Correct |
183 ms |
14160 KB |
Output is correct |
15 |
Correct |
168 ms |
15232 KB |
Output is correct |
16 |
Correct |
189 ms |
14268 KB |
Output is correct |
17 |
Correct |
195 ms |
14500 KB |
Output is correct |
18 |
Correct |
114 ms |
16208 KB |
Output is correct |
19 |
Correct |
144 ms |
13020 KB |
Output is correct |
20 |
Correct |
158 ms |
14672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
141 ms |
15184 KB |
Output is correct |
12 |
Correct |
146 ms |
14672 KB |
Output is correct |
13 |
Correct |
175 ms |
13908 KB |
Output is correct |
14 |
Correct |
183 ms |
14160 KB |
Output is correct |
15 |
Correct |
168 ms |
15232 KB |
Output is correct |
16 |
Correct |
189 ms |
14268 KB |
Output is correct |
17 |
Correct |
195 ms |
14500 KB |
Output is correct |
18 |
Correct |
114 ms |
16208 KB |
Output is correct |
19 |
Correct |
144 ms |
13020 KB |
Output is correct |
20 |
Correct |
158 ms |
14672 KB |
Output is correct |
21 |
Correct |
172 ms |
18256 KB |
Output is correct |
22 |
Correct |
181 ms |
18256 KB |
Output is correct |
23 |
Correct |
205 ms |
17488 KB |
Output is correct |
24 |
Correct |
239 ms |
17280 KB |
Output is correct |
25 |
Correct |
185 ms |
19280 KB |
Output is correct |
26 |
Correct |
228 ms |
17748 KB |
Output is correct |
27 |
Correct |
221 ms |
17744 KB |
Output is correct |
28 |
Correct |
129 ms |
20324 KB |
Output is correct |
29 |
Correct |
158 ms |
16296 KB |
Output is correct |
30 |
Correct |
180 ms |
18400 KB |
Output is correct |
31 |
Correct |
202 ms |
18260 KB |
Output is correct |
32 |
Correct |
228 ms |
18220 KB |
Output is correct |
33 |
Correct |
179 ms |
17236 KB |
Output is correct |
34 |
Correct |
215 ms |
17232 KB |
Output is correct |
35 |
Correct |
211 ms |
17748 KB |
Output is correct |
36 |
Correct |
112 ms |
16208 KB |
Output is correct |
37 |
Correct |
160 ms |
18248 KB |
Output is correct |
38 |
Correct |
224 ms |
16232 KB |
Output is correct |
39 |
Correct |
209 ms |
17308 KB |
Output is correct |
40 |
Correct |
227 ms |
17236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
42 ms |
27220 KB |
Output is correct |
12 |
Correct |
42 ms |
27384 KB |
Output is correct |
13 |
Correct |
59 ms |
27116 KB |
Output is correct |
14 |
Correct |
61 ms |
27220 KB |
Output is correct |
15 |
Correct |
45 ms |
27216 KB |
Output is correct |
16 |
Correct |
78 ms |
27216 KB |
Output is correct |
17 |
Correct |
56 ms |
27184 KB |
Output is correct |
18 |
Correct |
40 ms |
27484 KB |
Output is correct |
19 |
Correct |
45 ms |
27216 KB |
Output is correct |
20 |
Correct |
43 ms |
27388 KB |
Output is correct |
21 |
Correct |
50 ms |
27160 KB |
Output is correct |
22 |
Correct |
60 ms |
27220 KB |
Output is correct |
23 |
Correct |
46 ms |
27128 KB |
Output is correct |
24 |
Correct |
59 ms |
27164 KB |
Output is correct |
25 |
Correct |
56 ms |
27220 KB |
Output is correct |
26 |
Correct |
38 ms |
27216 KB |
Output is correct |
27 |
Correct |
44 ms |
27216 KB |
Output is correct |
28 |
Correct |
49 ms |
27216 KB |
Output is correct |
29 |
Correct |
48 ms |
27164 KB |
Output is correct |
30 |
Correct |
56 ms |
27216 KB |
Output is correct |
31 |
Correct |
46 ms |
27216 KB |
Output is correct |
32 |
Correct |
60 ms |
27220 KB |
Output is correct |
33 |
Correct |
60 ms |
27220 KB |
Output is correct |
34 |
Correct |
37 ms |
27080 KB |
Output is correct |
35 |
Correct |
56 ms |
27112 KB |
Output is correct |
36 |
Correct |
57 ms |
27284 KB |
Output is correct |
37 |
Correct |
57 ms |
27216 KB |
Output is correct |
38 |
Correct |
52 ms |
27288 KB |
Output is correct |
39 |
Correct |
52 ms |
27320 KB |
Output is correct |
40 |
Correct |
57 ms |
27216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
141 ms |
15184 KB |
Output is correct |
12 |
Correct |
146 ms |
14672 KB |
Output is correct |
13 |
Correct |
175 ms |
13908 KB |
Output is correct |
14 |
Correct |
183 ms |
14160 KB |
Output is correct |
15 |
Correct |
168 ms |
15232 KB |
Output is correct |
16 |
Correct |
189 ms |
14268 KB |
Output is correct |
17 |
Correct |
195 ms |
14500 KB |
Output is correct |
18 |
Correct |
114 ms |
16208 KB |
Output is correct |
19 |
Correct |
144 ms |
13020 KB |
Output is correct |
20 |
Correct |
158 ms |
14672 KB |
Output is correct |
21 |
Correct |
172 ms |
18256 KB |
Output is correct |
22 |
Correct |
181 ms |
18256 KB |
Output is correct |
23 |
Correct |
205 ms |
17488 KB |
Output is correct |
24 |
Correct |
239 ms |
17280 KB |
Output is correct |
25 |
Correct |
185 ms |
19280 KB |
Output is correct |
26 |
Correct |
228 ms |
17748 KB |
Output is correct |
27 |
Correct |
221 ms |
17744 KB |
Output is correct |
28 |
Correct |
129 ms |
20324 KB |
Output is correct |
29 |
Correct |
158 ms |
16296 KB |
Output is correct |
30 |
Correct |
180 ms |
18400 KB |
Output is correct |
31 |
Correct |
202 ms |
18260 KB |
Output is correct |
32 |
Correct |
228 ms |
18220 KB |
Output is correct |
33 |
Correct |
179 ms |
17236 KB |
Output is correct |
34 |
Correct |
215 ms |
17232 KB |
Output is correct |
35 |
Correct |
211 ms |
17748 KB |
Output is correct |
36 |
Correct |
112 ms |
16208 KB |
Output is correct |
37 |
Correct |
160 ms |
18248 KB |
Output is correct |
38 |
Correct |
224 ms |
16232 KB |
Output is correct |
39 |
Correct |
209 ms |
17308 KB |
Output is correct |
40 |
Correct |
227 ms |
17236 KB |
Output is correct |
41 |
Correct |
42 ms |
27220 KB |
Output is correct |
42 |
Correct |
42 ms |
27384 KB |
Output is correct |
43 |
Correct |
59 ms |
27116 KB |
Output is correct |
44 |
Correct |
61 ms |
27220 KB |
Output is correct |
45 |
Correct |
45 ms |
27216 KB |
Output is correct |
46 |
Correct |
78 ms |
27216 KB |
Output is correct |
47 |
Correct |
56 ms |
27184 KB |
Output is correct |
48 |
Correct |
40 ms |
27484 KB |
Output is correct |
49 |
Correct |
45 ms |
27216 KB |
Output is correct |
50 |
Correct |
43 ms |
27388 KB |
Output is correct |
51 |
Correct |
50 ms |
27160 KB |
Output is correct |
52 |
Correct |
60 ms |
27220 KB |
Output is correct |
53 |
Correct |
46 ms |
27128 KB |
Output is correct |
54 |
Correct |
59 ms |
27164 KB |
Output is correct |
55 |
Correct |
56 ms |
27220 KB |
Output is correct |
56 |
Correct |
38 ms |
27216 KB |
Output is correct |
57 |
Correct |
44 ms |
27216 KB |
Output is correct |
58 |
Correct |
49 ms |
27216 KB |
Output is correct |
59 |
Correct |
48 ms |
27164 KB |
Output is correct |
60 |
Correct |
56 ms |
27216 KB |
Output is correct |
61 |
Correct |
46 ms |
27216 KB |
Output is correct |
62 |
Correct |
60 ms |
27220 KB |
Output is correct |
63 |
Correct |
60 ms |
27220 KB |
Output is correct |
64 |
Correct |
37 ms |
27080 KB |
Output is correct |
65 |
Correct |
56 ms |
27112 KB |
Output is correct |
66 |
Correct |
57 ms |
27284 KB |
Output is correct |
67 |
Correct |
57 ms |
27216 KB |
Output is correct |
68 |
Correct |
52 ms |
27288 KB |
Output is correct |
69 |
Correct |
52 ms |
27320 KB |
Output is correct |
70 |
Correct |
57 ms |
27216 KB |
Output is correct |
71 |
Correct |
284 ms |
51444 KB |
Output is correct |
72 |
Correct |
270 ms |
51540 KB |
Output is correct |
73 |
Correct |
378 ms |
50256 KB |
Output is correct |
74 |
Correct |
586 ms |
50516 KB |
Output is correct |
75 |
Correct |
323 ms |
52420 KB |
Output is correct |
76 |
Correct |
585 ms |
50868 KB |
Output is correct |
77 |
Correct |
538 ms |
51028 KB |
Output is correct |
78 |
Correct |
183 ms |
54508 KB |
Output is correct |
79 |
Correct |
278 ms |
48468 KB |
Output is correct |
80 |
Correct |
292 ms |
51536 KB |
Output is correct |
81 |
Correct |
392 ms |
51536 KB |
Output is correct |
82 |
Correct |
598 ms |
50428 KB |
Output is correct |
83 |
Correct |
306 ms |
49760 KB |
Output is correct |
84 |
Correct |
632 ms |
50516 KB |
Output is correct |
85 |
Correct |
571 ms |
50772 KB |
Output is correct |
86 |
Correct |
178 ms |
48556 KB |
Output is correct |
87 |
Correct |
267 ms |
51536 KB |
Output is correct |
88 |
Correct |
287 ms |
48464 KB |
Output is correct |
89 |
Correct |
348 ms |
50168 KB |
Output is correct |
90 |
Correct |
410 ms |
50508 KB |
Output is correct |
91 |
Correct |
295 ms |
49656 KB |
Output is correct |
92 |
Correct |
575 ms |
50772 KB |
Output is correct |
93 |
Correct |
531 ms |
50772 KB |
Output is correct |
94 |
Correct |
169 ms |
48464 KB |
Output is correct |
95 |
Correct |
509 ms |
50940 KB |
Output is correct |
96 |
Correct |
510 ms |
50516 KB |
Output is correct |
97 |
Correct |
456 ms |
50516 KB |
Output is correct |
98 |
Correct |
458 ms |
50560 KB |
Output is correct |
99 |
Correct |
556 ms |
50664 KB |
Output is correct |
100 |
Correct |
508 ms |
50516 KB |
Output is correct |