/*
ЗАПУСКАЕМ
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░
*/
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
using namespace std;
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const pair<T, U> &_p) { _out << _p.first << ' ' << _p.second; return _out; }
template<typename T, typename U> inline istream &operator>> (istream &_in, pair<T, U> &_p) { _in >> _p.first >> _p.second; return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const vector<T> &_v) { if (_v.empty()) { return _out; } _out << _v.front(); for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline istream &operator>> (istream &_in, vector<T> &_v) { for (auto &_i : _v) { _in >> _i; } return _in; }
template<typename T> inline ostream &operator<< (ostream &_out, const set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T> inline ostream &operator<< (ostream &_out, const unordered_multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
template<typename T, typename U> inline ostream &operator<< (ostream &_out, const unordered_map<T, U> &_m) { if (_m.empty()) { return _out; } _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) { _out << ", (" << _it->first << ": " << _it->second << ')'; } return _out; }
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left227
#define right right227
#define next next227
#define rank rank227
#define prev prev227
#define y1 y1227
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
#define pb push_back
const string FILENAME = "input";
int l, q;
string s;
char c[1 << 20];
int ans[1594329];
int who[1000227];
int dp[1594329];
int pos[1594329];
long long g[30];
bool bad[1594329];
int prefs[1594329];
int kt[1594329];
long long get(string s) {
int it = 0;
long long res = 0;
for (auto x: s) {
if (x == '?') {
res += g[it] * 2;
} else {
res += g[it] * (x - '0');
}
it++;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//read(FILENAME);
cin >> l >> q;
cin >> s;
for (int i = 0; i < (1 << l); i++) {
c[i] = s[i] - '0';
}
int t = 1;
for (int i = 0; i < l - 7; i++) {
t *= 3;
}
g[0] = 1;
for (int i = 1; i <= 20; i++) {
g[i] = g[i - 1] * 3;
}
if (l <= 7) {
t = 1;
for (int i = 0; i < l; i++) {
t *= 3;
}
for (int mask = 0; mask < t; mask++) {
int id = mask;
bool was = false;
int cur = 0;
int r = 0;
int cur1 = 0;
int g = 1;
for (int t = 0; t < l; t++) {
int s = id % 3;
if (was) {
cur += s * g;
cur1 += s * g;
} else {
if (s == 2) {
was = true;
cur += 0 * g;
cur1 += 1 * g;
} else {
cur += s * g;
cur1 += s * g;
if (s) {
r += (1 << (l - t - 1));
}
}
}
id /= 3;
g *= 3;
}
if (!was) {
ans[mask] = c[r];
} else {
ans[mask] = ans[cur] + ans[cur1];
}
}
for (int it = 0; it < q; it++) {
string k;
cin >> k;
int x = get(k);
// cout << "opa" << endl;
cout << ans[x] << '\n';
}
return 0;
}
for (int it = 0; it < q; it++) {
string f;
cin >> f;
long long x;
x = get(f);
vector<char> st;
for (int i = 0; i < l; i++) {
st.pb(x % 3);
// cout << (int)st.back() << '\n';
x /= 3;
}
reverse(all(st));
int cur = 1;
for (int i = 7; i < l; i++) {
who[it] += (int)st[i] * cur;
cur *= 3;
}
for (int i = 0; i < 7; i++) {
prefs[it] += (int)st[i] * g[i];
}
}
for (int i = 0; i < l - 7; i++) {
for (int pre = 0; pre < (1 << i); pre++) {
int rpre = 0;
for (int k = 0; k < i; k++) {
if (pre & (1 << k)) {
rpre += g[k];
}
}
for (int suff = 0; suff < g[l - 7 - i - 1]; suff++) {
pos[rpre + 2 * g[i] + suff * g[i + 1]] = i + 1;
}
}
}
for (int i = 0; i < t; i++) {
if (pos[i] == 0) {
int curi = i;
int nmask = 0;
for (int j = 0; j < l - 7; j++) {
int g = curi % 3;
if (g) {
nmask += 1 << j;
}
curi /= 3;
}
kt[i] = nmask;
}
}
for (int mask = 0; mask < 128; mask++) {
for (int i = 0; i < t; i++) {
if (pos[i] == 0) {
dp[i] = c[(kt[i] << 7) + mask];
} else {
int ft = g[pos[i] - 1];
dp[i] = dp[i - ft] + dp[i - (ft << 1)];
}
}
for (int j = 0; j < g[7]; j++) {
bad[j] = false;
int cur = j;
for (int f = 0; f < 7; f++) {
int a = cur % 3;
if (a != 2) {
bool fs = ((mask & (1 << f)) != 0);
if (fs != a) {
bad[j] = true;
break;
}
}
cur /= 3;
}
}
for (int it = 0; it < q; it++) {
if (!bad[prefs[it]]) {
ans[it] += dp[who[it]];
}
}
}
for (int it = 0; it < q; it++) {
cout << ans[it] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
10 ms |
384 KB |
Output is correct |
8 |
Correct |
7 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
10 ms |
384 KB |
Output is correct |
8 |
Correct |
7 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
11 |
Correct |
677 ms |
16304 KB |
Output is correct |
12 |
Correct |
745 ms |
15884 KB |
Output is correct |
13 |
Correct |
578 ms |
15096 KB |
Output is correct |
14 |
Correct |
592 ms |
15184 KB |
Output is correct |
15 |
Correct |
821 ms |
16104 KB |
Output is correct |
16 |
Correct |
630 ms |
15428 KB |
Output is correct |
17 |
Correct |
698 ms |
15224 KB |
Output is correct |
18 |
Correct |
532 ms |
17144 KB |
Output is correct |
19 |
Correct |
589 ms |
14072 KB |
Output is correct |
20 |
Correct |
748 ms |
15864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
10 ms |
384 KB |
Output is correct |
8 |
Correct |
7 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
11 |
Correct |
677 ms |
16304 KB |
Output is correct |
12 |
Correct |
745 ms |
15884 KB |
Output is correct |
13 |
Correct |
578 ms |
15096 KB |
Output is correct |
14 |
Correct |
592 ms |
15184 KB |
Output is correct |
15 |
Correct |
821 ms |
16104 KB |
Output is correct |
16 |
Correct |
630 ms |
15428 KB |
Output is correct |
17 |
Correct |
698 ms |
15224 KB |
Output is correct |
18 |
Correct |
532 ms |
17144 KB |
Output is correct |
19 |
Correct |
589 ms |
14072 KB |
Output is correct |
20 |
Correct |
748 ms |
15864 KB |
Output is correct |
21 |
Correct |
786 ms |
16096 KB |
Output is correct |
22 |
Correct |
769 ms |
16264 KB |
Output is correct |
23 |
Correct |
675 ms |
15352 KB |
Output is correct |
24 |
Correct |
714 ms |
15228 KB |
Output is correct |
25 |
Correct |
1101 ms |
17296 KB |
Output is correct |
26 |
Correct |
769 ms |
15764 KB |
Output is correct |
27 |
Correct |
754 ms |
15736 KB |
Output is correct |
28 |
Correct |
567 ms |
18168 KB |
Output is correct |
29 |
Correct |
599 ms |
14200 KB |
Output is correct |
30 |
Correct |
848 ms |
16232 KB |
Output is correct |
31 |
Correct |
830 ms |
16240 KB |
Output is correct |
32 |
Correct |
680 ms |
16216 KB |
Output is correct |
33 |
Correct |
615 ms |
15096 KB |
Output is correct |
34 |
Correct |
681 ms |
15428 KB |
Output is correct |
35 |
Correct |
680 ms |
15608 KB |
Output is correct |
36 |
Correct |
469 ms |
14328 KB |
Output is correct |
37 |
Correct |
740 ms |
16220 KB |
Output is correct |
38 |
Correct |
556 ms |
14328 KB |
Output is correct |
39 |
Correct |
665 ms |
15352 KB |
Output is correct |
40 |
Correct |
625 ms |
15096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
10 ms |
384 KB |
Output is correct |
8 |
Correct |
7 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
11 |
Correct |
564 ms |
16408 KB |
Output is correct |
12 |
Correct |
514 ms |
16408 KB |
Output is correct |
13 |
Correct |
498 ms |
16408 KB |
Output is correct |
14 |
Correct |
497 ms |
16404 KB |
Output is correct |
15 |
Correct |
515 ms |
16536 KB |
Output is correct |
16 |
Correct |
499 ms |
16408 KB |
Output is correct |
17 |
Correct |
562 ms |
16476 KB |
Output is correct |
18 |
Correct |
492 ms |
16664 KB |
Output is correct |
19 |
Correct |
498 ms |
16356 KB |
Output is correct |
20 |
Correct |
496 ms |
16412 KB |
Output is correct |
21 |
Correct |
505 ms |
16536 KB |
Output is correct |
22 |
Correct |
513 ms |
16436 KB |
Output is correct |
23 |
Correct |
530 ms |
16408 KB |
Output is correct |
24 |
Correct |
509 ms |
16444 KB |
Output is correct |
25 |
Correct |
599 ms |
16408 KB |
Output is correct |
26 |
Correct |
534 ms |
16280 KB |
Output is correct |
27 |
Correct |
579 ms |
16380 KB |
Output is correct |
28 |
Correct |
597 ms |
16276 KB |
Output is correct |
29 |
Correct |
570 ms |
16412 KB |
Output is correct |
30 |
Correct |
612 ms |
16408 KB |
Output is correct |
31 |
Correct |
551 ms |
16280 KB |
Output is correct |
32 |
Correct |
526 ms |
16408 KB |
Output is correct |
33 |
Correct |
550 ms |
16408 KB |
Output is correct |
34 |
Correct |
479 ms |
16280 KB |
Output is correct |
35 |
Correct |
505 ms |
16408 KB |
Output is correct |
36 |
Correct |
514 ms |
16376 KB |
Output is correct |
37 |
Correct |
539 ms |
16408 KB |
Output is correct |
38 |
Correct |
519 ms |
16408 KB |
Output is correct |
39 |
Correct |
494 ms |
16408 KB |
Output is correct |
40 |
Correct |
503 ms |
16408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
10 ms |
384 KB |
Output is correct |
8 |
Correct |
7 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
11 |
Correct |
677 ms |
16304 KB |
Output is correct |
12 |
Correct |
745 ms |
15884 KB |
Output is correct |
13 |
Correct |
578 ms |
15096 KB |
Output is correct |
14 |
Correct |
592 ms |
15184 KB |
Output is correct |
15 |
Correct |
821 ms |
16104 KB |
Output is correct |
16 |
Correct |
630 ms |
15428 KB |
Output is correct |
17 |
Correct |
698 ms |
15224 KB |
Output is correct |
18 |
Correct |
532 ms |
17144 KB |
Output is correct |
19 |
Correct |
589 ms |
14072 KB |
Output is correct |
20 |
Correct |
748 ms |
15864 KB |
Output is correct |
21 |
Correct |
786 ms |
16096 KB |
Output is correct |
22 |
Correct |
769 ms |
16264 KB |
Output is correct |
23 |
Correct |
675 ms |
15352 KB |
Output is correct |
24 |
Correct |
714 ms |
15228 KB |
Output is correct |
25 |
Correct |
1101 ms |
17296 KB |
Output is correct |
26 |
Correct |
769 ms |
15764 KB |
Output is correct |
27 |
Correct |
754 ms |
15736 KB |
Output is correct |
28 |
Correct |
567 ms |
18168 KB |
Output is correct |
29 |
Correct |
599 ms |
14200 KB |
Output is correct |
30 |
Correct |
848 ms |
16232 KB |
Output is correct |
31 |
Correct |
830 ms |
16240 KB |
Output is correct |
32 |
Correct |
680 ms |
16216 KB |
Output is correct |
33 |
Correct |
615 ms |
15096 KB |
Output is correct |
34 |
Correct |
681 ms |
15428 KB |
Output is correct |
35 |
Correct |
680 ms |
15608 KB |
Output is correct |
36 |
Correct |
469 ms |
14328 KB |
Output is correct |
37 |
Correct |
740 ms |
16220 KB |
Output is correct |
38 |
Correct |
556 ms |
14328 KB |
Output is correct |
39 |
Correct |
665 ms |
15352 KB |
Output is correct |
40 |
Correct |
625 ms |
15096 KB |
Output is correct |
41 |
Correct |
564 ms |
16408 KB |
Output is correct |
42 |
Correct |
514 ms |
16408 KB |
Output is correct |
43 |
Correct |
498 ms |
16408 KB |
Output is correct |
44 |
Correct |
497 ms |
16404 KB |
Output is correct |
45 |
Correct |
515 ms |
16536 KB |
Output is correct |
46 |
Correct |
499 ms |
16408 KB |
Output is correct |
47 |
Correct |
562 ms |
16476 KB |
Output is correct |
48 |
Correct |
492 ms |
16664 KB |
Output is correct |
49 |
Correct |
498 ms |
16356 KB |
Output is correct |
50 |
Correct |
496 ms |
16412 KB |
Output is correct |
51 |
Correct |
505 ms |
16536 KB |
Output is correct |
52 |
Correct |
513 ms |
16436 KB |
Output is correct |
53 |
Correct |
530 ms |
16408 KB |
Output is correct |
54 |
Correct |
509 ms |
16444 KB |
Output is correct |
55 |
Correct |
599 ms |
16408 KB |
Output is correct |
56 |
Correct |
534 ms |
16280 KB |
Output is correct |
57 |
Correct |
579 ms |
16380 KB |
Output is correct |
58 |
Correct |
597 ms |
16276 KB |
Output is correct |
59 |
Correct |
570 ms |
16412 KB |
Output is correct |
60 |
Correct |
612 ms |
16408 KB |
Output is correct |
61 |
Correct |
551 ms |
16280 KB |
Output is correct |
62 |
Correct |
526 ms |
16408 KB |
Output is correct |
63 |
Correct |
550 ms |
16408 KB |
Output is correct |
64 |
Correct |
479 ms |
16280 KB |
Output is correct |
65 |
Correct |
505 ms |
16408 KB |
Output is correct |
66 |
Correct |
514 ms |
16376 KB |
Output is correct |
67 |
Correct |
539 ms |
16408 KB |
Output is correct |
68 |
Correct |
519 ms |
16408 KB |
Output is correct |
69 |
Correct |
494 ms |
16408 KB |
Output is correct |
70 |
Correct |
503 ms |
16408 KB |
Output is correct |
71 |
Correct |
1487 ms |
32336 KB |
Output is correct |
72 |
Correct |
1647 ms |
32408 KB |
Output is correct |
73 |
Correct |
1557 ms |
31060 KB |
Output is correct |
74 |
Correct |
1927 ms |
31256 KB |
Output is correct |
75 |
Execution timed out |
2031 ms |
27416 KB |
Time limit exceeded |
76 |
Halted |
0 ms |
0 KB |
- |