#include <bits/stdc++.h>
#define finish(x) return cout << x << endl, 0
typedef long long ll;
typedef long double ldb;
const int md = 1e9 + 7;
const ll inf = 4e18;
const int OO = 1;
const int OOO = 1;
using namespace std;
/*
Solution idea:
first compute through dp, the values F[mask] = sum(a[S]) for S contained in mask, G[mask] = sum(a[S]) for S contained in mask.
this can be done in O(L * 2^L) (through a nontrivial dp state).
if the string would contain only (0, ?), then the answer is F[mask], mask is binary representation of ?'s.
if the string would contain only (1, ?), same thing but G[mask].
so we use both arrays to cut the time complexity for the general case.
consider the character with minimum frequency in the query string, out of (0, 1, ?). The minimum frequency is between 0 and floor(L / 3).
if the minimum is ?: solve the query naively - try to replace each ? with either 0 or 1
if the minimum is 0: we get rid of 0's, and turn them to 1's and ?'s:
for the query string x0y (x and y can be any part of the query string), its answer is x?y - x1y.
once we end up with no 0's (on base cases of this recursive procedure), we can use array G[] as described above.
if the minimum is 1: we turn 1's to 0's and ?'s, in the same manner as above, then use F[].
in all cases we produce 2 possibilities for each character from the lowest frequency, and solve each possibility in O(1), so the complexity is:
time: O(2^L * L + Q * 2^floor(L / 3)).
memory: although the dp has O(2^L * L) states, we eventually only care about the last layer for all queries, so memory is O(2^L).
the problem was very interesting so I decided to write this little description.
the code may not be clear though (and I did not explain the dp); you are welcome to send questions to my codeforces account, Noam527.
*/
const int N = 1 << 20;
int L, L2, q, a[N];
int Z[2][N], O[2][N];
int MP[256] = {};
// cur = binary rep. by 1's
int work_question(int left, int cur) {
if (!left) return a[cur];
int x = left & -left;
return work_question(left ^ x, cur) + work_question(left ^ x, cur ^ x);
}
// gets to 0's.
// cur = binary rep. of ?'s
int work_0(int left, int cur) {
if (!left) return Z[L & 1][cur];
int x = left & -left;
return work_0(left ^ x, cur ^ x) - work_0(left ^ x, cur);
}
// gets to 1's.
// cur = binary rep. of ?'s
int work_1(int left, int cur) {
if (!left) return O[L & 1][L2 - 1 - cur];
int x = left & -left;
return work_1(left ^ x, cur ^ x) - work_1(left ^ x, cur);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
MP['0'] = 0, MP['1'] = 1, MP['?'] = 2;
cin >> L >> q;
L2 = (1 << L);
for (int i = 0; i < L2; i++) {
char c;
cin >> c;
a[i] = c - '0';
Z[0][i] = O[0][i] = a[i];
}
for (int i = 1; i <= L; i++) {
for (int j = 0; j < L2; j++) {
int x = j ^ (1 << (i - 1));
if (j & (1 << (i - 1))) {
Z[i & 1][j] = Z[i & 1 ^ 1][j] + Z[i & 1 ^ 1][x];
O[i & 1][j] = O[i & 1 ^ 1][j];
}
else {
Z[i & 1][j] = Z[i & 1 ^ 1][j];
O[i & 1][j] = O[i & 1 ^ 1][j] + O[i & 1 ^ 1][x];
}
}
}
while (q--) {
string s;
cin >> s;
int cnt[3] = {}, rep[3] = {};
for (int i = 0; i < L; i++) {
cnt[MP[s[i]]]++;
rep[MP[s[i]]] |= 1 << (L - 1 - i);
}
int at = 0;
if (cnt[1] < cnt[at]) at = 1;
if (cnt[2] < cnt[at]) at = 2;
if (at == 2) {
cout << work_question(rep[at], rep[1]) << '\n';
}
else {
if (at == 0)
cout << work_1(rep[at], rep[2]) << '\n';
else
cout << work_0(rep[at], rep[2]) << '\n';
}
}
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:82:23: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
Z[i & 1][j] = Z[i & 1 ^ 1][j] + Z[i & 1 ^ 1][x];
~~^~~
snake_escaping.cpp:82:41: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
Z[i & 1][j] = Z[i & 1 ^ 1][j] + Z[i & 1 ^ 1][x];
~~^~~
snake_escaping.cpp:83:23: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
O[i & 1][j] = O[i & 1 ^ 1][j];
~~^~~
snake_escaping.cpp:86:23: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
Z[i & 1][j] = Z[i & 1 ^ 1][j];
~~^~~
snake_escaping.cpp:87:23: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
O[i & 1][j] = O[i & 1 ^ 1][j] + O[i & 1 ^ 1][x];
~~^~~
snake_escaping.cpp:87:41: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
O[i & 1][j] = O[i & 1 ^ 1][j] + O[i & 1 ^ 1][x];
~~^~~
snake_escaping.cpp:97:15: warning: array subscript has type 'char' [-Wchar-subscripts]
cnt[MP[s[i]]]++;
^
snake_escaping.cpp:98:15: warning: array subscript has type 'char' [-Wchar-subscripts]
rep[MP[s[i]]] |= 1 << (L - 1 - i);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
230 ms |
8952 KB |
Output is correct |
12 |
Correct |
225 ms |
8700 KB |
Output is correct |
13 |
Correct |
250 ms |
7928 KB |
Output is correct |
14 |
Correct |
252 ms |
7928 KB |
Output is correct |
15 |
Correct |
246 ms |
8956 KB |
Output is correct |
16 |
Correct |
262 ms |
8440 KB |
Output is correct |
17 |
Correct |
259 ms |
8060 KB |
Output is correct |
18 |
Correct |
221 ms |
9976 KB |
Output is correct |
19 |
Correct |
217 ms |
7164 KB |
Output is correct |
20 |
Correct |
233 ms |
8696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
230 ms |
8952 KB |
Output is correct |
12 |
Correct |
225 ms |
8700 KB |
Output is correct |
13 |
Correct |
250 ms |
7928 KB |
Output is correct |
14 |
Correct |
252 ms |
7928 KB |
Output is correct |
15 |
Correct |
246 ms |
8956 KB |
Output is correct |
16 |
Correct |
262 ms |
8440 KB |
Output is correct |
17 |
Correct |
259 ms |
8060 KB |
Output is correct |
18 |
Correct |
221 ms |
9976 KB |
Output is correct |
19 |
Correct |
217 ms |
7164 KB |
Output is correct |
20 |
Correct |
233 ms |
8696 KB |
Output is correct |
21 |
Correct |
252 ms |
9152 KB |
Output is correct |
22 |
Correct |
250 ms |
9208 KB |
Output is correct |
23 |
Correct |
280 ms |
8184 KB |
Output is correct |
24 |
Correct |
318 ms |
8184 KB |
Output is correct |
25 |
Correct |
272 ms |
9976 KB |
Output is correct |
26 |
Correct |
309 ms |
8568 KB |
Output is correct |
27 |
Correct |
299 ms |
8784 KB |
Output is correct |
28 |
Correct |
248 ms |
11000 KB |
Output is correct |
29 |
Correct |
233 ms |
7032 KB |
Output is correct |
30 |
Correct |
255 ms |
9208 KB |
Output is correct |
31 |
Correct |
280 ms |
9080 KB |
Output is correct |
32 |
Correct |
332 ms |
9080 KB |
Output is correct |
33 |
Correct |
267 ms |
7928 KB |
Output is correct |
34 |
Correct |
303 ms |
8068 KB |
Output is correct |
35 |
Correct |
305 ms |
8532 KB |
Output is correct |
36 |
Correct |
245 ms |
7140 KB |
Output is correct |
37 |
Correct |
244 ms |
8952 KB |
Output is correct |
38 |
Correct |
261 ms |
7032 KB |
Output is correct |
39 |
Correct |
277 ms |
8184 KB |
Output is correct |
40 |
Correct |
303 ms |
7928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
91 ms |
23288 KB |
Output is correct |
12 |
Correct |
91 ms |
23160 KB |
Output is correct |
13 |
Correct |
103 ms |
23116 KB |
Output is correct |
14 |
Correct |
103 ms |
23032 KB |
Output is correct |
15 |
Correct |
89 ms |
23160 KB |
Output is correct |
16 |
Correct |
113 ms |
23160 KB |
Output is correct |
17 |
Correct |
110 ms |
23160 KB |
Output is correct |
18 |
Correct |
84 ms |
23288 KB |
Output is correct |
19 |
Correct |
90 ms |
23032 KB |
Output is correct |
20 |
Correct |
91 ms |
23176 KB |
Output is correct |
21 |
Correct |
96 ms |
23164 KB |
Output is correct |
22 |
Correct |
109 ms |
23160 KB |
Output is correct |
23 |
Correct |
89 ms |
23032 KB |
Output is correct |
24 |
Correct |
121 ms |
23032 KB |
Output is correct |
25 |
Correct |
106 ms |
23160 KB |
Output is correct |
26 |
Correct |
83 ms |
23032 KB |
Output is correct |
27 |
Correct |
89 ms |
23160 KB |
Output is correct |
28 |
Correct |
83 ms |
23032 KB |
Output is correct |
29 |
Correct |
103 ms |
23080 KB |
Output is correct |
30 |
Correct |
104 ms |
23032 KB |
Output is correct |
31 |
Correct |
95 ms |
23032 KB |
Output is correct |
32 |
Correct |
120 ms |
23164 KB |
Output is correct |
33 |
Correct |
116 ms |
23160 KB |
Output is correct |
34 |
Correct |
86 ms |
23032 KB |
Output is correct |
35 |
Correct |
104 ms |
23160 KB |
Output is correct |
36 |
Correct |
105 ms |
23064 KB |
Output is correct |
37 |
Correct |
106 ms |
23288 KB |
Output is correct |
38 |
Correct |
107 ms |
23160 KB |
Output is correct |
39 |
Correct |
101 ms |
23160 KB |
Output is correct |
40 |
Correct |
103 ms |
23160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
230 ms |
8952 KB |
Output is correct |
12 |
Correct |
225 ms |
8700 KB |
Output is correct |
13 |
Correct |
250 ms |
7928 KB |
Output is correct |
14 |
Correct |
252 ms |
7928 KB |
Output is correct |
15 |
Correct |
246 ms |
8956 KB |
Output is correct |
16 |
Correct |
262 ms |
8440 KB |
Output is correct |
17 |
Correct |
259 ms |
8060 KB |
Output is correct |
18 |
Correct |
221 ms |
9976 KB |
Output is correct |
19 |
Correct |
217 ms |
7164 KB |
Output is correct |
20 |
Correct |
233 ms |
8696 KB |
Output is correct |
21 |
Correct |
252 ms |
9152 KB |
Output is correct |
22 |
Correct |
250 ms |
9208 KB |
Output is correct |
23 |
Correct |
280 ms |
8184 KB |
Output is correct |
24 |
Correct |
318 ms |
8184 KB |
Output is correct |
25 |
Correct |
272 ms |
9976 KB |
Output is correct |
26 |
Correct |
309 ms |
8568 KB |
Output is correct |
27 |
Correct |
299 ms |
8784 KB |
Output is correct |
28 |
Correct |
248 ms |
11000 KB |
Output is correct |
29 |
Correct |
233 ms |
7032 KB |
Output is correct |
30 |
Correct |
255 ms |
9208 KB |
Output is correct |
31 |
Correct |
280 ms |
9080 KB |
Output is correct |
32 |
Correct |
332 ms |
9080 KB |
Output is correct |
33 |
Correct |
267 ms |
7928 KB |
Output is correct |
34 |
Correct |
303 ms |
8068 KB |
Output is correct |
35 |
Correct |
305 ms |
8532 KB |
Output is correct |
36 |
Correct |
245 ms |
7140 KB |
Output is correct |
37 |
Correct |
244 ms |
8952 KB |
Output is correct |
38 |
Correct |
261 ms |
7032 KB |
Output is correct |
39 |
Correct |
277 ms |
8184 KB |
Output is correct |
40 |
Correct |
303 ms |
7928 KB |
Output is correct |
41 |
Correct |
91 ms |
23288 KB |
Output is correct |
42 |
Correct |
91 ms |
23160 KB |
Output is correct |
43 |
Correct |
103 ms |
23116 KB |
Output is correct |
44 |
Correct |
103 ms |
23032 KB |
Output is correct |
45 |
Correct |
89 ms |
23160 KB |
Output is correct |
46 |
Correct |
113 ms |
23160 KB |
Output is correct |
47 |
Correct |
110 ms |
23160 KB |
Output is correct |
48 |
Correct |
84 ms |
23288 KB |
Output is correct |
49 |
Correct |
90 ms |
23032 KB |
Output is correct |
50 |
Correct |
91 ms |
23176 KB |
Output is correct |
51 |
Correct |
96 ms |
23164 KB |
Output is correct |
52 |
Correct |
109 ms |
23160 KB |
Output is correct |
53 |
Correct |
89 ms |
23032 KB |
Output is correct |
54 |
Correct |
121 ms |
23032 KB |
Output is correct |
55 |
Correct |
106 ms |
23160 KB |
Output is correct |
56 |
Correct |
83 ms |
23032 KB |
Output is correct |
57 |
Correct |
89 ms |
23160 KB |
Output is correct |
58 |
Correct |
83 ms |
23032 KB |
Output is correct |
59 |
Correct |
103 ms |
23080 KB |
Output is correct |
60 |
Correct |
104 ms |
23032 KB |
Output is correct |
61 |
Correct |
95 ms |
23032 KB |
Output is correct |
62 |
Correct |
120 ms |
23164 KB |
Output is correct |
63 |
Correct |
116 ms |
23160 KB |
Output is correct |
64 |
Correct |
86 ms |
23032 KB |
Output is correct |
65 |
Correct |
104 ms |
23160 KB |
Output is correct |
66 |
Correct |
105 ms |
23064 KB |
Output is correct |
67 |
Correct |
106 ms |
23288 KB |
Output is correct |
68 |
Correct |
107 ms |
23160 KB |
Output is correct |
69 |
Correct |
101 ms |
23160 KB |
Output is correct |
70 |
Correct |
103 ms |
23160 KB |
Output is correct |
71 |
Correct |
447 ms |
26100 KB |
Output is correct |
72 |
Correct |
463 ms |
26360 KB |
Output is correct |
73 |
Correct |
623 ms |
24696 KB |
Output is correct |
74 |
Correct |
803 ms |
25084 KB |
Output is correct |
75 |
Correct |
497 ms |
27156 KB |
Output is correct |
76 |
Correct |
954 ms |
27896 KB |
Output is correct |
77 |
Correct |
897 ms |
25272 KB |
Output is correct |
78 |
Correct |
414 ms |
28920 KB |
Output is correct |
79 |
Correct |
421 ms |
22904 KB |
Output is correct |
80 |
Correct |
478 ms |
26360 KB |
Output is correct |
81 |
Correct |
643 ms |
26104 KB |
Output is correct |
82 |
Correct |
809 ms |
24956 KB |
Output is correct |
83 |
Correct |
497 ms |
24188 KB |
Output is correct |
84 |
Correct |
1089 ms |
24936 KB |
Output is correct |
85 |
Correct |
953 ms |
25152 KB |
Output is correct |
86 |
Correct |
394 ms |
22880 KB |
Output is correct |
87 |
Correct |
431 ms |
25976 KB |
Output is correct |
88 |
Correct |
450 ms |
23176 KB |
Output is correct |
89 |
Correct |
623 ms |
24696 KB |
Output is correct |
90 |
Correct |
697 ms |
24952 KB |
Output is correct |
91 |
Correct |
489 ms |
24056 KB |
Output is correct |
92 |
Correct |
1070 ms |
25232 KB |
Output is correct |
93 |
Correct |
936 ms |
25080 KB |
Output is correct |
94 |
Correct |
397 ms |
23032 KB |
Output is correct |
95 |
Correct |
799 ms |
25208 KB |
Output is correct |
96 |
Correct |
788 ms |
25208 KB |
Output is correct |
97 |
Correct |
797 ms |
25164 KB |
Output is correct |
98 |
Correct |
819 ms |
40568 KB |
Output is correct |
99 |
Correct |
804 ms |
46600 KB |
Output is correct |
100 |
Correct |
824 ms |
46680 KB |
Output is correct |