#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2")
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <bitset>
#include <cstring>
#include <unordered_map>
using namespace std;
typedef long long ll;
ll const half = 7;
#define endl '\n'
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll l, q;
cin >> l >> q;
string a;
cin >> a;
vector<ll> pw(l + 1);
pw[0] = 1;
for(int i = 1; i < pw.size(); i++)
pw[i] = pw[i - 1] * 3;
vector<int> dp, bit(pw[max(0ll, l - half)] + 1, -1), bin_to3((1ll << max(0ll, l - half)) + 1), from3_to_bin(pw[max(0ll, l - half)] + 1);
for(int i = 0; i < (1 << max(0ll, l - half)); i++){
ll cnt = 0;
for(int j = max(0ll, l - half) - 1; j >= 0; j--){
if((i >> j) & 1){
cnt = (cnt + 1) * 3;
}
else
cnt *= 3;
}
cnt /= 3;
bin_to3[i] = cnt;
from3_to_bin[cnt] = i;
}
for(int i = 0; i < pw[(max(0ll, l - half))]; i++){
ll val = i;
for(int j = 0; j < max(0ll, l - half); j++){
if(val % 3 == 2){
bit[i] = j;
break;
}
val /= 3;
}
}
vector<vector<bool>> sub(pw[half], vector<bool> ((1ll << half) + 1, true));
for(int i = 0; i < pw[half]; i++){
vector<ll> u;
ll cur = pw[half];
ll val = i;
for(int i = half - 1; i >= 0; i--){
cur /= 3;
u.push_back(val / cur);
val %= cur;
}
for(int j = 0; j < (1ll << half); j++){
for(int k = 0; k < half; k++){
if(u[u.size() - k - 1] == 2)
continue;
if(u[u.size() - k - 1] != (j >> k) % 2){
sub[i][j] = false;
break;
}
}
}
}
vector<pair<ll, ll>> quer;
while(q--){
string b;
cin >> b;
while(b.size() < l)
b += "0";
ll cnt = 0, pos = 0, c1 = 0;
for(auto i: b){
if(i == '0'){
cnt *= 3;
}
else if(i == '1'){
cnt = (cnt + 1) * 3;
}
else{
cnt = (cnt + 2) * 3;
}
pos++;
if(pos == half || (half > b.size() && pos == b.size()) ){
c1 = cnt / 3;
}
}
cnt /= 3;
if(l > half){
cnt %= pw[l - half];
}
quer.push_back({c1, cnt});
}
vector<ll> ans(quer.size());
dp.resize(pw[max(0ll, l - half)] + 1);
for(int i = 0; i < (1ll << min(half, l)); i++){
if(l <= half){
for(int j = 0; j < quer.size(); j++){
if(sub[quer[j].first][i])
ans[j] += (a[i] - '0');
}
continue;
}
ll add = i * (1 << (l - half));
for(int j = 0; j < pw[l - half]; j++){
if(bit[j] == -1){
// cout << j << ' ' << from3_to_bin[j] << ' ' << add << endl;
dp[j] = (a[add + from3_to_bin[j]] - '0');
continue;
}
dp[j] = 0;
ll c1 = j - 2 * pw[bit[j]];
dp[j] += dp[c1];
c1 = j - pw[bit[j]];
dp[j] += dp[c1];
}
//
// cout << i << endl;
// for(int j = 0; j < pw[l - half]; j++)
// cout << dp[j] << ' ';
// cout << endl;
//
for(int j = 0; j < quer.size(); j++){
if(sub[quer[j].first][i]){
ans[j] += dp[quer[j].second];
}
}
}
for(auto i: ans)
cout << i << endl;
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:44:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int i = 1; i < pw.size(); i++)
| ~~^~~~~~~~~~~
snake_escaping.cpp:111:24: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
111 | while(b.size() < l)
| ~~~~~~~~~^~~
snake_escaping.cpp:128:55: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
128 | if(pos == half || (half > b.size() && pos == b.size()) ){
| ~~~~^~~~~~~~~~~
snake_escaping.cpp:147:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
147 | for(int j = 0; j < quer.size(); j++){
| ~~^~~~~~~~~~~~~
snake_escaping.cpp:175:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
175 | for(int j = 0; j < quer.size(); j++){
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
495 ms |
27936 KB |
Output is correct |
12 |
Correct |
584 ms |
27676 KB |
Output is correct |
13 |
Correct |
558 ms |
26812 KB |
Output is correct |
14 |
Correct |
623 ms |
26904 KB |
Output is correct |
15 |
Correct |
599 ms |
28072 KB |
Output is correct |
16 |
Correct |
610 ms |
27172 KB |
Output is correct |
17 |
Correct |
643 ms |
27156 KB |
Output is correct |
18 |
Correct |
372 ms |
29288 KB |
Output is correct |
19 |
Correct |
438 ms |
26028 KB |
Output is correct |
20 |
Correct |
565 ms |
27884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
495 ms |
27936 KB |
Output is correct |
12 |
Correct |
584 ms |
27676 KB |
Output is correct |
13 |
Correct |
558 ms |
26812 KB |
Output is correct |
14 |
Correct |
623 ms |
26904 KB |
Output is correct |
15 |
Correct |
599 ms |
28072 KB |
Output is correct |
16 |
Correct |
610 ms |
27172 KB |
Output is correct |
17 |
Correct |
643 ms |
27156 KB |
Output is correct |
18 |
Correct |
372 ms |
29288 KB |
Output is correct |
19 |
Correct |
438 ms |
26028 KB |
Output is correct |
20 |
Correct |
565 ms |
27884 KB |
Output is correct |
21 |
Correct |
577 ms |
27948 KB |
Output is correct |
22 |
Correct |
587 ms |
28060 KB |
Output is correct |
23 |
Correct |
643 ms |
27328 KB |
Output is correct |
24 |
Correct |
648 ms |
27300 KB |
Output is correct |
25 |
Correct |
646 ms |
29128 KB |
Output is correct |
26 |
Correct |
633 ms |
27504 KB |
Output is correct |
27 |
Correct |
673 ms |
27556 KB |
Output is correct |
28 |
Correct |
372 ms |
30580 KB |
Output is correct |
29 |
Correct |
468 ms |
26452 KB |
Output is correct |
30 |
Correct |
593 ms |
28904 KB |
Output is correct |
31 |
Correct |
640 ms |
27948 KB |
Output is correct |
32 |
Correct |
639 ms |
28200 KB |
Output is correct |
33 |
Correct |
523 ms |
26788 KB |
Output is correct |
34 |
Correct |
710 ms |
26948 KB |
Output is correct |
35 |
Correct |
683 ms |
27696 KB |
Output is correct |
36 |
Correct |
336 ms |
25892 KB |
Output is correct |
37 |
Correct |
529 ms |
28076 KB |
Output is correct |
38 |
Correct |
478 ms |
25964 KB |
Output is correct |
39 |
Correct |
599 ms |
27288 KB |
Output is correct |
40 |
Correct |
663 ms |
26936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
497 ms |
22196 KB |
Output is correct |
12 |
Correct |
439 ms |
22456 KB |
Output is correct |
13 |
Correct |
472 ms |
22204 KB |
Output is correct |
14 |
Correct |
450 ms |
22196 KB |
Output is correct |
15 |
Correct |
452 ms |
22460 KB |
Output is correct |
16 |
Correct |
473 ms |
22188 KB |
Output is correct |
17 |
Correct |
426 ms |
22208 KB |
Output is correct |
18 |
Correct |
432 ms |
22456 KB |
Output is correct |
19 |
Correct |
525 ms |
22200 KB |
Output is correct |
20 |
Correct |
420 ms |
22452 KB |
Output is correct |
21 |
Correct |
466 ms |
22260 KB |
Output is correct |
22 |
Correct |
461 ms |
22256 KB |
Output is correct |
23 |
Correct |
429 ms |
22212 KB |
Output is correct |
24 |
Correct |
452 ms |
22196 KB |
Output is correct |
25 |
Correct |
486 ms |
22204 KB |
Output is correct |
26 |
Correct |
422 ms |
22256 KB |
Output is correct |
27 |
Correct |
450 ms |
22292 KB |
Output is correct |
28 |
Correct |
461 ms |
22204 KB |
Output is correct |
29 |
Correct |
546 ms |
22196 KB |
Output is correct |
30 |
Correct |
464 ms |
22196 KB |
Output is correct |
31 |
Correct |
459 ms |
22204 KB |
Output is correct |
32 |
Correct |
466 ms |
22256 KB |
Output is correct |
33 |
Correct |
433 ms |
22208 KB |
Output is correct |
34 |
Correct |
413 ms |
22260 KB |
Output is correct |
35 |
Correct |
449 ms |
22200 KB |
Output is correct |
36 |
Correct |
423 ms |
22200 KB |
Output is correct |
37 |
Correct |
477 ms |
22208 KB |
Output is correct |
38 |
Correct |
436 ms |
22208 KB |
Output is correct |
39 |
Correct |
427 ms |
22200 KB |
Output is correct |
40 |
Correct |
458 ms |
22204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
495 ms |
27936 KB |
Output is correct |
12 |
Correct |
584 ms |
27676 KB |
Output is correct |
13 |
Correct |
558 ms |
26812 KB |
Output is correct |
14 |
Correct |
623 ms |
26904 KB |
Output is correct |
15 |
Correct |
599 ms |
28072 KB |
Output is correct |
16 |
Correct |
610 ms |
27172 KB |
Output is correct |
17 |
Correct |
643 ms |
27156 KB |
Output is correct |
18 |
Correct |
372 ms |
29288 KB |
Output is correct |
19 |
Correct |
438 ms |
26028 KB |
Output is correct |
20 |
Correct |
565 ms |
27884 KB |
Output is correct |
21 |
Correct |
577 ms |
27948 KB |
Output is correct |
22 |
Correct |
587 ms |
28060 KB |
Output is correct |
23 |
Correct |
643 ms |
27328 KB |
Output is correct |
24 |
Correct |
648 ms |
27300 KB |
Output is correct |
25 |
Correct |
646 ms |
29128 KB |
Output is correct |
26 |
Correct |
633 ms |
27504 KB |
Output is correct |
27 |
Correct |
673 ms |
27556 KB |
Output is correct |
28 |
Correct |
372 ms |
30580 KB |
Output is correct |
29 |
Correct |
468 ms |
26452 KB |
Output is correct |
30 |
Correct |
593 ms |
28904 KB |
Output is correct |
31 |
Correct |
640 ms |
27948 KB |
Output is correct |
32 |
Correct |
639 ms |
28200 KB |
Output is correct |
33 |
Correct |
523 ms |
26788 KB |
Output is correct |
34 |
Correct |
710 ms |
26948 KB |
Output is correct |
35 |
Correct |
683 ms |
27696 KB |
Output is correct |
36 |
Correct |
336 ms |
25892 KB |
Output is correct |
37 |
Correct |
529 ms |
28076 KB |
Output is correct |
38 |
Correct |
478 ms |
25964 KB |
Output is correct |
39 |
Correct |
599 ms |
27288 KB |
Output is correct |
40 |
Correct |
663 ms |
26936 KB |
Output is correct |
41 |
Correct |
497 ms |
22196 KB |
Output is correct |
42 |
Correct |
439 ms |
22456 KB |
Output is correct |
43 |
Correct |
472 ms |
22204 KB |
Output is correct |
44 |
Correct |
450 ms |
22196 KB |
Output is correct |
45 |
Correct |
452 ms |
22460 KB |
Output is correct |
46 |
Correct |
473 ms |
22188 KB |
Output is correct |
47 |
Correct |
426 ms |
22208 KB |
Output is correct |
48 |
Correct |
432 ms |
22456 KB |
Output is correct |
49 |
Correct |
525 ms |
22200 KB |
Output is correct |
50 |
Correct |
420 ms |
22452 KB |
Output is correct |
51 |
Correct |
466 ms |
22260 KB |
Output is correct |
52 |
Correct |
461 ms |
22256 KB |
Output is correct |
53 |
Correct |
429 ms |
22212 KB |
Output is correct |
54 |
Correct |
452 ms |
22196 KB |
Output is correct |
55 |
Correct |
486 ms |
22204 KB |
Output is correct |
56 |
Correct |
422 ms |
22256 KB |
Output is correct |
57 |
Correct |
450 ms |
22292 KB |
Output is correct |
58 |
Correct |
461 ms |
22204 KB |
Output is correct |
59 |
Correct |
546 ms |
22196 KB |
Output is correct |
60 |
Correct |
464 ms |
22196 KB |
Output is correct |
61 |
Correct |
459 ms |
22204 KB |
Output is correct |
62 |
Correct |
466 ms |
22256 KB |
Output is correct |
63 |
Correct |
433 ms |
22208 KB |
Output is correct |
64 |
Correct |
413 ms |
22260 KB |
Output is correct |
65 |
Correct |
449 ms |
22200 KB |
Output is correct |
66 |
Correct |
423 ms |
22200 KB |
Output is correct |
67 |
Correct |
477 ms |
22208 KB |
Output is correct |
68 |
Correct |
436 ms |
22208 KB |
Output is correct |
69 |
Correct |
427 ms |
22200 KB |
Output is correct |
70 |
Correct |
458 ms |
22204 KB |
Output is correct |
71 |
Correct |
980 ms |
65536 KB |
Output is correct |
72 |
Correct |
1041 ms |
65536 KB |
Output is correct |
73 |
Correct |
1160 ms |
65536 KB |
Output is correct |
74 |
Correct |
1195 ms |
65536 KB |
Output is correct |
75 |
Correct |
1193 ms |
65536 KB |
Output is correct |
76 |
Correct |
1255 ms |
65536 KB |
Output is correct |
77 |
Correct |
1233 ms |
65536 KB |
Output is correct |
78 |
Correct |
742 ms |
65536 KB |
Output is correct |
79 |
Correct |
911 ms |
65536 KB |
Output is correct |
80 |
Correct |
1018 ms |
65536 KB |
Output is correct |
81 |
Correct |
1219 ms |
65536 KB |
Output is correct |
82 |
Correct |
1251 ms |
65536 KB |
Output is correct |
83 |
Correct |
979 ms |
65536 KB |
Output is correct |
84 |
Correct |
1206 ms |
65536 KB |
Output is correct |
85 |
Correct |
1241 ms |
65536 KB |
Output is correct |
86 |
Correct |
712 ms |
65536 KB |
Output is correct |
87 |
Correct |
983 ms |
65536 KB |
Output is correct |
88 |
Correct |
888 ms |
65536 KB |
Output is correct |
89 |
Correct |
1134 ms |
65536 KB |
Output is correct |
90 |
Correct |
1175 ms |
65536 KB |
Output is correct |
91 |
Correct |
961 ms |
65536 KB |
Output is correct |
92 |
Correct |
1239 ms |
65536 KB |
Output is correct |
93 |
Correct |
1277 ms |
65536 KB |
Output is correct |
94 |
Correct |
718 ms |
65536 KB |
Output is correct |
95 |
Correct |
1183 ms |
65536 KB |
Output is correct |
96 |
Correct |
1237 ms |
65536 KB |
Output is correct |
97 |
Correct |
1172 ms |
65536 KB |
Output is correct |
98 |
Correct |
1186 ms |
65536 KB |
Output is correct |
99 |
Correct |
1205 ms |
65536 KB |
Output is correct |
100 |
Correct |
1184 ms |
65536 KB |
Output is correct |