#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];
inline int zero(int mask) {
return Z[L & 1][mask];
}
inline int one(int mask) {
return O[L & 1][mask];
}
// cur = binary rep. by 1's
int work_question(const vector<int> &ind, int nxt, int cur) {
if (nxt == ind.size()) return a[cur];
return work_question(ind, nxt + 1, cur) + work_question(ind, nxt + 1, cur | (1 << ind[nxt]));
}
// gets to 0's.
// cur = binary rep. of ?'s
int work_0(const vector<int> &ind, int nxt, int cur) {
if (nxt == ind.size()) return zero(cur);
return work_0(ind, nxt + 1, cur | (1 << ind[nxt])) - work_0(ind, nxt + 1, cur);
}
// gets to 1's.
// cur = binary rep. of ?'s
int work_1(const vector<int> &ind, int nxt, int cur) {
if (nxt == ind.size()) return one(L2 - 1 - cur);
return work_1(ind, nxt + 1, cur | (1 << ind[nxt])) - work_1(ind, nxt + 1, cur);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
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;
vector<int> pos[3];
for (int i = 0; i < L; i++) {
if (s[i] == '0')
pos[0].push_back(L - 1 - i);
else if (s[i] == '1')
pos[1].push_back(L - 1 - i);
else
pos[2].push_back(L - 1 - i);
}
int at = 0;
if (pos[1].size() < pos[at].size()) at = 1;
if (pos[2].size() < pos[at].size()) at = 2;
if (at == 2) {
int cur = 0;
for (int i = 0; i < L; i++)
if (s[i] == '1')
cur |= 1 << (L - 1 - i);
cout << work_question(pos[at], 0, cur) << '\n';
}
else {
int cur = 0;
for (int i = 0; i < L; i++)
if (s[i] == '?')
cur |= 1 << (L - 1 - i);
if (at == 0)
cout << work_1(pos[at], 0, cur) << '\n';
else
cout << work_0(pos[at], 0, cur) << '\n';
}
}
}
Compilation message
snake_escaping.cpp: In function 'int work_question(const std::vector<int>&, int, int)':
snake_escaping.cpp:49:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (nxt == ind.size()) return a[cur];
~~~~^~~~~~~~~~~~~
snake_escaping.cpp: In function 'int work_0(const std::vector<int>&, int, int)':
snake_escaping.cpp:56:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (nxt == ind.size()) return zero(cur);
~~~~^~~~~~~~~~~~~
snake_escaping.cpp: In function 'int work_1(const std::vector<int>&, int, int)':
snake_escaping.cpp:63:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (nxt == ind.size()) return one(L2 - 1 - cur);
~~~~^~~~~~~~~~~~~
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];
~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
376 KB |
Output is correct |
3 |
Correct |
6 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 |
6 ms |
376 KB |
Output is correct |
7 |
Correct |
6 ms |
504 KB |
Output is correct |
8 |
Correct |
5 ms |
464 KB |
Output is correct |
9 |
Correct |
6 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
376 KB |
Output is correct |
3 |
Correct |
6 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 |
6 ms |
376 KB |
Output is correct |
7 |
Correct |
6 ms |
504 KB |
Output is correct |
8 |
Correct |
5 ms |
464 KB |
Output is correct |
9 |
Correct |
6 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
376 KB |
Output is correct |
11 |
Correct |
723 ms |
15328 KB |
Output is correct |
12 |
Correct |
685 ms |
15096 KB |
Output is correct |
13 |
Correct |
767 ms |
14328 KB |
Output is correct |
14 |
Correct |
791 ms |
14328 KB |
Output is correct |
15 |
Correct |
719 ms |
15224 KB |
Output is correct |
16 |
Correct |
819 ms |
14456 KB |
Output is correct |
17 |
Correct |
797 ms |
14456 KB |
Output is correct |
18 |
Correct |
466 ms |
16248 KB |
Output is correct |
19 |
Correct |
706 ms |
13176 KB |
Output is correct |
20 |
Correct |
690 ms |
14960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
376 KB |
Output is correct |
3 |
Correct |
6 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 |
6 ms |
376 KB |
Output is correct |
7 |
Correct |
6 ms |
504 KB |
Output is correct |
8 |
Correct |
5 ms |
464 KB |
Output is correct |
9 |
Correct |
6 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
376 KB |
Output is correct |
11 |
Correct |
723 ms |
15328 KB |
Output is correct |
12 |
Correct |
685 ms |
15096 KB |
Output is correct |
13 |
Correct |
767 ms |
14328 KB |
Output is correct |
14 |
Correct |
791 ms |
14328 KB |
Output is correct |
15 |
Correct |
719 ms |
15224 KB |
Output is correct |
16 |
Correct |
819 ms |
14456 KB |
Output is correct |
17 |
Correct |
797 ms |
14456 KB |
Output is correct |
18 |
Correct |
466 ms |
16248 KB |
Output is correct |
19 |
Correct |
706 ms |
13176 KB |
Output is correct |
20 |
Correct |
690 ms |
14960 KB |
Output is correct |
21 |
Correct |
765 ms |
18452 KB |
Output is correct |
22 |
Correct |
795 ms |
18632 KB |
Output is correct |
23 |
Correct |
950 ms |
17496 KB |
Output is correct |
24 |
Correct |
963 ms |
17532 KB |
Output is correct |
25 |
Correct |
833 ms |
19432 KB |
Output is correct |
26 |
Correct |
983 ms |
17916 KB |
Output is correct |
27 |
Correct |
963 ms |
17800 KB |
Output is correct |
28 |
Correct |
507 ms |
20284 KB |
Output is correct |
29 |
Correct |
762 ms |
16352 KB |
Output is correct |
30 |
Correct |
812 ms |
18532 KB |
Output is correct |
31 |
Correct |
920 ms |
18476 KB |
Output is correct |
32 |
Correct |
995 ms |
18328 KB |
Output is correct |
33 |
Correct |
795 ms |
17336 KB |
Output is correct |
34 |
Correct |
957 ms |
17424 KB |
Output is correct |
35 |
Correct |
970 ms |
17732 KB |
Output is correct |
36 |
Correct |
485 ms |
16708 KB |
Output is correct |
37 |
Correct |
766 ms |
18296 KB |
Output is correct |
38 |
Correct |
790 ms |
16216 KB |
Output is correct |
39 |
Correct |
918 ms |
17400 KB |
Output is correct |
40 |
Correct |
974 ms |
17260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
376 KB |
Output is correct |
3 |
Correct |
6 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 |
6 ms |
376 KB |
Output is correct |
7 |
Correct |
6 ms |
504 KB |
Output is correct |
8 |
Correct |
5 ms |
464 KB |
Output is correct |
9 |
Correct |
6 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
376 KB |
Output is correct |
11 |
Correct |
133 ms |
23220 KB |
Output is correct |
12 |
Correct |
129 ms |
23136 KB |
Output is correct |
13 |
Correct |
145 ms |
23032 KB |
Output is correct |
14 |
Correct |
155 ms |
23160 KB |
Output is correct |
15 |
Correct |
124 ms |
23468 KB |
Output is correct |
16 |
Correct |
175 ms |
23160 KB |
Output is correct |
17 |
Correct |
174 ms |
23160 KB |
Output is correct |
18 |
Correct |
109 ms |
23288 KB |
Output is correct |
19 |
Correct |
123 ms |
23032 KB |
Output is correct |
20 |
Correct |
123 ms |
23160 KB |
Output is correct |
21 |
Correct |
150 ms |
23288 KB |
Output is correct |
22 |
Correct |
163 ms |
23160 KB |
Output is correct |
23 |
Correct |
127 ms |
23032 KB |
Output is correct |
24 |
Correct |
190 ms |
23160 KB |
Output is correct |
25 |
Correct |
174 ms |
23156 KB |
Output is correct |
26 |
Correct |
107 ms |
23032 KB |
Output is correct |
27 |
Correct |
127 ms |
23160 KB |
Output is correct |
28 |
Correct |
126 ms |
23076 KB |
Output is correct |
29 |
Correct |
140 ms |
23160 KB |
Output is correct |
30 |
Correct |
168 ms |
23160 KB |
Output is correct |
31 |
Correct |
128 ms |
23216 KB |
Output is correct |
32 |
Correct |
177 ms |
23212 KB |
Output is correct |
33 |
Correct |
172 ms |
23160 KB |
Output is correct |
34 |
Correct |
107 ms |
23032 KB |
Output is correct |
35 |
Correct |
150 ms |
23184 KB |
Output is correct |
36 |
Correct |
156 ms |
23164 KB |
Output is correct |
37 |
Correct |
161 ms |
23160 KB |
Output is correct |
38 |
Correct |
155 ms |
23160 KB |
Output is correct |
39 |
Correct |
152 ms |
23288 KB |
Output is correct |
40 |
Correct |
158 ms |
23084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
376 KB |
Output is correct |
3 |
Correct |
6 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 |
6 ms |
376 KB |
Output is correct |
7 |
Correct |
6 ms |
504 KB |
Output is correct |
8 |
Correct |
5 ms |
464 KB |
Output is correct |
9 |
Correct |
6 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
376 KB |
Output is correct |
11 |
Correct |
723 ms |
15328 KB |
Output is correct |
12 |
Correct |
685 ms |
15096 KB |
Output is correct |
13 |
Correct |
767 ms |
14328 KB |
Output is correct |
14 |
Correct |
791 ms |
14328 KB |
Output is correct |
15 |
Correct |
719 ms |
15224 KB |
Output is correct |
16 |
Correct |
819 ms |
14456 KB |
Output is correct |
17 |
Correct |
797 ms |
14456 KB |
Output is correct |
18 |
Correct |
466 ms |
16248 KB |
Output is correct |
19 |
Correct |
706 ms |
13176 KB |
Output is correct |
20 |
Correct |
690 ms |
14960 KB |
Output is correct |
21 |
Correct |
765 ms |
18452 KB |
Output is correct |
22 |
Correct |
795 ms |
18632 KB |
Output is correct |
23 |
Correct |
950 ms |
17496 KB |
Output is correct |
24 |
Correct |
963 ms |
17532 KB |
Output is correct |
25 |
Correct |
833 ms |
19432 KB |
Output is correct |
26 |
Correct |
983 ms |
17916 KB |
Output is correct |
27 |
Correct |
963 ms |
17800 KB |
Output is correct |
28 |
Correct |
507 ms |
20284 KB |
Output is correct |
29 |
Correct |
762 ms |
16352 KB |
Output is correct |
30 |
Correct |
812 ms |
18532 KB |
Output is correct |
31 |
Correct |
920 ms |
18476 KB |
Output is correct |
32 |
Correct |
995 ms |
18328 KB |
Output is correct |
33 |
Correct |
795 ms |
17336 KB |
Output is correct |
34 |
Correct |
957 ms |
17424 KB |
Output is correct |
35 |
Correct |
970 ms |
17732 KB |
Output is correct |
36 |
Correct |
485 ms |
16708 KB |
Output is correct |
37 |
Correct |
766 ms |
18296 KB |
Output is correct |
38 |
Correct |
790 ms |
16216 KB |
Output is correct |
39 |
Correct |
918 ms |
17400 KB |
Output is correct |
40 |
Correct |
974 ms |
17260 KB |
Output is correct |
41 |
Correct |
133 ms |
23220 KB |
Output is correct |
42 |
Correct |
129 ms |
23136 KB |
Output is correct |
43 |
Correct |
145 ms |
23032 KB |
Output is correct |
44 |
Correct |
155 ms |
23160 KB |
Output is correct |
45 |
Correct |
124 ms |
23468 KB |
Output is correct |
46 |
Correct |
175 ms |
23160 KB |
Output is correct |
47 |
Correct |
174 ms |
23160 KB |
Output is correct |
48 |
Correct |
109 ms |
23288 KB |
Output is correct |
49 |
Correct |
123 ms |
23032 KB |
Output is correct |
50 |
Correct |
123 ms |
23160 KB |
Output is correct |
51 |
Correct |
150 ms |
23288 KB |
Output is correct |
52 |
Correct |
163 ms |
23160 KB |
Output is correct |
53 |
Correct |
127 ms |
23032 KB |
Output is correct |
54 |
Correct |
190 ms |
23160 KB |
Output is correct |
55 |
Correct |
174 ms |
23156 KB |
Output is correct |
56 |
Correct |
107 ms |
23032 KB |
Output is correct |
57 |
Correct |
127 ms |
23160 KB |
Output is correct |
58 |
Correct |
126 ms |
23076 KB |
Output is correct |
59 |
Correct |
140 ms |
23160 KB |
Output is correct |
60 |
Correct |
168 ms |
23160 KB |
Output is correct |
61 |
Correct |
128 ms |
23216 KB |
Output is correct |
62 |
Correct |
177 ms |
23212 KB |
Output is correct |
63 |
Correct |
172 ms |
23160 KB |
Output is correct |
64 |
Correct |
107 ms |
23032 KB |
Output is correct |
65 |
Correct |
150 ms |
23184 KB |
Output is correct |
66 |
Correct |
156 ms |
23164 KB |
Output is correct |
67 |
Correct |
161 ms |
23160 KB |
Output is correct |
68 |
Correct |
155 ms |
23160 KB |
Output is correct |
69 |
Correct |
152 ms |
23288 KB |
Output is correct |
70 |
Correct |
158 ms |
23084 KB |
Output is correct |
71 |
Correct |
1202 ms |
47692 KB |
Output is correct |
72 |
Correct |
1203 ms |
47844 KB |
Output is correct |
73 |
Correct |
1697 ms |
46276 KB |
Output is correct |
74 |
Correct |
1952 ms |
46768 KB |
Output is correct |
75 |
Correct |
1331 ms |
48720 KB |
Output is correct |
76 |
Execution timed out |
2003 ms |
44176 KB |
Time limit exceeded |
77 |
Halted |
0 ms |
0 KB |
- |