//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
string rk = "x-"; vector<string> spstr,rgstr;
string c1 = "xxx";
vector<string> c2,c3,c4,c5,c6,c7;
int n,a[11]; int64_t dp[11][12][22][301]; // first i frames, carry amount from previous i, carry amount from i, score after i
// carry is -1 if we don't add
string s;
bool canrg[10][10];
void solve(){
cin >> n >> s; s = ' ' + s;
for(int i = 1; i <= n; i++){
cin >> a[i];
for(int j = 0; j < 12; j++) for(int k = 0; k < 22; k++){
memset(dp[i][j][k],0,8*(30*i+1));
}
}
auto can = [&](int i, string& t){
assert(t.size() == 2);
for(int j = 0; j < 2; j++){
char c = s[i+j]; if(c == '?') continue;
if(c != t[j]) return false;
}
return true;
};
auto can1 = [&](int i, string& t){
assert(t.size() == 3);
for(int j = 0; j < 3; j++){
char c = s[i+j];
if(c == '?') continue;
if(c != t[j]) return false;
}
return true;
};
dp[0][0][0][0] = 1;
auto add = [&](int64_t& x, int64_t y){
x = (x+y);
};
// solve normal
for(int i = 1; i <= n-1; i++){
// generate good strings
bool grk = can(2*i-1,rk);
vector<string> gsp,grg;
for(auto& t : spstr) if(can(2*i-1,t)) gsp.push_back(t);
for(auto& t : rgstr) if(can(2*i-1,t)) grg.push_back(t);
swap(gsp,spstr);
swap(grg,rgstr);
int lo = 0, hi = 30*i;
if(a[i] != -1) lo = hi = a[i];
for(int j = -1; j <= 10; j++) for(int k = -1; k <= 20; k++){ // previous previous, previous carries
// strike
// our shot needs to make j zero, k non negative
if(grk && (j == -1 || j-10 == 0) && (k == -1 || (k-10 >= 0 && k-10 <= 10))){
int nk = (k == -1 ? -1 : k-10);
// we can gain some more from carries if needed
for(int gain = 0; gain <= 20; gain++){
// l >= gain+10
//if(l-gain-10 < 0) break;
for(int l = max(lo,gain+10); l <= hi; l++) add(dp[i][nk+1][gain+1][l],dp[i-1][j+1][k+1][l-gain-10]);
}
}
// spare
if(k == -1 || k-10 == 0){
// TODO: opt break
for(auto& t : spstr){
// previous previous gets subtracted by fs, previous gets subtracted by 10
// both are gone after this frame, so need to become 0
int fs = t[0]-'0';
if(j != -1 && j-fs != 0) continue;
for(int gain = 0; gain <= 10; gain++){
//if(l-gain-10 < 0) break;
// add to previous slot to satisfy one bonus feature
for(int l = max(lo,gain+10); l <= hi; l++) add(dp[i][gain+1][0][l],dp[i-1][j+1][k+1][l-gain-10]);
}
}
}
// regular
for(auto& t : rgstr){
// previous previous gets subtracted by fs, previous gets subtracted by sn+fs
// both are gone after this frame, so need to become 0
int fs = t[0]-'0',sn = t[1]-'0';
if(j != -1 && j-fs != 0) continue;
if(k != -1 && k-fs-sn != 0) continue;
//if(l-fs-sn < 0) continue;
for(int l = max(lo,fs+sn); l <= hi; l++) add(dp[i][0][0][l],dp[i-1][j+1][k+1][l-fs-sn]);
}
}
swap(gsp,spstr);
swap(grg,rgstr);
}
// solve last
// preprocess equalities
vector<string> gc2,gc3,gc4,gc5,gc6,gc7;
for(auto& t : c2) if(can1(2*n-1,t)) gc2.push_back(t);
for(auto& t : c3) if(can1(2*n-1,t)) gc3.push_back(t);
for(auto& t : c4) if(can1(2*n-1,t)) gc4.push_back(t);
for(auto& t : c5) if(can1(2*n-1,t)) gc5.push_back(t);
for(auto& t : c6) if(can1(2*n-1,t)) gc6.push_back(t);
for(auto& t : c7) if(can1(2*n-1,t)) gc7.push_back(t);
bool gc1 = can1(2*n-1,c1);
swap(gc2,c2);
swap(gc3,c3);
swap(gc4,c4);
swap(gc5,c5);
swap(gc6,c6);
swap(gc7,c7);
int64_t res = 0;
int lo = 0, hi = 300;
if(a[n] != -1) lo = hi = a[n];
for(int j = -1; j <= 10; j++) for(int k = -1; k <= 20; k++){ // previous previous, previous carries
if((j == -1 || j-10 == 0) && (k == -1 || k-20 == 0)){
// xxx
if(gc1) for(int l = max(lo,30); l <= hi; l++) add(res,dp[n-1][j+1][k+1][l-30]);
// xxA
// condition is same since bonus is only contributed in the first two shots
for(auto& t : c2) {
int ls = t[2]-'0';
for(int l = max(lo,20+ls); l <= hi; l++) add(res,dp[n-1][j+1][k+1][l-20-ls]);
}
}
if((j == -1 || j-10 == 0)){
// xA/
for(auto& t : c3) {
int fs = t[1]-'0';
if(k != -1 && k-10-fs != 0) continue;
for(int l = max(lo,20); l <= hi; l++) add(res,dp[n-1][j+1][k+1][l-20]);
}
}
// xAB
for(auto& t : c4){
int fs = t[1]-'0',sn = t[2]-'0';
if(j != -1 && j-10 != 0) continue;
if(k != -1 && k-10-fs != 0) continue;
for(int l = max(lo,10+fs+sn); l <= hi; l++) add(res,dp[n-1][j+1][k+1][l-10-fs-sn]);
}
if(k == -1 || k-10 == 0){
// A/x
for(auto& t : c5) {
int fs = t[0]-'0';
if(j != -1 && j-fs != 0) continue;
for(int l = max(lo,20); l <= hi; l++) add(res,dp[n-1][j+1][k+1][l-20]);
}
// A/B
for(auto& t : c6) if(k == -1 || k-10 == 0){
int fs = t[0]-'0',ls = t[2]-'0';
if(j != -1 && j-fs != 0) continue;
for(int l = max(lo,10+ls); l <= hi; l++) add(res,dp[n-1][j+1][k+1][l-10-ls]);
}
}
// AB-
for(auto& t : c7){
int fs = t[0]-'0',sn = t[1]-'0';
if(j != -1 && j-fs != 0) continue;
if(k != -1 && k-fs-sn != 0) continue;
for(int l = max(lo,fs+sn); l <= hi; l++) add(res,dp[n-1][j+1][k+1][l-fs-sn]);
}
}
swap(gc2,c2);
swap(gc3,c3);
swap(gc4,c4);
swap(gc5,c5);
swap(gc6,c6);
swap(gc7,c7);
cout << res << "\n";
}
int main(){
cin.tie(0)->sync_with_stdio(0);
for(int i = 0; i <= 9; i++){
spstr.push_back(to_string(i) + "/");
c2.push_back("xx"+to_string(i));
c3.push_back("x"+to_string(i)+"/");
for(int j = 0; i+j < 10; j++){
rgstr.push_back(to_string(i)+to_string(j));
c4.push_back("x"+to_string(i)+to_string(j));
c7.push_back(to_string(i)+to_string(j)+"-");
}
c5.push_back(to_string(i)+"/x");
for(int j = 0; j <= 9; j++){
c6.push_back(to_string(i)+"/"+to_string(j));
}
}
int t; cin >> t; while(t--) solve();
}
// number of strs for frame: 10 + 1 + 10
// 5726805883325784576
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
6484 KB |
Output is correct |
2 |
Correct |
6 ms |
5844 KB |
Output is correct |
3 |
Correct |
5 ms |
5848 KB |
Output is correct |
4 |
Correct |
6 ms |
6484 KB |
Output is correct |
5 |
Correct |
5 ms |
6484 KB |
Output is correct |
6 |
Correct |
5 ms |
6484 KB |
Output is correct |
7 |
Correct |
10 ms |
6484 KB |
Output is correct |
8 |
Correct |
11 ms |
6484 KB |
Output is correct |
9 |
Correct |
7 ms |
6484 KB |
Output is correct |
10 |
Correct |
9 ms |
6580 KB |
Output is correct |
11 |
Correct |
12 ms |
6484 KB |
Output is correct |
12 |
Correct |
5 ms |
1492 KB |
Output is correct |
13 |
Correct |
1 ms |
1492 KB |
Output is correct |
14 |
Correct |
3 ms |
1492 KB |
Output is correct |
15 |
Correct |
2 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
6568 KB |
Output is correct |
2 |
Correct |
10 ms |
6484 KB |
Output is correct |
3 |
Correct |
9 ms |
6484 KB |
Output is correct |
4 |
Correct |
11 ms |
6484 KB |
Output is correct |
5 |
Correct |
12 ms |
6484 KB |
Output is correct |
6 |
Correct |
13 ms |
6580 KB |
Output is correct |
7 |
Correct |
17 ms |
6572 KB |
Output is correct |
8 |
Correct |
15 ms |
6572 KB |
Output is correct |
9 |
Correct |
15 ms |
6580 KB |
Output is correct |
10 |
Correct |
24 ms |
6576 KB |
Output is correct |
11 |
Correct |
24 ms |
6484 KB |
Output is correct |
12 |
Correct |
25 ms |
6484 KB |
Output is correct |
13 |
Correct |
29 ms |
6484 KB |
Output is correct |
14 |
Correct |
22 ms |
6484 KB |
Output is correct |
15 |
Correct |
32 ms |
6564 KB |
Output is correct |
16 |
Correct |
22 ms |
6484 KB |
Output is correct |
17 |
Correct |
23 ms |
6484 KB |
Output is correct |
18 |
Correct |
30 ms |
6484 KB |
Output is correct |
19 |
Correct |
30 ms |
6572 KB |
Output is correct |
20 |
Correct |
32 ms |
6484 KB |
Output is correct |
21 |
Correct |
34 ms |
6556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
6484 KB |
Output is correct |
2 |
Correct |
7 ms |
6484 KB |
Output is correct |
3 |
Correct |
7 ms |
6484 KB |
Output is correct |
4 |
Correct |
7 ms |
6572 KB |
Output is correct |
5 |
Correct |
7 ms |
6484 KB |
Output is correct |
6 |
Correct |
11 ms |
6484 KB |
Output is correct |
7 |
Correct |
10 ms |
6484 KB |
Output is correct |
8 |
Correct |
17 ms |
6484 KB |
Output is correct |
9 |
Correct |
12 ms |
6484 KB |
Output is correct |
10 |
Correct |
15 ms |
6484 KB |
Output is correct |
11 |
Correct |
15 ms |
6572 KB |
Output is correct |
12 |
Correct |
16 ms |
6580 KB |
Output is correct |
13 |
Correct |
14 ms |
6484 KB |
Output is correct |
14 |
Correct |
17 ms |
6484 KB |
Output is correct |
15 |
Correct |
17 ms |
6484 KB |
Output is correct |
16 |
Correct |
21 ms |
6540 KB |
Output is correct |
17 |
Correct |
15 ms |
6484 KB |
Output is correct |
18 |
Correct |
9 ms |
6484 KB |
Output is correct |
19 |
Correct |
8 ms |
6572 KB |
Output is correct |
20 |
Correct |
10 ms |
6580 KB |
Output is correct |
21 |
Correct |
9 ms |
6484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
6484 KB |
Output is correct |
2 |
Correct |
10 ms |
6568 KB |
Output is correct |
3 |
Correct |
13 ms |
6568 KB |
Output is correct |
4 |
Correct |
11 ms |
6468 KB |
Output is correct |
5 |
Correct |
11 ms |
5844 KB |
Output is correct |
6 |
Correct |
13 ms |
6572 KB |
Output is correct |
7 |
Correct |
12 ms |
6568 KB |
Output is correct |
8 |
Correct |
11 ms |
6568 KB |
Output is correct |
9 |
Correct |
11 ms |
6484 KB |
Output is correct |
10 |
Correct |
30 ms |
6484 KB |
Output is correct |
11 |
Correct |
21 ms |
6484 KB |
Output is correct |
12 |
Correct |
20 ms |
6576 KB |
Output is correct |
13 |
Correct |
16 ms |
6484 KB |
Output is correct |
14 |
Correct |
16 ms |
6568 KB |
Output is correct |
15 |
Correct |
19 ms |
6572 KB |
Output is correct |
16 |
Correct |
17 ms |
6572 KB |
Output is correct |
17 |
Correct |
20 ms |
6572 KB |
Output is correct |
18 |
Correct |
17 ms |
6484 KB |
Output is correct |
19 |
Correct |
12 ms |
6568 KB |
Output is correct |
20 |
Correct |
17 ms |
6488 KB |
Output is correct |
21 |
Correct |
14 ms |
6560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
6484 KB |
Output is correct |
2 |
Correct |
6 ms |
5844 KB |
Output is correct |
3 |
Correct |
5 ms |
5848 KB |
Output is correct |
4 |
Correct |
6 ms |
6484 KB |
Output is correct |
5 |
Correct |
5 ms |
6484 KB |
Output is correct |
6 |
Correct |
5 ms |
6484 KB |
Output is correct |
7 |
Correct |
10 ms |
6484 KB |
Output is correct |
8 |
Correct |
11 ms |
6484 KB |
Output is correct |
9 |
Correct |
7 ms |
6484 KB |
Output is correct |
10 |
Correct |
9 ms |
6580 KB |
Output is correct |
11 |
Correct |
12 ms |
6484 KB |
Output is correct |
12 |
Correct |
5 ms |
1492 KB |
Output is correct |
13 |
Correct |
1 ms |
1492 KB |
Output is correct |
14 |
Correct |
3 ms |
1492 KB |
Output is correct |
15 |
Correct |
2 ms |
1492 KB |
Output is correct |
16 |
Correct |
12 ms |
6568 KB |
Output is correct |
17 |
Correct |
10 ms |
6484 KB |
Output is correct |
18 |
Correct |
9 ms |
6484 KB |
Output is correct |
19 |
Correct |
11 ms |
6484 KB |
Output is correct |
20 |
Correct |
12 ms |
6484 KB |
Output is correct |
21 |
Correct |
13 ms |
6580 KB |
Output is correct |
22 |
Correct |
17 ms |
6572 KB |
Output is correct |
23 |
Correct |
15 ms |
6572 KB |
Output is correct |
24 |
Correct |
15 ms |
6580 KB |
Output is correct |
25 |
Correct |
24 ms |
6576 KB |
Output is correct |
26 |
Correct |
24 ms |
6484 KB |
Output is correct |
27 |
Correct |
25 ms |
6484 KB |
Output is correct |
28 |
Correct |
29 ms |
6484 KB |
Output is correct |
29 |
Correct |
22 ms |
6484 KB |
Output is correct |
30 |
Correct |
32 ms |
6564 KB |
Output is correct |
31 |
Correct |
22 ms |
6484 KB |
Output is correct |
32 |
Correct |
23 ms |
6484 KB |
Output is correct |
33 |
Correct |
30 ms |
6484 KB |
Output is correct |
34 |
Correct |
30 ms |
6572 KB |
Output is correct |
35 |
Correct |
32 ms |
6484 KB |
Output is correct |
36 |
Correct |
34 ms |
6556 KB |
Output is correct |
37 |
Correct |
9 ms |
6484 KB |
Output is correct |
38 |
Correct |
7 ms |
6484 KB |
Output is correct |
39 |
Correct |
7 ms |
6484 KB |
Output is correct |
40 |
Correct |
7 ms |
6572 KB |
Output is correct |
41 |
Correct |
7 ms |
6484 KB |
Output is correct |
42 |
Correct |
11 ms |
6484 KB |
Output is correct |
43 |
Correct |
10 ms |
6484 KB |
Output is correct |
44 |
Correct |
17 ms |
6484 KB |
Output is correct |
45 |
Correct |
12 ms |
6484 KB |
Output is correct |
46 |
Correct |
15 ms |
6484 KB |
Output is correct |
47 |
Correct |
15 ms |
6572 KB |
Output is correct |
48 |
Correct |
16 ms |
6580 KB |
Output is correct |
49 |
Correct |
14 ms |
6484 KB |
Output is correct |
50 |
Correct |
17 ms |
6484 KB |
Output is correct |
51 |
Correct |
17 ms |
6484 KB |
Output is correct |
52 |
Correct |
21 ms |
6540 KB |
Output is correct |
53 |
Correct |
15 ms |
6484 KB |
Output is correct |
54 |
Correct |
9 ms |
6484 KB |
Output is correct |
55 |
Correct |
8 ms |
6572 KB |
Output is correct |
56 |
Correct |
10 ms |
6580 KB |
Output is correct |
57 |
Correct |
9 ms |
6484 KB |
Output is correct |
58 |
Correct |
15 ms |
6484 KB |
Output is correct |
59 |
Correct |
10 ms |
6568 KB |
Output is correct |
60 |
Correct |
13 ms |
6568 KB |
Output is correct |
61 |
Correct |
11 ms |
6468 KB |
Output is correct |
62 |
Correct |
11 ms |
5844 KB |
Output is correct |
63 |
Correct |
13 ms |
6572 KB |
Output is correct |
64 |
Correct |
12 ms |
6568 KB |
Output is correct |
65 |
Correct |
11 ms |
6568 KB |
Output is correct |
66 |
Correct |
11 ms |
6484 KB |
Output is correct |
67 |
Correct |
30 ms |
6484 KB |
Output is correct |
68 |
Correct |
21 ms |
6484 KB |
Output is correct |
69 |
Correct |
20 ms |
6576 KB |
Output is correct |
70 |
Correct |
16 ms |
6484 KB |
Output is correct |
71 |
Correct |
16 ms |
6568 KB |
Output is correct |
72 |
Correct |
19 ms |
6572 KB |
Output is correct |
73 |
Correct |
17 ms |
6572 KB |
Output is correct |
74 |
Correct |
20 ms |
6572 KB |
Output is correct |
75 |
Correct |
17 ms |
6484 KB |
Output is correct |
76 |
Correct |
12 ms |
6568 KB |
Output is correct |
77 |
Correct |
17 ms |
6488 KB |
Output is correct |
78 |
Correct |
14 ms |
6560 KB |
Output is correct |
79 |
Correct |
12 ms |
6484 KB |
Output is correct |
80 |
Correct |
14 ms |
6484 KB |
Output is correct |
81 |
Correct |
11 ms |
6484 KB |
Output is correct |
82 |
Correct |
11 ms |
6484 KB |
Output is correct |
83 |
Correct |
10 ms |
6484 KB |
Output is correct |
84 |
Correct |
15 ms |
6484 KB |
Output is correct |
85 |
Correct |
15 ms |
6572 KB |
Output is correct |
86 |
Correct |
17 ms |
6576 KB |
Output is correct |
87 |
Correct |
15 ms |
6576 KB |
Output is correct |
88 |
Correct |
20 ms |
6484 KB |
Output is correct |
89 |
Correct |
31 ms |
6572 KB |
Output is correct |
90 |
Correct |
33 ms |
6572 KB |
Output is correct |
91 |
Correct |
27 ms |
6484 KB |
Output is correct |
92 |
Correct |
11 ms |
6484 KB |
Output is correct |
93 |
Correct |
46 ms |
6564 KB |
Output is correct |
94 |
Correct |
40 ms |
6560 KB |
Output is correct |
95 |
Correct |
52 ms |
6552 KB |
Output is correct |
96 |
Correct |
41 ms |
6552 KB |
Output is correct |
97 |
Correct |
40 ms |
6484 KB |
Output is correct |
98 |
Correct |
36 ms |
6556 KB |
Output is correct |
99 |
Correct |
39 ms |
6484 KB |
Output is correct |
100 |
Correct |
44 ms |
6484 KB |
Output is correct |