#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "
const ll MAX_N = (1 << 20) + 10;
const ll MAX_RIGHT = 2000000; ll MAX_LEFT = 7;
int pow3[50];
int dp[MAX_RIGHT];
int price[MAX_N];
string queries[MAX_N];
int ans[MAX_N];
int n, q;
char first2[MAX_RIGHT];
int sum[MAX_RIGHT];
int rightPart[MAX_N];
void prec() {
for(ll i = 0; i < q; i ++) {
ll currMask = 0;
for(ll j = n - 1; j >= MAX_LEFT; j --) {
currMask *= 3;
currMask += (ll)(queries[i][j] - '0');
}
rightPart[i] = currMask;
}
ll triBits = n - MAX_LEFT;
for(ll i = 0; i < pow3[triBits]; i ++) {
bool flag = false;
for(ll j = triBits - 1; j >= 0; j --) {
if((i / pow3[j]) % 3 == 2) {
first2[i] = j;
flag = true;
break;
}
}
if(!flag) {
ll currMask = 0;
for(ll j = triBits - 1; j >= 0; j --) {
currMask += ((i / pow3[j]) % 3) * (1 << (j + MAX_LEFT));
}
sum[i] = currMask;
first2[i] = -1;
}
}
}
void getAns(ll mask) {
vector<ll> indices;
for(ll i = 0; i < q; i ++) {
for(ll j = 0; j < MAX_LEFT; j ++) {
if(mask & (1 << j)) {
if(queries[i][j] == '0') {
break;
}
} else {
if(queries[i][j] == '1') {
break;
}
}
if(j == MAX_LEFT - 1) {
indices.push_back(i);
}
}
}
ll triBits = n - MAX_LEFT;
for(ll i = 0; i < pow3[triBits]; i ++) {
if(first2[i] != -1) {
ll j = first2[i];
dp[i] = dp[i - pow3[j]] + dp[i - 2 * pow3[j]];
} else {
ll currMask = mask + sum[i];
dp[i] = price[currMask];
}
}
for(auto it : indices) {
ans[it] += dp[rightPart[it]];
}
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
pow3[0] = 1;
for(ll i = 1; i < 50; i ++) {
pow3[i] = pow3[i - 1] * 3ll;
}
cin >> n >> q;
chkmin(MAX_LEFT, n);
if(n < 20) {MAX_LEFT = 6;}
string in;
cin >> in;
for(ll i = 0; i < (1 << n); i ++) {
price[i] = in[i] - '0';
}
for(ll i = 0; i < q; i ++) {
cin >> queries[i];
reverse(queries[i].begin(), queries[i].end());
for(auto &it : queries[i]) {
if(it == '?') {
it = '2';
}
}
}
prec();
for(ll mask = 0; mask < (1 << MAX_LEFT); mask ++) {
getAns(mask);
}
for(ll i = 0; i < q; i ++) {
cout << ans[i] << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
33280 KB |
Output is correct |
2 |
Correct |
23 ms |
33280 KB |
Output is correct |
3 |
Correct |
24 ms |
33280 KB |
Output is correct |
4 |
Correct |
24 ms |
33280 KB |
Output is correct |
5 |
Correct |
23 ms |
33280 KB |
Output is correct |
6 |
Correct |
23 ms |
33280 KB |
Output is correct |
7 |
Correct |
24 ms |
33280 KB |
Output is correct |
8 |
Correct |
24 ms |
33280 KB |
Output is correct |
9 |
Correct |
23 ms |
33152 KB |
Output is correct |
10 |
Correct |
23 ms |
33280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
33280 KB |
Output is correct |
2 |
Correct |
23 ms |
33280 KB |
Output is correct |
3 |
Correct |
24 ms |
33280 KB |
Output is correct |
4 |
Correct |
24 ms |
33280 KB |
Output is correct |
5 |
Correct |
23 ms |
33280 KB |
Output is correct |
6 |
Correct |
23 ms |
33280 KB |
Output is correct |
7 |
Correct |
24 ms |
33280 KB |
Output is correct |
8 |
Correct |
24 ms |
33280 KB |
Output is correct |
9 |
Correct |
23 ms |
33152 KB |
Output is correct |
10 |
Correct |
23 ms |
33280 KB |
Output is correct |
11 |
Correct |
1197 ms |
53524 KB |
Output is correct |
12 |
Correct |
1236 ms |
52784 KB |
Output is correct |
13 |
Correct |
1180 ms |
45916 KB |
Output is correct |
14 |
Correct |
1274 ms |
44988 KB |
Output is correct |
15 |
Correct |
1382 ms |
45228 KB |
Output is correct |
16 |
Correct |
1341 ms |
45008 KB |
Output is correct |
17 |
Correct |
1335 ms |
44572 KB |
Output is correct |
18 |
Correct |
1663 ms |
57144 KB |
Output is correct |
19 |
Correct |
1085 ms |
43424 KB |
Output is correct |
20 |
Correct |
1277 ms |
53456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
33280 KB |
Output is correct |
2 |
Correct |
23 ms |
33280 KB |
Output is correct |
3 |
Correct |
24 ms |
33280 KB |
Output is correct |
4 |
Correct |
24 ms |
33280 KB |
Output is correct |
5 |
Correct |
23 ms |
33280 KB |
Output is correct |
6 |
Correct |
23 ms |
33280 KB |
Output is correct |
7 |
Correct |
24 ms |
33280 KB |
Output is correct |
8 |
Correct |
24 ms |
33280 KB |
Output is correct |
9 |
Correct |
23 ms |
33152 KB |
Output is correct |
10 |
Correct |
23 ms |
33280 KB |
Output is correct |
11 |
Correct |
1197 ms |
53524 KB |
Output is correct |
12 |
Correct |
1236 ms |
52784 KB |
Output is correct |
13 |
Correct |
1180 ms |
45916 KB |
Output is correct |
14 |
Correct |
1274 ms |
44988 KB |
Output is correct |
15 |
Correct |
1382 ms |
45228 KB |
Output is correct |
16 |
Correct |
1341 ms |
45008 KB |
Output is correct |
17 |
Correct |
1335 ms |
44572 KB |
Output is correct |
18 |
Correct |
1663 ms |
57144 KB |
Output is correct |
19 |
Correct |
1085 ms |
43424 KB |
Output is correct |
20 |
Correct |
1277 ms |
53456 KB |
Output is correct |
21 |
Correct |
1316 ms |
57212 KB |
Output is correct |
22 |
Correct |
1268 ms |
53320 KB |
Output is correct |
23 |
Correct |
1242 ms |
46292 KB |
Output is correct |
24 |
Correct |
1309 ms |
45060 KB |
Output is correct |
25 |
Correct |
1399 ms |
46644 KB |
Output is correct |
26 |
Correct |
1362 ms |
45544 KB |
Output is correct |
27 |
Correct |
1364 ms |
44788 KB |
Output is correct |
28 |
Correct |
1665 ms |
57624 KB |
Output is correct |
29 |
Correct |
1083 ms |
43528 KB |
Output is correct |
30 |
Correct |
1284 ms |
53512 KB |
Output is correct |
31 |
Correct |
1388 ms |
47444 KB |
Output is correct |
32 |
Correct |
1366 ms |
46192 KB |
Output is correct |
33 |
Correct |
1014 ms |
45208 KB |
Output is correct |
34 |
Correct |
1342 ms |
44660 KB |
Output is correct |
35 |
Correct |
1363 ms |
44832 KB |
Output is correct |
36 |
Correct |
461 ms |
49128 KB |
Output is correct |
37 |
Correct |
1175 ms |
53064 KB |
Output is correct |
38 |
Correct |
1103 ms |
43228 KB |
Output is correct |
39 |
Correct |
1261 ms |
44640 KB |
Output is correct |
40 |
Correct |
1314 ms |
45276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
33280 KB |
Output is correct |
2 |
Correct |
23 ms |
33280 KB |
Output is correct |
3 |
Correct |
24 ms |
33280 KB |
Output is correct |
4 |
Correct |
24 ms |
33280 KB |
Output is correct |
5 |
Correct |
23 ms |
33280 KB |
Output is correct |
6 |
Correct |
23 ms |
33280 KB |
Output is correct |
7 |
Correct |
24 ms |
33280 KB |
Output is correct |
8 |
Correct |
24 ms |
33280 KB |
Output is correct |
9 |
Correct |
23 ms |
33152 KB |
Output is correct |
10 |
Correct |
23 ms |
33280 KB |
Output is correct |
11 |
Correct |
549 ms |
50588 KB |
Output is correct |
12 |
Correct |
540 ms |
50584 KB |
Output is correct |
13 |
Correct |
541 ms |
49820 KB |
Output is correct |
14 |
Correct |
552 ms |
49692 KB |
Output is correct |
15 |
Correct |
554 ms |
50072 KB |
Output is correct |
16 |
Correct |
549 ms |
49688 KB |
Output is correct |
17 |
Correct |
546 ms |
49684 KB |
Output is correct |
18 |
Correct |
541 ms |
50828 KB |
Output is correct |
19 |
Correct |
524 ms |
49560 KB |
Output is correct |
20 |
Correct |
539 ms |
50588 KB |
Output is correct |
21 |
Correct |
548 ms |
49816 KB |
Output is correct |
22 |
Correct |
547 ms |
49812 KB |
Output is correct |
23 |
Correct |
516 ms |
49940 KB |
Output is correct |
24 |
Correct |
544 ms |
49688 KB |
Output is correct |
25 |
Correct |
549 ms |
49660 KB |
Output is correct |
26 |
Correct |
502 ms |
50428 KB |
Output is correct |
27 |
Correct |
536 ms |
50584 KB |
Output is correct |
28 |
Correct |
531 ms |
49504 KB |
Output is correct |
29 |
Correct |
539 ms |
49688 KB |
Output is correct |
30 |
Correct |
541 ms |
49688 KB |
Output is correct |
31 |
Correct |
520 ms |
49816 KB |
Output is correct |
32 |
Correct |
563 ms |
49668 KB |
Output is correct |
33 |
Correct |
548 ms |
49688 KB |
Output is correct |
34 |
Correct |
480 ms |
50456 KB |
Output is correct |
35 |
Correct |
558 ms |
49992 KB |
Output is correct |
36 |
Correct |
550 ms |
49816 KB |
Output is correct |
37 |
Correct |
541 ms |
49816 KB |
Output is correct |
38 |
Correct |
547 ms |
49816 KB |
Output is correct |
39 |
Correct |
553 ms |
49816 KB |
Output is correct |
40 |
Correct |
542 ms |
49944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
33280 KB |
Output is correct |
2 |
Correct |
23 ms |
33280 KB |
Output is correct |
3 |
Correct |
24 ms |
33280 KB |
Output is correct |
4 |
Correct |
24 ms |
33280 KB |
Output is correct |
5 |
Correct |
23 ms |
33280 KB |
Output is correct |
6 |
Correct |
23 ms |
33280 KB |
Output is correct |
7 |
Correct |
24 ms |
33280 KB |
Output is correct |
8 |
Correct |
24 ms |
33280 KB |
Output is correct |
9 |
Correct |
23 ms |
33152 KB |
Output is correct |
10 |
Correct |
23 ms |
33280 KB |
Output is correct |
11 |
Correct |
1197 ms |
53524 KB |
Output is correct |
12 |
Correct |
1236 ms |
52784 KB |
Output is correct |
13 |
Correct |
1180 ms |
45916 KB |
Output is correct |
14 |
Correct |
1274 ms |
44988 KB |
Output is correct |
15 |
Correct |
1382 ms |
45228 KB |
Output is correct |
16 |
Correct |
1341 ms |
45008 KB |
Output is correct |
17 |
Correct |
1335 ms |
44572 KB |
Output is correct |
18 |
Correct |
1663 ms |
57144 KB |
Output is correct |
19 |
Correct |
1085 ms |
43424 KB |
Output is correct |
20 |
Correct |
1277 ms |
53456 KB |
Output is correct |
21 |
Correct |
1316 ms |
57212 KB |
Output is correct |
22 |
Correct |
1268 ms |
53320 KB |
Output is correct |
23 |
Correct |
1242 ms |
46292 KB |
Output is correct |
24 |
Correct |
1309 ms |
45060 KB |
Output is correct |
25 |
Correct |
1399 ms |
46644 KB |
Output is correct |
26 |
Correct |
1362 ms |
45544 KB |
Output is correct |
27 |
Correct |
1364 ms |
44788 KB |
Output is correct |
28 |
Correct |
1665 ms |
57624 KB |
Output is correct |
29 |
Correct |
1083 ms |
43528 KB |
Output is correct |
30 |
Correct |
1284 ms |
53512 KB |
Output is correct |
31 |
Correct |
1388 ms |
47444 KB |
Output is correct |
32 |
Correct |
1366 ms |
46192 KB |
Output is correct |
33 |
Correct |
1014 ms |
45208 KB |
Output is correct |
34 |
Correct |
1342 ms |
44660 KB |
Output is correct |
35 |
Correct |
1363 ms |
44832 KB |
Output is correct |
36 |
Correct |
461 ms |
49128 KB |
Output is correct |
37 |
Correct |
1175 ms |
53064 KB |
Output is correct |
38 |
Correct |
1103 ms |
43228 KB |
Output is correct |
39 |
Correct |
1261 ms |
44640 KB |
Output is correct |
40 |
Correct |
1314 ms |
45276 KB |
Output is correct |
41 |
Correct |
549 ms |
50588 KB |
Output is correct |
42 |
Correct |
540 ms |
50584 KB |
Output is correct |
43 |
Correct |
541 ms |
49820 KB |
Output is correct |
44 |
Correct |
552 ms |
49692 KB |
Output is correct |
45 |
Correct |
554 ms |
50072 KB |
Output is correct |
46 |
Correct |
549 ms |
49688 KB |
Output is correct |
47 |
Correct |
546 ms |
49684 KB |
Output is correct |
48 |
Correct |
541 ms |
50828 KB |
Output is correct |
49 |
Correct |
524 ms |
49560 KB |
Output is correct |
50 |
Correct |
539 ms |
50588 KB |
Output is correct |
51 |
Correct |
548 ms |
49816 KB |
Output is correct |
52 |
Correct |
547 ms |
49812 KB |
Output is correct |
53 |
Correct |
516 ms |
49940 KB |
Output is correct |
54 |
Correct |
544 ms |
49688 KB |
Output is correct |
55 |
Correct |
549 ms |
49660 KB |
Output is correct |
56 |
Correct |
502 ms |
50428 KB |
Output is correct |
57 |
Correct |
536 ms |
50584 KB |
Output is correct |
58 |
Correct |
531 ms |
49504 KB |
Output is correct |
59 |
Correct |
539 ms |
49688 KB |
Output is correct |
60 |
Correct |
541 ms |
49688 KB |
Output is correct |
61 |
Correct |
520 ms |
49816 KB |
Output is correct |
62 |
Correct |
563 ms |
49668 KB |
Output is correct |
63 |
Correct |
548 ms |
49688 KB |
Output is correct |
64 |
Correct |
480 ms |
50456 KB |
Output is correct |
65 |
Correct |
558 ms |
49992 KB |
Output is correct |
66 |
Correct |
550 ms |
49816 KB |
Output is correct |
67 |
Correct |
541 ms |
49816 KB |
Output is correct |
68 |
Correct |
547 ms |
49816 KB |
Output is correct |
69 |
Correct |
553 ms |
49816 KB |
Output is correct |
70 |
Correct |
542 ms |
49944 KB |
Output is correct |
71 |
Runtime error |
215 ms |
65540 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
72 |
Halted |
0 ms |
0 KB |
- |