#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 = 7;
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 |
10 ms |
860 KB |
Output is correct |
2 |
Correct |
10 ms |
860 KB |
Output is correct |
3 |
Correct |
9 ms |
928 KB |
Output is correct |
4 |
Correct |
10 ms |
860 KB |
Output is correct |
5 |
Correct |
10 ms |
860 KB |
Output is correct |
6 |
Correct |
9 ms |
856 KB |
Output is correct |
7 |
Correct |
10 ms |
928 KB |
Output is correct |
8 |
Correct |
9 ms |
860 KB |
Output is correct |
9 |
Correct |
9 ms |
860 KB |
Output is correct |
10 |
Correct |
10 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
860 KB |
Output is correct |
2 |
Correct |
10 ms |
860 KB |
Output is correct |
3 |
Correct |
9 ms |
928 KB |
Output is correct |
4 |
Correct |
10 ms |
860 KB |
Output is correct |
5 |
Correct |
10 ms |
860 KB |
Output is correct |
6 |
Correct |
9 ms |
856 KB |
Output is correct |
7 |
Correct |
10 ms |
928 KB |
Output is correct |
8 |
Correct |
9 ms |
860 KB |
Output is correct |
9 |
Correct |
9 ms |
860 KB |
Output is correct |
10 |
Correct |
10 ms |
860 KB |
Output is correct |
11 |
Correct |
510 ms |
4692 KB |
Output is correct |
12 |
Correct |
515 ms |
4432 KB |
Output is correct |
13 |
Correct |
257 ms |
3668 KB |
Output is correct |
14 |
Correct |
260 ms |
3920 KB |
Output is correct |
15 |
Correct |
822 ms |
4704 KB |
Output is correct |
16 |
Correct |
430 ms |
4256 KB |
Output is correct |
17 |
Correct |
372 ms |
3924 KB |
Output is correct |
18 |
Correct |
104 ms |
5712 KB |
Output is correct |
19 |
Correct |
135 ms |
2896 KB |
Output is correct |
20 |
Correct |
523 ms |
4496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
860 KB |
Output is correct |
2 |
Correct |
10 ms |
860 KB |
Output is correct |
3 |
Correct |
9 ms |
928 KB |
Output is correct |
4 |
Correct |
10 ms |
860 KB |
Output is correct |
5 |
Correct |
10 ms |
860 KB |
Output is correct |
6 |
Correct |
9 ms |
856 KB |
Output is correct |
7 |
Correct |
10 ms |
928 KB |
Output is correct |
8 |
Correct |
9 ms |
860 KB |
Output is correct |
9 |
Correct |
9 ms |
860 KB |
Output is correct |
10 |
Correct |
10 ms |
860 KB |
Output is correct |
11 |
Correct |
510 ms |
4692 KB |
Output is correct |
12 |
Correct |
515 ms |
4432 KB |
Output is correct |
13 |
Correct |
257 ms |
3668 KB |
Output is correct |
14 |
Correct |
260 ms |
3920 KB |
Output is correct |
15 |
Correct |
822 ms |
4704 KB |
Output is correct |
16 |
Correct |
430 ms |
4256 KB |
Output is correct |
17 |
Correct |
372 ms |
3924 KB |
Output is correct |
18 |
Correct |
104 ms |
5712 KB |
Output is correct |
19 |
Correct |
135 ms |
2896 KB |
Output is correct |
20 |
Correct |
523 ms |
4496 KB |
Output is correct |
21 |
Correct |
1316 ms |
16720 KB |
Output is correct |
22 |
Correct |
1790 ms |
24264 KB |
Output is correct |
23 |
Correct |
1662 ms |
23232 KB |
Output is correct |
24 |
Correct |
1634 ms |
22984 KB |
Output is correct |
25 |
Correct |
1331 ms |
25032 KB |
Output is correct |
26 |
Correct |
1815 ms |
23632 KB |
Output is correct |
27 |
Correct |
1975 ms |
23376 KB |
Output is correct |
28 |
Correct |
1286 ms |
25940 KB |
Output is correct |
29 |
Correct |
1321 ms |
22096 KB |
Output is correct |
30 |
Correct |
1684 ms |
24260 KB |
Output is correct |
31 |
Execution timed out |
2037 ms |
22980 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
860 KB |
Output is correct |
2 |
Correct |
10 ms |
860 KB |
Output is correct |
3 |
Correct |
9 ms |
928 KB |
Output is correct |
4 |
Correct |
10 ms |
860 KB |
Output is correct |
5 |
Correct |
10 ms |
860 KB |
Output is correct |
6 |
Correct |
9 ms |
856 KB |
Output is correct |
7 |
Correct |
10 ms |
928 KB |
Output is correct |
8 |
Correct |
9 ms |
860 KB |
Output is correct |
9 |
Correct |
9 ms |
860 KB |
Output is correct |
10 |
Correct |
10 ms |
860 KB |
Output is correct |
11 |
Runtime error |
4 ms |
2816 KB |
Execution killed with signal 6 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
860 KB |
Output is correct |
2 |
Correct |
10 ms |
860 KB |
Output is correct |
3 |
Correct |
9 ms |
928 KB |
Output is correct |
4 |
Correct |
10 ms |
860 KB |
Output is correct |
5 |
Correct |
10 ms |
860 KB |
Output is correct |
6 |
Correct |
9 ms |
856 KB |
Output is correct |
7 |
Correct |
10 ms |
928 KB |
Output is correct |
8 |
Correct |
9 ms |
860 KB |
Output is correct |
9 |
Correct |
9 ms |
860 KB |
Output is correct |
10 |
Correct |
10 ms |
860 KB |
Output is correct |
11 |
Correct |
510 ms |
4692 KB |
Output is correct |
12 |
Correct |
515 ms |
4432 KB |
Output is correct |
13 |
Correct |
257 ms |
3668 KB |
Output is correct |
14 |
Correct |
260 ms |
3920 KB |
Output is correct |
15 |
Correct |
822 ms |
4704 KB |
Output is correct |
16 |
Correct |
430 ms |
4256 KB |
Output is correct |
17 |
Correct |
372 ms |
3924 KB |
Output is correct |
18 |
Correct |
104 ms |
5712 KB |
Output is correct |
19 |
Correct |
135 ms |
2896 KB |
Output is correct |
20 |
Correct |
523 ms |
4496 KB |
Output is correct |
21 |
Correct |
1316 ms |
16720 KB |
Output is correct |
22 |
Correct |
1790 ms |
24264 KB |
Output is correct |
23 |
Correct |
1662 ms |
23232 KB |
Output is correct |
24 |
Correct |
1634 ms |
22984 KB |
Output is correct |
25 |
Correct |
1331 ms |
25032 KB |
Output is correct |
26 |
Correct |
1815 ms |
23632 KB |
Output is correct |
27 |
Correct |
1975 ms |
23376 KB |
Output is correct |
28 |
Correct |
1286 ms |
25940 KB |
Output is correct |
29 |
Correct |
1321 ms |
22096 KB |
Output is correct |
30 |
Correct |
1684 ms |
24260 KB |
Output is correct |
31 |
Execution timed out |
2037 ms |
22980 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |