#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a,b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a,b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
*/
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
void solve(int test_case)
{
ll n; cin >> n;
string s; cin >> s;
s = "$" + s;
vector<ll> a(n+5);
rep1(i,n) cin >> a[i];
ll m = 30*n+5;
ll dp1[m+5][15][15], dp2[m+5][15][15];
memset(dp1,0,sizeof dp1);
memset(dp2,0,sizeof dp2);
if(a[n] != -1){
dp1[a[n]][0][0] = 1;
}
else{
rep(j,m+1){
dp1[j][0][0] = 1;
}
}
ll ptr = 2*n+1;
auto ok = [&](string put, string pat){
assert(sz(put) == sz(pat));
rep(i,sz(put)){
if(pat[i] == '?') conts;
if(put[i] != pat[i]) return false;
}
return true;
};
// final stage
{
string pat = "";
rep(iter,3){
pat.pb(s[ptr--]);
}
reverse(all(pat));
vector<array<ll,3>> possible;
// xxx
if(ok("xxx",pat)){
ll score = 30;
possible.pb({score,10,10});
}
// xxA
rep(A,10){
string curr = "xx"+to_string(A);
if(ok(curr,pat)){
ll score = 20+A;
possible.pb({score,10,10});
}
}
// xA/
rep(A,10){
string curr = "x"+to_string(A)+"/";
if(ok(curr,pat)){
ll score = 20;
possible.pb({score,10,A});
}
}
// xAB
rep(A,10){
rep(B,10){
if(A+B >= 10) break;
string curr = "x"+to_string(A)+to_string(B);
if(ok(curr,pat)){
ll score = 10+A+B;
possible.pb({score,10,A});
}
}
}
// A/x
rep(A,10){
string curr = to_string(A)+"/x";
if(ok(curr,pat)){
ll score = 20;
possible.pb({score,A,10-A});
}
}
// A/B
rep(A,10){
rep(B,10){
string curr = to_string(A)+"/"+to_string(B);
if(ok(curr,pat)){
ll score = 10+B;
possible.pb({score,A,10-A});
}
}
}
// AB-
rep(A,10){
rep(B,10){
if(A+B >= 10) break;
string curr = to_string(A)+to_string(B)+"-";
if(ok(curr,pat)){
ll score = A+B;
possible.pb({score,A,B});
}
}
}
rep(j,m+1){
if(!dp1[j][0][0]) conts;
for(auto [score,x,y] : possible){
if(j >= score){
dp2[j-score][x][y] += dp1[j][0][0];
}
}
}
}
rev(i,n-1,1){
string pat = "";
rep(iter,2){
pat.pb(s[ptr--]);
}
reverse(all(pat));
rep(j,m+1){
rep(x,11){
rep(y,11){
if(a[i] != -1 and a[i] != j){
dp1[j][x][y] = 0;
}
else{
dp1[j][x][y] = dp2[j][x][y];
}
dp2[j][x][y] = 0;
}
}
}
rep(x,11){
rep(y,11){
vector<array<ll,3>> possible;
// x-
if(ok("x-",pat)){
ll score = 10+x+y;
possible.pb({score,10,x});
}
// A/
rep(A,10){
string curr = to_string(A)+"/";
if(ok(curr,pat)){
ll score = 10+x;
possible.pb({score,A,10-A});
}
}
// AB
rep(A,10){
rep(B,10){
if(A+B >= 10) conts;
string curr = to_string(A)+to_string(B);
if(ok(curr,pat)){
ll score = A+B;
possible.pb({score,A,B});
}
}
}
rep(j,m+1){
if(!dp1[j][x][y]) conts;
for(auto [score,x2,y2] : possible){
if(j >= score){
dp2[j-score][x2][y2] += dp1[j][x][y];
}
}
}
}
}
}
ll ans = 0;
rep(x,11){
rep(y,11){
ans += dp2[0][x][y];
}
}
cout << ans << endl;
}
int main()
{
fastio;
int t = 1;
cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
1372 KB |
Output is correct |
2 |
Correct |
34 ms |
1372 KB |
Output is correct |
3 |
Correct |
37 ms |
1368 KB |
Output is correct |
4 |
Correct |
39 ms |
1372 KB |
Output is correct |
5 |
Correct |
41 ms |
1528 KB |
Output is correct |
6 |
Correct |
46 ms |
1528 KB |
Output is correct |
7 |
Correct |
67 ms |
1620 KB |
Output is correct |
8 |
Correct |
49 ms |
1368 KB |
Output is correct |
9 |
Correct |
40 ms |
1372 KB |
Output is correct |
10 |
Correct |
49 ms |
1540 KB |
Output is correct |
11 |
Correct |
81 ms |
1372 KB |
Output is correct |
12 |
Correct |
10 ms |
600 KB |
Output is correct |
13 |
Correct |
4 ms |
604 KB |
Output is correct |
14 |
Correct |
11 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
1368 KB |
Output is correct |
2 |
Correct |
51 ms |
1368 KB |
Output is correct |
3 |
Correct |
49 ms |
1372 KB |
Output is correct |
4 |
Correct |
50 ms |
1824 KB |
Output is correct |
5 |
Correct |
61 ms |
1372 KB |
Output is correct |
6 |
Correct |
87 ms |
1452 KB |
Output is correct |
7 |
Correct |
88 ms |
1368 KB |
Output is correct |
8 |
Correct |
85 ms |
1528 KB |
Output is correct |
9 |
Correct |
83 ms |
1536 KB |
Output is correct |
10 |
Correct |
107 ms |
1372 KB |
Output is correct |
11 |
Correct |
116 ms |
1372 KB |
Output is correct |
12 |
Correct |
124 ms |
1372 KB |
Output is correct |
13 |
Correct |
116 ms |
1528 KB |
Output is correct |
14 |
Correct |
102 ms |
1532 KB |
Output is correct |
15 |
Correct |
107 ms |
1372 KB |
Output is correct |
16 |
Correct |
105 ms |
1528 KB |
Output is correct |
17 |
Correct |
101 ms |
1372 KB |
Output is correct |
18 |
Correct |
124 ms |
1528 KB |
Output is correct |
19 |
Correct |
128 ms |
1368 KB |
Output is correct |
20 |
Correct |
129 ms |
1776 KB |
Output is correct |
21 |
Correct |
146 ms |
1532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
1372 KB |
Output is correct |
2 |
Correct |
55 ms |
1524 KB |
Output is correct |
3 |
Correct |
48 ms |
1372 KB |
Output is correct |
4 |
Correct |
45 ms |
1536 KB |
Output is correct |
5 |
Correct |
43 ms |
1372 KB |
Output is correct |
6 |
Correct |
82 ms |
1372 KB |
Output is correct |
7 |
Correct |
82 ms |
1372 KB |
Output is correct |
8 |
Correct |
81 ms |
1528 KB |
Output is correct |
9 |
Correct |
84 ms |
1528 KB |
Output is correct |
10 |
Correct |
98 ms |
1372 KB |
Output is correct |
11 |
Correct |
98 ms |
1372 KB |
Output is correct |
12 |
Correct |
97 ms |
1528 KB |
Output is correct |
13 |
Correct |
98 ms |
1524 KB |
Output is correct |
14 |
Correct |
105 ms |
1372 KB |
Output is correct |
15 |
Correct |
94 ms |
1528 KB |
Output is correct |
16 |
Correct |
97 ms |
1548 KB |
Output is correct |
17 |
Correct |
97 ms |
1368 KB |
Output is correct |
18 |
Correct |
91 ms |
1544 KB |
Output is correct |
19 |
Correct |
85 ms |
1372 KB |
Output is correct |
20 |
Correct |
81 ms |
1528 KB |
Output is correct |
21 |
Correct |
81 ms |
1524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
1372 KB |
Output is correct |
2 |
Correct |
64 ms |
1532 KB |
Output is correct |
3 |
Correct |
88 ms |
1420 KB |
Output is correct |
4 |
Correct |
60 ms |
1416 KB |
Output is correct |
5 |
Correct |
59 ms |
1372 KB |
Output is correct |
6 |
Correct |
59 ms |
1412 KB |
Output is correct |
7 |
Correct |
61 ms |
1528 KB |
Output is correct |
8 |
Correct |
55 ms |
1372 KB |
Output is correct |
9 |
Correct |
59 ms |
1372 KB |
Output is correct |
10 |
Correct |
161 ms |
1372 KB |
Output is correct |
11 |
Correct |
159 ms |
1620 KB |
Output is correct |
12 |
Correct |
148 ms |
1624 KB |
Output is correct |
13 |
Correct |
144 ms |
1372 KB |
Output is correct |
14 |
Correct |
98 ms |
1368 KB |
Output is correct |
15 |
Correct |
99 ms |
1620 KB |
Output is correct |
16 |
Correct |
98 ms |
1624 KB |
Output is correct |
17 |
Correct |
95 ms |
1368 KB |
Output is correct |
18 |
Correct |
80 ms |
1528 KB |
Output is correct |
19 |
Correct |
80 ms |
1524 KB |
Output is correct |
20 |
Correct |
79 ms |
1692 KB |
Output is correct |
21 |
Correct |
74 ms |
1368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
1372 KB |
Output is correct |
2 |
Correct |
34 ms |
1372 KB |
Output is correct |
3 |
Correct |
37 ms |
1368 KB |
Output is correct |
4 |
Correct |
39 ms |
1372 KB |
Output is correct |
5 |
Correct |
41 ms |
1528 KB |
Output is correct |
6 |
Correct |
46 ms |
1528 KB |
Output is correct |
7 |
Correct |
67 ms |
1620 KB |
Output is correct |
8 |
Correct |
49 ms |
1368 KB |
Output is correct |
9 |
Correct |
40 ms |
1372 KB |
Output is correct |
10 |
Correct |
49 ms |
1540 KB |
Output is correct |
11 |
Correct |
81 ms |
1372 KB |
Output is correct |
12 |
Correct |
10 ms |
600 KB |
Output is correct |
13 |
Correct |
4 ms |
604 KB |
Output is correct |
14 |
Correct |
11 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
604 KB |
Output is correct |
16 |
Correct |
38 ms |
1368 KB |
Output is correct |
17 |
Correct |
51 ms |
1368 KB |
Output is correct |
18 |
Correct |
49 ms |
1372 KB |
Output is correct |
19 |
Correct |
50 ms |
1824 KB |
Output is correct |
20 |
Correct |
61 ms |
1372 KB |
Output is correct |
21 |
Correct |
87 ms |
1452 KB |
Output is correct |
22 |
Correct |
88 ms |
1368 KB |
Output is correct |
23 |
Correct |
85 ms |
1528 KB |
Output is correct |
24 |
Correct |
83 ms |
1536 KB |
Output is correct |
25 |
Correct |
107 ms |
1372 KB |
Output is correct |
26 |
Correct |
116 ms |
1372 KB |
Output is correct |
27 |
Correct |
124 ms |
1372 KB |
Output is correct |
28 |
Correct |
116 ms |
1528 KB |
Output is correct |
29 |
Correct |
102 ms |
1532 KB |
Output is correct |
30 |
Correct |
107 ms |
1372 KB |
Output is correct |
31 |
Correct |
105 ms |
1528 KB |
Output is correct |
32 |
Correct |
101 ms |
1372 KB |
Output is correct |
33 |
Correct |
124 ms |
1528 KB |
Output is correct |
34 |
Correct |
128 ms |
1368 KB |
Output is correct |
35 |
Correct |
129 ms |
1776 KB |
Output is correct |
36 |
Correct |
146 ms |
1532 KB |
Output is correct |
37 |
Correct |
22 ms |
1372 KB |
Output is correct |
38 |
Correct |
55 ms |
1524 KB |
Output is correct |
39 |
Correct |
48 ms |
1372 KB |
Output is correct |
40 |
Correct |
45 ms |
1536 KB |
Output is correct |
41 |
Correct |
43 ms |
1372 KB |
Output is correct |
42 |
Correct |
82 ms |
1372 KB |
Output is correct |
43 |
Correct |
82 ms |
1372 KB |
Output is correct |
44 |
Correct |
81 ms |
1528 KB |
Output is correct |
45 |
Correct |
84 ms |
1528 KB |
Output is correct |
46 |
Correct |
98 ms |
1372 KB |
Output is correct |
47 |
Correct |
98 ms |
1372 KB |
Output is correct |
48 |
Correct |
97 ms |
1528 KB |
Output is correct |
49 |
Correct |
98 ms |
1524 KB |
Output is correct |
50 |
Correct |
105 ms |
1372 KB |
Output is correct |
51 |
Correct |
94 ms |
1528 KB |
Output is correct |
52 |
Correct |
97 ms |
1548 KB |
Output is correct |
53 |
Correct |
97 ms |
1368 KB |
Output is correct |
54 |
Correct |
91 ms |
1544 KB |
Output is correct |
55 |
Correct |
85 ms |
1372 KB |
Output is correct |
56 |
Correct |
81 ms |
1528 KB |
Output is correct |
57 |
Correct |
81 ms |
1524 KB |
Output is correct |
58 |
Correct |
90 ms |
1372 KB |
Output is correct |
59 |
Correct |
64 ms |
1532 KB |
Output is correct |
60 |
Correct |
88 ms |
1420 KB |
Output is correct |
61 |
Correct |
60 ms |
1416 KB |
Output is correct |
62 |
Correct |
59 ms |
1372 KB |
Output is correct |
63 |
Correct |
59 ms |
1412 KB |
Output is correct |
64 |
Correct |
61 ms |
1528 KB |
Output is correct |
65 |
Correct |
55 ms |
1372 KB |
Output is correct |
66 |
Correct |
59 ms |
1372 KB |
Output is correct |
67 |
Correct |
161 ms |
1372 KB |
Output is correct |
68 |
Correct |
159 ms |
1620 KB |
Output is correct |
69 |
Correct |
148 ms |
1624 KB |
Output is correct |
70 |
Correct |
144 ms |
1372 KB |
Output is correct |
71 |
Correct |
98 ms |
1368 KB |
Output is correct |
72 |
Correct |
99 ms |
1620 KB |
Output is correct |
73 |
Correct |
98 ms |
1624 KB |
Output is correct |
74 |
Correct |
95 ms |
1368 KB |
Output is correct |
75 |
Correct |
80 ms |
1528 KB |
Output is correct |
76 |
Correct |
80 ms |
1524 KB |
Output is correct |
77 |
Correct |
79 ms |
1692 KB |
Output is correct |
78 |
Correct |
74 ms |
1368 KB |
Output is correct |
79 |
Correct |
86 ms |
1532 KB |
Output is correct |
80 |
Correct |
63 ms |
1368 KB |
Output is correct |
81 |
Correct |
61 ms |
1620 KB |
Output is correct |
82 |
Correct |
57 ms |
1372 KB |
Output is correct |
83 |
Correct |
51 ms |
1372 KB |
Output is correct |
84 |
Correct |
88 ms |
1372 KB |
Output is correct |
85 |
Correct |
88 ms |
1368 KB |
Output is correct |
86 |
Correct |
85 ms |
1372 KB |
Output is correct |
87 |
Correct |
86 ms |
1372 KB |
Output is correct |
88 |
Correct |
100 ms |
1532 KB |
Output is correct |
89 |
Correct |
113 ms |
1372 KB |
Output is correct |
90 |
Correct |
105 ms |
1368 KB |
Output is correct |
91 |
Correct |
117 ms |
1532 KB |
Output is correct |
92 |
Correct |
76 ms |
1528 KB |
Output is correct |
93 |
Correct |
100 ms |
1524 KB |
Output is correct |
94 |
Correct |
131 ms |
1532 KB |
Output is correct |
95 |
Correct |
160 ms |
1524 KB |
Output is correct |
96 |
Correct |
179 ms |
1532 KB |
Output is correct |
97 |
Correct |
259 ms |
1532 KB |
Output is correct |
98 |
Correct |
268 ms |
1528 KB |
Output is correct |
99 |
Correct |
280 ms |
1368 KB |
Output is correct |
100 |
Correct |
279 ms |
1524 KB |
Output is correct |