#pragma GCC optimize("O3,unroll-loops")
#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
1372 KB |
Output is correct |
2 |
Correct |
30 ms |
1368 KB |
Output is correct |
3 |
Correct |
33 ms |
1372 KB |
Output is correct |
4 |
Correct |
35 ms |
1372 KB |
Output is correct |
5 |
Correct |
36 ms |
1372 KB |
Output is correct |
6 |
Correct |
41 ms |
1372 KB |
Output is correct |
7 |
Correct |
49 ms |
1368 KB |
Output is correct |
8 |
Correct |
43 ms |
1372 KB |
Output is correct |
9 |
Correct |
36 ms |
1372 KB |
Output is correct |
10 |
Correct |
42 ms |
1368 KB |
Output is correct |
11 |
Correct |
68 ms |
1368 KB |
Output is correct |
12 |
Correct |
10 ms |
600 KB |
Output is correct |
13 |
Correct |
2 ms |
600 KB |
Output is correct |
14 |
Correct |
10 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
1372 KB |
Output is correct |
2 |
Correct |
45 ms |
1424 KB |
Output is correct |
3 |
Correct |
45 ms |
1368 KB |
Output is correct |
4 |
Correct |
44 ms |
1368 KB |
Output is correct |
5 |
Correct |
52 ms |
1368 KB |
Output is correct |
6 |
Correct |
70 ms |
1372 KB |
Output is correct |
7 |
Correct |
78 ms |
1372 KB |
Output is correct |
8 |
Correct |
74 ms |
1368 KB |
Output is correct |
9 |
Correct |
71 ms |
1372 KB |
Output is correct |
10 |
Correct |
94 ms |
1368 KB |
Output is correct |
11 |
Correct |
98 ms |
1372 KB |
Output is correct |
12 |
Correct |
108 ms |
1372 KB |
Output is correct |
13 |
Correct |
104 ms |
1372 KB |
Output is correct |
14 |
Correct |
93 ms |
1620 KB |
Output is correct |
15 |
Correct |
92 ms |
1372 KB |
Output is correct |
16 |
Correct |
88 ms |
1368 KB |
Output is correct |
17 |
Correct |
91 ms |
1372 KB |
Output is correct |
18 |
Correct |
115 ms |
1372 KB |
Output is correct |
19 |
Correct |
112 ms |
1372 KB |
Output is correct |
20 |
Correct |
118 ms |
1532 KB |
Output is correct |
21 |
Correct |
123 ms |
1536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
1372 KB |
Output is correct |
2 |
Correct |
47 ms |
1372 KB |
Output is correct |
3 |
Correct |
42 ms |
1572 KB |
Output is correct |
4 |
Correct |
40 ms |
1528 KB |
Output is correct |
5 |
Correct |
38 ms |
1324 KB |
Output is correct |
6 |
Correct |
71 ms |
1368 KB |
Output is correct |
7 |
Correct |
71 ms |
1372 KB |
Output is correct |
8 |
Correct |
71 ms |
1372 KB |
Output is correct |
9 |
Correct |
79 ms |
1548 KB |
Output is correct |
10 |
Correct |
84 ms |
1368 KB |
Output is correct |
11 |
Correct |
85 ms |
1372 KB |
Output is correct |
12 |
Correct |
91 ms |
1368 KB |
Output is correct |
13 |
Correct |
84 ms |
1372 KB |
Output is correct |
14 |
Correct |
84 ms |
1368 KB |
Output is correct |
15 |
Correct |
84 ms |
1524 KB |
Output is correct |
16 |
Correct |
85 ms |
1372 KB |
Output is correct |
17 |
Correct |
84 ms |
1368 KB |
Output is correct |
18 |
Correct |
79 ms |
1544 KB |
Output is correct |
19 |
Correct |
70 ms |
1532 KB |
Output is correct |
20 |
Correct |
73 ms |
1368 KB |
Output is correct |
21 |
Correct |
71 ms |
1532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
1528 KB |
Output is correct |
2 |
Correct |
55 ms |
1372 KB |
Output is correct |
3 |
Correct |
77 ms |
1524 KB |
Output is correct |
4 |
Correct |
53 ms |
1360 KB |
Output is correct |
5 |
Correct |
51 ms |
1384 KB |
Output is correct |
6 |
Correct |
52 ms |
1444 KB |
Output is correct |
7 |
Correct |
54 ms |
1372 KB |
Output is correct |
8 |
Correct |
49 ms |
1776 KB |
Output is correct |
9 |
Correct |
58 ms |
1372 KB |
Output is correct |
10 |
Correct |
164 ms |
1520 KB |
Output is correct |
11 |
Correct |
142 ms |
1524 KB |
Output is correct |
12 |
Correct |
138 ms |
1372 KB |
Output is correct |
13 |
Correct |
130 ms |
1520 KB |
Output is correct |
14 |
Correct |
90 ms |
1524 KB |
Output is correct |
15 |
Correct |
82 ms |
1372 KB |
Output is correct |
16 |
Correct |
89 ms |
1368 KB |
Output is correct |
17 |
Correct |
87 ms |
1524 KB |
Output is correct |
18 |
Correct |
67 ms |
1368 KB |
Output is correct |
19 |
Correct |
69 ms |
1372 KB |
Output is correct |
20 |
Correct |
75 ms |
1524 KB |
Output is correct |
21 |
Correct |
66 ms |
1528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
1372 KB |
Output is correct |
2 |
Correct |
30 ms |
1368 KB |
Output is correct |
3 |
Correct |
33 ms |
1372 KB |
Output is correct |
4 |
Correct |
35 ms |
1372 KB |
Output is correct |
5 |
Correct |
36 ms |
1372 KB |
Output is correct |
6 |
Correct |
41 ms |
1372 KB |
Output is correct |
7 |
Correct |
49 ms |
1368 KB |
Output is correct |
8 |
Correct |
43 ms |
1372 KB |
Output is correct |
9 |
Correct |
36 ms |
1372 KB |
Output is correct |
10 |
Correct |
42 ms |
1368 KB |
Output is correct |
11 |
Correct |
68 ms |
1368 KB |
Output is correct |
12 |
Correct |
10 ms |
600 KB |
Output is correct |
13 |
Correct |
2 ms |
600 KB |
Output is correct |
14 |
Correct |
10 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
604 KB |
Output is correct |
16 |
Correct |
34 ms |
1372 KB |
Output is correct |
17 |
Correct |
45 ms |
1424 KB |
Output is correct |
18 |
Correct |
45 ms |
1368 KB |
Output is correct |
19 |
Correct |
44 ms |
1368 KB |
Output is correct |
20 |
Correct |
52 ms |
1368 KB |
Output is correct |
21 |
Correct |
70 ms |
1372 KB |
Output is correct |
22 |
Correct |
78 ms |
1372 KB |
Output is correct |
23 |
Correct |
74 ms |
1368 KB |
Output is correct |
24 |
Correct |
71 ms |
1372 KB |
Output is correct |
25 |
Correct |
94 ms |
1368 KB |
Output is correct |
26 |
Correct |
98 ms |
1372 KB |
Output is correct |
27 |
Correct |
108 ms |
1372 KB |
Output is correct |
28 |
Correct |
104 ms |
1372 KB |
Output is correct |
29 |
Correct |
93 ms |
1620 KB |
Output is correct |
30 |
Correct |
92 ms |
1372 KB |
Output is correct |
31 |
Correct |
88 ms |
1368 KB |
Output is correct |
32 |
Correct |
91 ms |
1372 KB |
Output is correct |
33 |
Correct |
115 ms |
1372 KB |
Output is correct |
34 |
Correct |
112 ms |
1372 KB |
Output is correct |
35 |
Correct |
118 ms |
1532 KB |
Output is correct |
36 |
Correct |
123 ms |
1536 KB |
Output is correct |
37 |
Correct |
19 ms |
1372 KB |
Output is correct |
38 |
Correct |
47 ms |
1372 KB |
Output is correct |
39 |
Correct |
42 ms |
1572 KB |
Output is correct |
40 |
Correct |
40 ms |
1528 KB |
Output is correct |
41 |
Correct |
38 ms |
1324 KB |
Output is correct |
42 |
Correct |
71 ms |
1368 KB |
Output is correct |
43 |
Correct |
71 ms |
1372 KB |
Output is correct |
44 |
Correct |
71 ms |
1372 KB |
Output is correct |
45 |
Correct |
79 ms |
1548 KB |
Output is correct |
46 |
Correct |
84 ms |
1368 KB |
Output is correct |
47 |
Correct |
85 ms |
1372 KB |
Output is correct |
48 |
Correct |
91 ms |
1368 KB |
Output is correct |
49 |
Correct |
84 ms |
1372 KB |
Output is correct |
50 |
Correct |
84 ms |
1368 KB |
Output is correct |
51 |
Correct |
84 ms |
1524 KB |
Output is correct |
52 |
Correct |
85 ms |
1372 KB |
Output is correct |
53 |
Correct |
84 ms |
1368 KB |
Output is correct |
54 |
Correct |
79 ms |
1544 KB |
Output is correct |
55 |
Correct |
70 ms |
1532 KB |
Output is correct |
56 |
Correct |
73 ms |
1368 KB |
Output is correct |
57 |
Correct |
71 ms |
1532 KB |
Output is correct |
58 |
Correct |
81 ms |
1528 KB |
Output is correct |
59 |
Correct |
55 ms |
1372 KB |
Output is correct |
60 |
Correct |
77 ms |
1524 KB |
Output is correct |
61 |
Correct |
53 ms |
1360 KB |
Output is correct |
62 |
Correct |
51 ms |
1384 KB |
Output is correct |
63 |
Correct |
52 ms |
1444 KB |
Output is correct |
64 |
Correct |
54 ms |
1372 KB |
Output is correct |
65 |
Correct |
49 ms |
1776 KB |
Output is correct |
66 |
Correct |
58 ms |
1372 KB |
Output is correct |
67 |
Correct |
164 ms |
1520 KB |
Output is correct |
68 |
Correct |
142 ms |
1524 KB |
Output is correct |
69 |
Correct |
138 ms |
1372 KB |
Output is correct |
70 |
Correct |
130 ms |
1520 KB |
Output is correct |
71 |
Correct |
90 ms |
1524 KB |
Output is correct |
72 |
Correct |
82 ms |
1372 KB |
Output is correct |
73 |
Correct |
89 ms |
1368 KB |
Output is correct |
74 |
Correct |
87 ms |
1524 KB |
Output is correct |
75 |
Correct |
67 ms |
1368 KB |
Output is correct |
76 |
Correct |
69 ms |
1372 KB |
Output is correct |
77 |
Correct |
75 ms |
1524 KB |
Output is correct |
78 |
Correct |
66 ms |
1528 KB |
Output is correct |
79 |
Correct |
78 ms |
1528 KB |
Output is correct |
80 |
Correct |
56 ms |
1372 KB |
Output is correct |
81 |
Correct |
49 ms |
1372 KB |
Output is correct |
82 |
Correct |
54 ms |
1620 KB |
Output is correct |
83 |
Correct |
44 ms |
1532 KB |
Output is correct |
84 |
Correct |
78 ms |
1536 KB |
Output is correct |
85 |
Correct |
79 ms |
1368 KB |
Output is correct |
86 |
Correct |
76 ms |
1372 KB |
Output is correct |
87 |
Correct |
78 ms |
1628 KB |
Output is correct |
88 |
Correct |
85 ms |
1532 KB |
Output is correct |
89 |
Correct |
101 ms |
1372 KB |
Output is correct |
90 |
Correct |
96 ms |
1372 KB |
Output is correct |
91 |
Correct |
105 ms |
1372 KB |
Output is correct |
92 |
Correct |
67 ms |
1524 KB |
Output is correct |
93 |
Correct |
91 ms |
1524 KB |
Output is correct |
94 |
Correct |
116 ms |
1536 KB |
Output is correct |
95 |
Correct |
151 ms |
1528 KB |
Output is correct |
96 |
Correct |
174 ms |
1532 KB |
Output is correct |
97 |
Correct |
245 ms |
1372 KB |
Output is correct |
98 |
Correct |
259 ms |
1532 KB |
Output is correct |
99 |
Correct |
244 ms |
1368 KB |
Output is correct |
100 |
Correct |
244 ms |
1372 KB |
Output is correct |