#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <numeric>
#include <ctime>
#include <complex>
#include <bitset>
#include <random>
#include <climits>
#include <stack>
/*#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,tune=native")*/
using namespace std;
typedef long long ll;
typedef long double ld;
//#define int ll
#define double ld
#define loop(i, n) for(int i = 0; i < (int)n; ++i)
#define loop1(i, n) for(int i = 1; i <= (int)n; ++i)
#define F first
#define S second
#define pb push_back
#define pi pair <int, int>
#define all(x) begin(x), end(x)
#define ti tuple <int, int, int>
#define Point Vect
#define no {cout << "No"; return;}
#define yes {cout << "Yes"; return;}
#define mkp make_pair
#define mkt make_tuple
#define cerr if(0) cerr
const int N = 1100000;
int n, a[2][N], f[2][N];
bool odd[N];
void solveq(string & s) {
int maskq = 0, masku = 0;
loop(i, n) {
if (s[i] == '?')
maskq |= (1 << i);
else if (s[i] == '1')
masku |= (1 << i);
}
int ans = a[0][masku];
for (int mask = maskq; mask; mask = (maskq & (mask - 1)))
ans += a[0][mask | masku];
cout << ans << "\n";
}
void solve01(string& s, bool fl) {
int maskq = 0, masku = 0;
loop(i, n) {
if (s[i] == '?')
maskq |= (1 << i);
else if (s[i] == '1')
masku |= (1 << i);
}
int ans = f[fl][maskq] * (odd[masku] ? -1 : 1);
for (int mask = masku; mask; mask = (masku & (mask - 1)))
ans += f[fl][mask | maskq] * (odd[mask] == odd[masku] ? 1 : -1);
cout << ans << "\n";
}
void solve() {
int q;
cin >> n >> q;
loop(i, (1 << n)) {
char c; cin >> c;
a[0][i] = f[0][i] = f[1][(1 << n) - 1 - i] = a[1][(1 << n) - i - 1] = c - '0';
}
loop(i, n)
loop(mask, (1 << n))
if (mask & (1 << i)) {
f[0][mask] += f[0][mask ^ (1 << i)];
f[1][mask] += f[1][mask ^ (1 << i)];
}
loop(mask, (1 << n))
loop(i, n)
if (mask & (1 << i))
odd[mask] = !odd[mask ^ (1 << i)];
loop(i, q) {
string s; cin >> s;
reverse(all(s));
int cq = 0, c0 = 0, c1 = 0;
loop(j, n)
++(s[j] == '?' ? cq : (s[j] == '0' ? c0 : c1));
if (min({ cq, c1, c0 }) == cq)
solveq(s);
else if (min(c1, c0) == c1)
solve01(s, 0);
else {
loop(j, n) {
if (s[j] == '0')
s[j] = '1';
else if (s[j] == '1')
s[j] = '0';
}
solve01(s, 1);
}
}
}
signed main() {
//freopen("art2.in", "r", stdin);
//freopen("art2.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//int t; cin >> t; loop(i, t)
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
412 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
412 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
325 ms |
15340 KB |
Output is correct |
12 |
Correct |
300 ms |
14828 KB |
Output is correct |
13 |
Correct |
312 ms |
14060 KB |
Output is correct |
14 |
Correct |
307 ms |
14316 KB |
Output is correct |
15 |
Correct |
328 ms |
15212 KB |
Output is correct |
16 |
Correct |
332 ms |
14444 KB |
Output is correct |
17 |
Correct |
367 ms |
14444 KB |
Output is correct |
18 |
Correct |
235 ms |
16108 KB |
Output is correct |
19 |
Correct |
260 ms |
13164 KB |
Output is correct |
20 |
Correct |
366 ms |
14928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
412 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
325 ms |
15340 KB |
Output is correct |
12 |
Correct |
300 ms |
14828 KB |
Output is correct |
13 |
Correct |
312 ms |
14060 KB |
Output is correct |
14 |
Correct |
307 ms |
14316 KB |
Output is correct |
15 |
Correct |
328 ms |
15212 KB |
Output is correct |
16 |
Correct |
332 ms |
14444 KB |
Output is correct |
17 |
Correct |
367 ms |
14444 KB |
Output is correct |
18 |
Correct |
235 ms |
16108 KB |
Output is correct |
19 |
Correct |
260 ms |
13164 KB |
Output is correct |
20 |
Correct |
366 ms |
14928 KB |
Output is correct |
21 |
Correct |
402 ms |
18412 KB |
Output is correct |
22 |
Correct |
365 ms |
18584 KB |
Output is correct |
23 |
Correct |
384 ms |
17516 KB |
Output is correct |
24 |
Correct |
379 ms |
17388 KB |
Output is correct |
25 |
Correct |
437 ms |
19308 KB |
Output is correct |
26 |
Correct |
421 ms |
17836 KB |
Output is correct |
27 |
Correct |
451 ms |
17672 KB |
Output is correct |
28 |
Correct |
223 ms |
20332 KB |
Output is correct |
29 |
Correct |
306 ms |
16236 KB |
Output is correct |
30 |
Correct |
463 ms |
18668 KB |
Output is correct |
31 |
Correct |
470 ms |
18412 KB |
Output is correct |
32 |
Correct |
406 ms |
18284 KB |
Output is correct |
33 |
Correct |
396 ms |
17156 KB |
Output is correct |
34 |
Correct |
421 ms |
17260 KB |
Output is correct |
35 |
Correct |
453 ms |
17644 KB |
Output is correct |
36 |
Correct |
226 ms |
16364 KB |
Output is correct |
37 |
Correct |
321 ms |
18284 KB |
Output is correct |
38 |
Correct |
319 ms |
16236 KB |
Output is correct |
39 |
Correct |
434 ms |
17516 KB |
Output is correct |
40 |
Correct |
380 ms |
17260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
412 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
159 ms |
20076 KB |
Output is correct |
12 |
Correct |
158 ms |
20076 KB |
Output is correct |
13 |
Correct |
160 ms |
20076 KB |
Output is correct |
14 |
Correct |
185 ms |
20076 KB |
Output is correct |
15 |
Correct |
158 ms |
20204 KB |
Output is correct |
16 |
Correct |
190 ms |
20204 KB |
Output is correct |
17 |
Correct |
173 ms |
20076 KB |
Output is correct |
18 |
Correct |
139 ms |
20332 KB |
Output is correct |
19 |
Correct |
149 ms |
19948 KB |
Output is correct |
20 |
Correct |
180 ms |
20204 KB |
Output is correct |
21 |
Correct |
168 ms |
20204 KB |
Output is correct |
22 |
Correct |
193 ms |
20204 KB |
Output is correct |
23 |
Correct |
175 ms |
20076 KB |
Output is correct |
24 |
Correct |
180 ms |
20332 KB |
Output is correct |
25 |
Correct |
171 ms |
20076 KB |
Output is correct |
26 |
Correct |
138 ms |
19948 KB |
Output is correct |
27 |
Correct |
151 ms |
20204 KB |
Output is correct |
28 |
Correct |
147 ms |
19948 KB |
Output is correct |
29 |
Correct |
165 ms |
20204 KB |
Output is correct |
30 |
Correct |
172 ms |
20076 KB |
Output is correct |
31 |
Correct |
158 ms |
20076 KB |
Output is correct |
32 |
Correct |
185 ms |
20076 KB |
Output is correct |
33 |
Correct |
181 ms |
20076 KB |
Output is correct |
34 |
Correct |
137 ms |
20076 KB |
Output is correct |
35 |
Correct |
169 ms |
20032 KB |
Output is correct |
36 |
Correct |
166 ms |
20036 KB |
Output is correct |
37 |
Correct |
167 ms |
20136 KB |
Output is correct |
38 |
Correct |
192 ms |
20116 KB |
Output is correct |
39 |
Correct |
167 ms |
20076 KB |
Output is correct |
40 |
Correct |
165 ms |
20076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
412 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
325 ms |
15340 KB |
Output is correct |
12 |
Correct |
300 ms |
14828 KB |
Output is correct |
13 |
Correct |
312 ms |
14060 KB |
Output is correct |
14 |
Correct |
307 ms |
14316 KB |
Output is correct |
15 |
Correct |
328 ms |
15212 KB |
Output is correct |
16 |
Correct |
332 ms |
14444 KB |
Output is correct |
17 |
Correct |
367 ms |
14444 KB |
Output is correct |
18 |
Correct |
235 ms |
16108 KB |
Output is correct |
19 |
Correct |
260 ms |
13164 KB |
Output is correct |
20 |
Correct |
366 ms |
14928 KB |
Output is correct |
21 |
Correct |
402 ms |
18412 KB |
Output is correct |
22 |
Correct |
365 ms |
18584 KB |
Output is correct |
23 |
Correct |
384 ms |
17516 KB |
Output is correct |
24 |
Correct |
379 ms |
17388 KB |
Output is correct |
25 |
Correct |
437 ms |
19308 KB |
Output is correct |
26 |
Correct |
421 ms |
17836 KB |
Output is correct |
27 |
Correct |
451 ms |
17672 KB |
Output is correct |
28 |
Correct |
223 ms |
20332 KB |
Output is correct |
29 |
Correct |
306 ms |
16236 KB |
Output is correct |
30 |
Correct |
463 ms |
18668 KB |
Output is correct |
31 |
Correct |
470 ms |
18412 KB |
Output is correct |
32 |
Correct |
406 ms |
18284 KB |
Output is correct |
33 |
Correct |
396 ms |
17156 KB |
Output is correct |
34 |
Correct |
421 ms |
17260 KB |
Output is correct |
35 |
Correct |
453 ms |
17644 KB |
Output is correct |
36 |
Correct |
226 ms |
16364 KB |
Output is correct |
37 |
Correct |
321 ms |
18284 KB |
Output is correct |
38 |
Correct |
319 ms |
16236 KB |
Output is correct |
39 |
Correct |
434 ms |
17516 KB |
Output is correct |
40 |
Correct |
380 ms |
17260 KB |
Output is correct |
41 |
Correct |
159 ms |
20076 KB |
Output is correct |
42 |
Correct |
158 ms |
20076 KB |
Output is correct |
43 |
Correct |
160 ms |
20076 KB |
Output is correct |
44 |
Correct |
185 ms |
20076 KB |
Output is correct |
45 |
Correct |
158 ms |
20204 KB |
Output is correct |
46 |
Correct |
190 ms |
20204 KB |
Output is correct |
47 |
Correct |
173 ms |
20076 KB |
Output is correct |
48 |
Correct |
139 ms |
20332 KB |
Output is correct |
49 |
Correct |
149 ms |
19948 KB |
Output is correct |
50 |
Correct |
180 ms |
20204 KB |
Output is correct |
51 |
Correct |
168 ms |
20204 KB |
Output is correct |
52 |
Correct |
193 ms |
20204 KB |
Output is correct |
53 |
Correct |
175 ms |
20076 KB |
Output is correct |
54 |
Correct |
180 ms |
20332 KB |
Output is correct |
55 |
Correct |
171 ms |
20076 KB |
Output is correct |
56 |
Correct |
138 ms |
19948 KB |
Output is correct |
57 |
Correct |
151 ms |
20204 KB |
Output is correct |
58 |
Correct |
147 ms |
19948 KB |
Output is correct |
59 |
Correct |
165 ms |
20204 KB |
Output is correct |
60 |
Correct |
172 ms |
20076 KB |
Output is correct |
61 |
Correct |
158 ms |
20076 KB |
Output is correct |
62 |
Correct |
185 ms |
20076 KB |
Output is correct |
63 |
Correct |
181 ms |
20076 KB |
Output is correct |
64 |
Correct |
137 ms |
20076 KB |
Output is correct |
65 |
Correct |
169 ms |
20032 KB |
Output is correct |
66 |
Correct |
166 ms |
20036 KB |
Output is correct |
67 |
Correct |
167 ms |
20136 KB |
Output is correct |
68 |
Correct |
192 ms |
20116 KB |
Output is correct |
69 |
Correct |
167 ms |
20076 KB |
Output is correct |
70 |
Correct |
165 ms |
20076 KB |
Output is correct |
71 |
Correct |
838 ms |
44396 KB |
Output is correct |
72 |
Correct |
718 ms |
44652 KB |
Output is correct |
73 |
Correct |
864 ms |
43340 KB |
Output is correct |
74 |
Correct |
1266 ms |
43500 KB |
Output is correct |
75 |
Correct |
813 ms |
45548 KB |
Output is correct |
76 |
Correct |
1322 ms |
43756 KB |
Output is correct |
77 |
Correct |
1173 ms |
43680 KB |
Output is correct |
78 |
Correct |
439 ms |
47608 KB |
Output is correct |
79 |
Correct |
635 ms |
41412 KB |
Output is correct |
80 |
Correct |
987 ms |
44652 KB |
Output is correct |
81 |
Correct |
1008 ms |
44520 KB |
Output is correct |
82 |
Correct |
1509 ms |
43540 KB |
Output is correct |
83 |
Correct |
719 ms |
42600 KB |
Output is correct |
84 |
Correct |
1160 ms |
43724 KB |
Output is correct |
85 |
Correct |
1170 ms |
43700 KB |
Output is correct |
86 |
Correct |
432 ms |
41512 KB |
Output is correct |
87 |
Correct |
693 ms |
44604 KB |
Output is correct |
88 |
Correct |
614 ms |
41404 KB |
Output is correct |
89 |
Correct |
883 ms |
43076 KB |
Output is correct |
90 |
Correct |
888 ms |
43500 KB |
Output is correct |
91 |
Correct |
657 ms |
42768 KB |
Output is correct |
92 |
Correct |
1304 ms |
43480 KB |
Output is correct |
93 |
Correct |
1182 ms |
43604 KB |
Output is correct |
94 |
Correct |
439 ms |
41452 KB |
Output is correct |
95 |
Correct |
1012 ms |
43756 KB |
Output is correct |
96 |
Correct |
1049 ms |
43500 KB |
Output is correct |
97 |
Correct |
1014 ms |
43628 KB |
Output is correct |
98 |
Correct |
1004 ms |
43548 KB |
Output is correct |
99 |
Correct |
1024 ms |
43632 KB |
Output is correct |
100 |
Correct |
1000 ms |
43472 KB |
Output is correct |