#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<long long, long long>
#define t3i tuple<long long, long long, long long>
const int MinVop = 6;
int calc(vector<int> &tri, int x, int l, string &tox) {
if (l == x) {
int v = 0;
for (int i = 0; i < l; i++) {
v *= 2;
v += tri[i];
}
return tox[v] - '0';
}
if (tri[x] == 2) {
int ans = 0;
tri[x] = 0;
ans += calc(tri, x + 1, l, tox);
tri[x] = 1;
ans += calc(tri, x + 1, l, tox);
tri[x] = 2;
return ans;
}
return calc(tri, x + 1, l, tox);
}
int build(vector<int> &tri, vector<int> &ans, string &tox, int l, int q) {
int v = 0, vop = 0;
for (int i = 0; i < l; i++) {
v *= 3;
v += tri[i];
vop += (tri[i] == 2);
}
if (vop < MinVop) {
vector<int> tri_(l);
for (int i = 0; i < l; i++) tri_[i] = tri[i];
ans[v] = calc(tri_, 0, l, tox);
}
if (ans[v] != -1) return ans[v];
for (int i = 0; i < l; i++) {
if (tri[i] == 2) {
int cans = 0;
tri[i] = 0;
cans += build(tri, ans, tox, l, q);
tri[i] = 1;
cans += build(tri, ans, tox, l, q);
tri[i] = 2;
ans[v] = cans;
}
}
return ans[v];
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int l, q;
string tox;
cin >> l >> q >> tox;
vector<int> ans(pow(3, l) + 100, -1);
vector<int> tri(l, 2);
build(tri, ans, tox, l, q);
while (q--) {
string s;
cin >> s;
int v = 0, vop = 0;
for (int i = 0; i < l; i++) {
v *= 3;
v += (s[i] == '?' ? 2 : s[i] - '0');
vop += (s[i] == '?');
}
if (vop >= MinVop) {
cout << ans[v] << "\n";
} else {
vector<int> tri(l);
for (int i = 0; i < l; i++) tri[i] = (s[i] == '?' ? 2 : s[i] - '0');
cout << calc(tri, 0, l, tox) << "\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
860 KB |
Output is correct |
2 |
Correct |
15 ms |
936 KB |
Output is correct |
3 |
Correct |
15 ms |
932 KB |
Output is correct |
4 |
Correct |
22 ms |
860 KB |
Output is correct |
5 |
Correct |
15 ms |
860 KB |
Output is correct |
6 |
Correct |
15 ms |
936 KB |
Output is correct |
7 |
Correct |
15 ms |
860 KB |
Output is correct |
8 |
Correct |
15 ms |
928 KB |
Output is correct |
9 |
Correct |
15 ms |
860 KB |
Output is correct |
10 |
Correct |
15 ms |
856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
860 KB |
Output is correct |
2 |
Correct |
15 ms |
936 KB |
Output is correct |
3 |
Correct |
15 ms |
932 KB |
Output is correct |
4 |
Correct |
22 ms |
860 KB |
Output is correct |
5 |
Correct |
15 ms |
860 KB |
Output is correct |
6 |
Correct |
15 ms |
936 KB |
Output is correct |
7 |
Correct |
15 ms |
860 KB |
Output is correct |
8 |
Correct |
15 ms |
928 KB |
Output is correct |
9 |
Correct |
15 ms |
860 KB |
Output is correct |
10 |
Correct |
15 ms |
856 KB |
Output is correct |
11 |
Correct |
526 ms |
4688 KB |
Output is correct |
12 |
Correct |
386 ms |
4568 KB |
Output is correct |
13 |
Correct |
264 ms |
3644 KB |
Output is correct |
14 |
Correct |
263 ms |
3920 KB |
Output is correct |
15 |
Correct |
167 ms |
4716 KB |
Output is correct |
16 |
Correct |
342 ms |
4036 KB |
Output is correct |
17 |
Correct |
351 ms |
4020 KB |
Output is correct |
18 |
Correct |
118 ms |
5788 KB |
Output is correct |
19 |
Correct |
141 ms |
2896 KB |
Output is correct |
20 |
Correct |
339 ms |
4436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
860 KB |
Output is correct |
2 |
Correct |
15 ms |
936 KB |
Output is correct |
3 |
Correct |
15 ms |
932 KB |
Output is correct |
4 |
Correct |
22 ms |
860 KB |
Output is correct |
5 |
Correct |
15 ms |
860 KB |
Output is correct |
6 |
Correct |
15 ms |
936 KB |
Output is correct |
7 |
Correct |
15 ms |
860 KB |
Output is correct |
8 |
Correct |
15 ms |
928 KB |
Output is correct |
9 |
Correct |
15 ms |
860 KB |
Output is correct |
10 |
Correct |
15 ms |
856 KB |
Output is correct |
11 |
Correct |
526 ms |
4688 KB |
Output is correct |
12 |
Correct |
386 ms |
4568 KB |
Output is correct |
13 |
Correct |
264 ms |
3644 KB |
Output is correct |
14 |
Correct |
263 ms |
3920 KB |
Output is correct |
15 |
Correct |
167 ms |
4716 KB |
Output is correct |
16 |
Correct |
342 ms |
4036 KB |
Output is correct |
17 |
Correct |
351 ms |
4020 KB |
Output is correct |
18 |
Correct |
118 ms |
5788 KB |
Output is correct |
19 |
Correct |
141 ms |
2896 KB |
Output is correct |
20 |
Correct |
339 ms |
4436 KB |
Output is correct |
21 |
Correct |
1282 ms |
16788 KB |
Output is correct |
22 |
Correct |
1452 ms |
16976 KB |
Output is correct |
23 |
Correct |
1569 ms |
16100 KB |
Output is correct |
24 |
Correct |
1549 ms |
15956 KB |
Output is correct |
25 |
Correct |
1254 ms |
17812 KB |
Output is correct |
26 |
Correct |
1605 ms |
16288 KB |
Output is correct |
27 |
Correct |
1561 ms |
16208 KB |
Output is correct |
28 |
Correct |
1232 ms |
18832 KB |
Output is correct |
29 |
Correct |
1251 ms |
14932 KB |
Output is correct |
30 |
Correct |
1411 ms |
17052 KB |
Output is correct |
31 |
Correct |
1248 ms |
16804 KB |
Output is correct |
32 |
Correct |
1916 ms |
23888 KB |
Output is correct |
33 |
Correct |
1452 ms |
22972 KB |
Output is correct |
34 |
Correct |
1626 ms |
23124 KB |
Output is correct |
35 |
Correct |
1584 ms |
23360 KB |
Output is correct |
36 |
Correct |
1252 ms |
21968 KB |
Output is correct |
37 |
Correct |
1273 ms |
23892 KB |
Output is correct |
38 |
Correct |
1303 ms |
22100 KB |
Output is correct |
39 |
Correct |
1537 ms |
23240 KB |
Output is correct |
40 |
Correct |
1566 ms |
23124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
860 KB |
Output is correct |
2 |
Correct |
15 ms |
936 KB |
Output is correct |
3 |
Correct |
15 ms |
932 KB |
Output is correct |
4 |
Correct |
22 ms |
860 KB |
Output is correct |
5 |
Correct |
15 ms |
860 KB |
Output is correct |
6 |
Correct |
15 ms |
936 KB |
Output is correct |
7 |
Correct |
15 ms |
860 KB |
Output is correct |
8 |
Correct |
15 ms |
928 KB |
Output is correct |
9 |
Correct |
15 ms |
860 KB |
Output is correct |
10 |
Correct |
15 ms |
856 KB |
Output is correct |
11 |
Runtime error |
4 ms |
2812 KB |
Execution killed with signal 6 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
860 KB |
Output is correct |
2 |
Correct |
15 ms |
936 KB |
Output is correct |
3 |
Correct |
15 ms |
932 KB |
Output is correct |
4 |
Correct |
22 ms |
860 KB |
Output is correct |
5 |
Correct |
15 ms |
860 KB |
Output is correct |
6 |
Correct |
15 ms |
936 KB |
Output is correct |
7 |
Correct |
15 ms |
860 KB |
Output is correct |
8 |
Correct |
15 ms |
928 KB |
Output is correct |
9 |
Correct |
15 ms |
860 KB |
Output is correct |
10 |
Correct |
15 ms |
856 KB |
Output is correct |
11 |
Correct |
526 ms |
4688 KB |
Output is correct |
12 |
Correct |
386 ms |
4568 KB |
Output is correct |
13 |
Correct |
264 ms |
3644 KB |
Output is correct |
14 |
Correct |
263 ms |
3920 KB |
Output is correct |
15 |
Correct |
167 ms |
4716 KB |
Output is correct |
16 |
Correct |
342 ms |
4036 KB |
Output is correct |
17 |
Correct |
351 ms |
4020 KB |
Output is correct |
18 |
Correct |
118 ms |
5788 KB |
Output is correct |
19 |
Correct |
141 ms |
2896 KB |
Output is correct |
20 |
Correct |
339 ms |
4436 KB |
Output is correct |
21 |
Correct |
1282 ms |
16788 KB |
Output is correct |
22 |
Correct |
1452 ms |
16976 KB |
Output is correct |
23 |
Correct |
1569 ms |
16100 KB |
Output is correct |
24 |
Correct |
1549 ms |
15956 KB |
Output is correct |
25 |
Correct |
1254 ms |
17812 KB |
Output is correct |
26 |
Correct |
1605 ms |
16288 KB |
Output is correct |
27 |
Correct |
1561 ms |
16208 KB |
Output is correct |
28 |
Correct |
1232 ms |
18832 KB |
Output is correct |
29 |
Correct |
1251 ms |
14932 KB |
Output is correct |
30 |
Correct |
1411 ms |
17052 KB |
Output is correct |
31 |
Correct |
1248 ms |
16804 KB |
Output is correct |
32 |
Correct |
1916 ms |
23888 KB |
Output is correct |
33 |
Correct |
1452 ms |
22972 KB |
Output is correct |
34 |
Correct |
1626 ms |
23124 KB |
Output is correct |
35 |
Correct |
1584 ms |
23360 KB |
Output is correct |
36 |
Correct |
1252 ms |
21968 KB |
Output is correct |
37 |
Correct |
1273 ms |
23892 KB |
Output is correct |
38 |
Correct |
1303 ms |
22100 KB |
Output is correct |
39 |
Correct |
1537 ms |
23240 KB |
Output is correct |
40 |
Correct |
1566 ms |
23124 KB |
Output is correct |
41 |
Runtime error |
4 ms |
2812 KB |
Execution killed with signal 6 |
42 |
Halted |
0 ms |
0 KB |
- |