#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 3e5+5;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n;
ll ali[MAXN];
struct turn{
char a, b, c;
} t[15];
ll dp[15][15][15][305];
bool did[15][15][15][305];
bool isa(char a){
return (a >= '0' && a <= '9');
}
char table[15] = "0123456789x/-";
ll do_it(int trn, ll needed, ll atis1, ll atis2){
if(needed < 0) return 0;
if(trn == 0 && (needed == 0 || needed == 301)){return 1;}
if(did[trn][atis1][atis2][needed]){ return dp[trn][atis1][atis2][needed];}
did[trn][atis1][atis2][needed] = 1;
int fr = needed;
if(ali[trn] != -1){
if(needed == 301){
needed = ali[trn];
}else if(needed != ali[trn]){
return dp[trn][atis1][atis2][fr] = dp[trn][atis1][atis2][needed] = 0;
}
}
if(fr != needed){
if(did[trn][atis1][atis2][needed]){return dp[trn][atis1][atis2][fr] = dp[trn][atis1][atis2][needed];}
did[trn][atis1][atis2][needed] = 1;
}
turn dis = t[trn];
if(dis.c == 'h'){
if(dis.a == '?' && dis.b == '?'){
ll ans = 0;
for(int i = 0; i < 10; i++){
ans += do_it(trn - 1, (needed == 301 ? 301 : needed - 10 - atis1), i, 10 - i);
for(int j = 0; i + j < 10; j++){
ans += do_it(trn - 1, (needed == 301 ? 301 : needed - i - j), i, j);
}
}
ans += do_it(trn - 1, (needed == 301 ? 301 : needed - 10 - atis1 - atis2),10, atis1);
return dp[trn][atis1][atis2][fr] = dp[trn][atis1][atis2][needed] = ans;
}else if(dis.a == '?'){
if(dis.b == '/'){
ll ans = 0;
for(int i = 0; i < 10; i++){
ans += do_it(trn - 1, (needed == 301 ? 301: needed - 10 - atis1), i, 10 - i);
}
return dp[trn][atis1][atis2][fr] = dp[trn][atis1][atis2][needed] = ans;
}else if(dis.b == '-'){
return dp[trn][atis1][atis2][fr] = dp[trn][atis1][atis2][needed] = do_it(trn - 1, (needed == 301 ? 301: needed - 10 - atis1 - atis2), 10, atis1);
}else if(isa(dis.b)){
ll sec = dis.b - '0';
ll ans = 0;
for(int i = 0; i + sec < 10; i++){
ans += do_it(trn - 1, (needed == 301 ? 301: needed - sec - i), i, sec);
}
return dp[trn][atis1][atis2][fr] = dp[trn][atis1][atis2][needed] = ans;
}else{
return 0;
}
}else if(dis.b == '?'){
if(dis.a == 'x'){
return dp[trn][atis1][atis2][fr] = dp[trn][atis1][atis2][needed] = do_it(trn - 1, (needed == 301 ? 301: needed - 10 - atis1 - atis2), 10, atis1);
}else if(isa(dis.a)){
ll ans = 0;
ll ilk = dis.a - '0';
for(int i = 0; i + ilk < 10; i++){
ans += do_it(trn - 1,(needed == 301 ? 301: needed - ilk - i), ilk, i);
}
ans += do_it(trn - 1, (needed == 301 ? 301: needed - 10 - atis1), ilk, 10 - ilk);
return dp[trn][atis1][atis2][fr] = dp[trn][atis1][atis2][needed] = ans;
}else{
return 0;
}
}else{
if(dis.a == 'x' && dis.b == '-'){
return dp[trn][atis1][atis2][fr] = dp[trn][atis1][atis2][needed] = do_it(trn - 1, (needed == 301 ? 301: needed - 10 - atis1 - atis2), 10, atis1);
}else if(isa(dis.a) && dis.b == '/'){
ll ilk = dis.a - '0';
//cout<<atis1<<" "<<needed<<endl;
ll ans = do_it(trn - 1, (needed == 301 ? 301: needed - 10 - atis1), ilk, 10 - ilk);
//cout<<ans<<endl;
return dp[trn][atis1][atis2][fr] = dp[trn][atis1][atis2][needed] =ans;
}else if(isa(dis.a) && isa(dis.b)){
ll ilk = dis.a - '0';
ll iki = dis.b - '0';
ll ans = do_it(trn - 1, (needed == 301 ? 301: needed -ilk - iki), ilk, iki);
return dp[trn][atis1][atis2][fr] = dp[trn][atis1][atis2][needed] = ans;
}else{
return 0;
}
}
}else{
char a, b ,c;
for(int i = 0; i < (dis.a == '?' ? 11 : 1); i++){
for(int j = 0; j < (dis.b == '?' ? 12 : 1); j++){
for(int k = 0; k < (dis.c == '?' ? 13 : 1); k++){
if(dis.a == '?'){
a = table[i];
}else{
a = dis.a;
}
if(dis.b == '?'){
b = table[j];
}else{
b = dis.b;
}
if(dis.c == '?'){
c = table[k];
}else{
c = dis.c;
}
if(a == 'x'){
if(b == 'x'){
if(c == 'x'){
ll ans = do_it(trn - 1, (needed == 301 ? 301: needed - 30), 10, 10);
//cout<<trn - 1<<" "<<(needed == 301 ? 301: needed - 30)<<endl;
//cout<<ans<<endl;
dp[trn][atis1][atis2][needed] += ans;
dp[trn][atis1][atis2][fr] = dp[trn][atis1][atis2][needed];
}
else if(isa(c)){
ll deg = (int) (c - '0');
ll ans = do_it(trn - 1, (needed == 301 ? 301:needed - 20 - deg), 10, 10);
dp[trn][atis1][atis2][needed] += ans;
dp[trn][atis1][atis2][fr] = dp[trn][atis1][atis2][needed];
}
}else if(isa(b)){
if(c == '/'){
ll cur = (int)(b - '0');//xA/
ll ans = do_it(trn - 1,(needed == 301 ? 301:needed - 20), 10, cur);
//if(ans) cout<<ans<<endl;
dp[trn][atis1][atis2][needed] += ans;
dp[trn][atis1][atis2][fr] = dp[trn][atis1][atis2][needed];
}else if(c >= '0' && c <= '9'){
ll ilk = (int)(b - '0');
ll iki = (int)(c - '0');
ll ans = 0;
if(ilk + iki < 10)
ans = do_it(trn - 1, (needed == 301 ? 301: needed - 10 - ilk - iki), 10, ilk);
dp[trn][atis1][atis2][needed] += ans;
dp[trn][atis1][atis2][fr] = dp[trn][atis1][atis2][needed];
}
}
}else if(isa(a)){
if(b == '/'){
if(c == 'x'){
ll ilk = (int) (a -'0');
ll ans = do_it(trn - 1, (needed == 301 ? 301: needed - 20), ilk, 10 - ilk);
//cout<<trn<<" "<<a<<" "<<b<<" "<<c<<" "<<ans<<endl;
dp[trn][atis1][atis2][needed] += ans;
dp[trn][atis1][atis2][fr] = dp[trn][atis1][atis2][needed];
}else if(isa(c)){
ll ilk = (int) (a - '0');
ll iki = (int) (c - '0');
ll ans = do_it(trn - 1, (needed == 301 ? 301: needed - 10 - iki), ilk, 10 - ilk);
// cout<<a<<b<<c<<" "<<ans<<endl;
dp[trn][atis1][atis2][needed] += ans;
dp[trn][atis1][atis2][fr] = dp[trn][atis1][atis2][needed];
}
}else if(isa(b) && c == '-'){
ll ilk = (int) (a - '0');
ll iki = (int) (b - '0');
ll ans = 0;
if(ilk + iki < 10){
ans = do_it(trn - 1, (needed == 301 ? 301:needed - ilk - iki), ilk, iki);
}
dp[trn][atis1][atis2][needed] += ans;
dp[trn][atis1][atis2][fr] = dp[trn][atis1][atis2][needed];
}
}
}
}
}
return dp[trn][atis1][atis2][fr] = dp[trn][atis1][atis2][needed] ;
}
}
void solve(){
ll n;
cin>>n;
string s;
cin>>s;
for(int i = 1; i <= n; i++){
cin>>ali[i];
}
for(int i = 0; i <= 10; i++){
for(int j = 0; j <= 14; j++){
for(int k = 0; k <= 14; k++){
for(int l = 0; l <= 302; l++){
dp[i][j][k][l] = 0;
did[i][j][k][l] = 0;
}
}
}
}
for(int i = 0; i < 2*n - 2; i += 2){
t[i/2 + 1] = {s[i], s[i + 1], 'h'};
}
t[n] = {s[2*n - 2],s[2*n - 1], s[2*n]};
cout<<do_it(n, 301, 0, 0)<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
#ifdef Local
freopen("../IO/int.txt","r",stdin);
freopen("../IO/out.txt","w",stdout);
#endif
int q;
cin>>q;
while(q--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
7160 KB |
Output is correct |
2 |
Correct |
41 ms |
7032 KB |
Output is correct |
3 |
Correct |
41 ms |
7032 KB |
Output is correct |
4 |
Correct |
40 ms |
7032 KB |
Output is correct |
5 |
Correct |
40 ms |
7032 KB |
Output is correct |
6 |
Correct |
41 ms |
7048 KB |
Output is correct |
7 |
Correct |
41 ms |
7032 KB |
Output is correct |
8 |
Correct |
40 ms |
7032 KB |
Output is correct |
9 |
Correct |
41 ms |
7032 KB |
Output is correct |
10 |
Correct |
40 ms |
7032 KB |
Output is correct |
11 |
Correct |
40 ms |
7032 KB |
Output is correct |
12 |
Correct |
41 ms |
7032 KB |
Output is correct |
13 |
Correct |
15 ms |
7032 KB |
Output is correct |
14 |
Correct |
43 ms |
7032 KB |
Output is correct |
15 |
Correct |
16 ms |
7032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
7032 KB |
Output is correct |
2 |
Correct |
49 ms |
7032 KB |
Output is correct |
3 |
Correct |
52 ms |
7084 KB |
Output is correct |
4 |
Correct |
46 ms |
7032 KB |
Output is correct |
5 |
Correct |
46 ms |
7032 KB |
Output is correct |
6 |
Correct |
50 ms |
7032 KB |
Output is correct |
7 |
Correct |
51 ms |
7032 KB |
Output is correct |
8 |
Correct |
54 ms |
7032 KB |
Output is correct |
9 |
Correct |
46 ms |
7032 KB |
Output is correct |
10 |
Correct |
79 ms |
7032 KB |
Output is correct |
11 |
Correct |
100 ms |
7032 KB |
Output is correct |
12 |
Correct |
73 ms |
7032 KB |
Output is correct |
13 |
Correct |
95 ms |
7032 KB |
Output is correct |
14 |
Correct |
55 ms |
7032 KB |
Output is correct |
15 |
Correct |
51 ms |
7044 KB |
Output is correct |
16 |
Correct |
51 ms |
7032 KB |
Output is correct |
17 |
Correct |
52 ms |
7032 KB |
Output is correct |
18 |
Correct |
95 ms |
7160 KB |
Output is correct |
19 |
Correct |
103 ms |
7032 KB |
Output is correct |
20 |
Correct |
107 ms |
7032 KB |
Output is correct |
21 |
Correct |
109 ms |
7032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
7040 KB |
Output is correct |
2 |
Correct |
43 ms |
7032 KB |
Output is correct |
3 |
Correct |
43 ms |
7036 KB |
Output is correct |
4 |
Correct |
43 ms |
7032 KB |
Output is correct |
5 |
Correct |
40 ms |
7088 KB |
Output is correct |
6 |
Correct |
42 ms |
7036 KB |
Output is correct |
7 |
Correct |
41 ms |
7032 KB |
Output is correct |
8 |
Correct |
46 ms |
7032 KB |
Output is correct |
9 |
Correct |
43 ms |
7032 KB |
Output is correct |
10 |
Correct |
44 ms |
7032 KB |
Output is correct |
11 |
Correct |
43 ms |
7032 KB |
Output is correct |
12 |
Correct |
43 ms |
7036 KB |
Output is correct |
13 |
Correct |
44 ms |
7032 KB |
Output is correct |
14 |
Correct |
43 ms |
7032 KB |
Output is correct |
15 |
Correct |
43 ms |
7032 KB |
Output is correct |
16 |
Correct |
43 ms |
7032 KB |
Output is correct |
17 |
Correct |
46 ms |
7096 KB |
Output is correct |
18 |
Correct |
43 ms |
7032 KB |
Output is correct |
19 |
Correct |
41 ms |
7032 KB |
Output is correct |
20 |
Correct |
42 ms |
7044 KB |
Output is correct |
21 |
Correct |
41 ms |
7032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
7136 KB |
Output is correct |
2 |
Correct |
47 ms |
7032 KB |
Output is correct |
3 |
Correct |
44 ms |
7032 KB |
Output is correct |
4 |
Correct |
41 ms |
7032 KB |
Output is correct |
5 |
Correct |
43 ms |
7032 KB |
Output is correct |
6 |
Correct |
44 ms |
7032 KB |
Output is correct |
7 |
Correct |
45 ms |
7160 KB |
Output is correct |
8 |
Correct |
44 ms |
7032 KB |
Output is correct |
9 |
Correct |
46 ms |
7032 KB |
Output is correct |
10 |
Correct |
58 ms |
7032 KB |
Output is correct |
11 |
Correct |
54 ms |
7032 KB |
Output is correct |
12 |
Correct |
54 ms |
7032 KB |
Output is correct |
13 |
Correct |
51 ms |
7032 KB |
Output is correct |
14 |
Correct |
42 ms |
7032 KB |
Output is correct |
15 |
Correct |
43 ms |
7032 KB |
Output is correct |
16 |
Correct |
47 ms |
7032 KB |
Output is correct |
17 |
Correct |
44 ms |
7032 KB |
Output is correct |
18 |
Correct |
41 ms |
7032 KB |
Output is correct |
19 |
Correct |
41 ms |
7032 KB |
Output is correct |
20 |
Correct |
44 ms |
7032 KB |
Output is correct |
21 |
Correct |
40 ms |
7032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
7160 KB |
Output is correct |
2 |
Correct |
41 ms |
7032 KB |
Output is correct |
3 |
Correct |
41 ms |
7032 KB |
Output is correct |
4 |
Correct |
40 ms |
7032 KB |
Output is correct |
5 |
Correct |
40 ms |
7032 KB |
Output is correct |
6 |
Correct |
41 ms |
7048 KB |
Output is correct |
7 |
Correct |
41 ms |
7032 KB |
Output is correct |
8 |
Correct |
40 ms |
7032 KB |
Output is correct |
9 |
Correct |
41 ms |
7032 KB |
Output is correct |
10 |
Correct |
40 ms |
7032 KB |
Output is correct |
11 |
Correct |
40 ms |
7032 KB |
Output is correct |
12 |
Correct |
41 ms |
7032 KB |
Output is correct |
13 |
Correct |
15 ms |
7032 KB |
Output is correct |
14 |
Correct |
43 ms |
7032 KB |
Output is correct |
15 |
Correct |
16 ms |
7032 KB |
Output is correct |
16 |
Correct |
30 ms |
7032 KB |
Output is correct |
17 |
Correct |
49 ms |
7032 KB |
Output is correct |
18 |
Correct |
52 ms |
7084 KB |
Output is correct |
19 |
Correct |
46 ms |
7032 KB |
Output is correct |
20 |
Correct |
46 ms |
7032 KB |
Output is correct |
21 |
Correct |
50 ms |
7032 KB |
Output is correct |
22 |
Correct |
51 ms |
7032 KB |
Output is correct |
23 |
Correct |
54 ms |
7032 KB |
Output is correct |
24 |
Correct |
46 ms |
7032 KB |
Output is correct |
25 |
Correct |
79 ms |
7032 KB |
Output is correct |
26 |
Correct |
100 ms |
7032 KB |
Output is correct |
27 |
Correct |
73 ms |
7032 KB |
Output is correct |
28 |
Correct |
95 ms |
7032 KB |
Output is correct |
29 |
Correct |
55 ms |
7032 KB |
Output is correct |
30 |
Correct |
51 ms |
7044 KB |
Output is correct |
31 |
Correct |
51 ms |
7032 KB |
Output is correct |
32 |
Correct |
52 ms |
7032 KB |
Output is correct |
33 |
Correct |
95 ms |
7160 KB |
Output is correct |
34 |
Correct |
103 ms |
7032 KB |
Output is correct |
35 |
Correct |
107 ms |
7032 KB |
Output is correct |
36 |
Correct |
109 ms |
7032 KB |
Output is correct |
37 |
Correct |
21 ms |
7040 KB |
Output is correct |
38 |
Correct |
43 ms |
7032 KB |
Output is correct |
39 |
Correct |
43 ms |
7036 KB |
Output is correct |
40 |
Correct |
43 ms |
7032 KB |
Output is correct |
41 |
Correct |
40 ms |
7088 KB |
Output is correct |
42 |
Correct |
42 ms |
7036 KB |
Output is correct |
43 |
Correct |
41 ms |
7032 KB |
Output is correct |
44 |
Correct |
46 ms |
7032 KB |
Output is correct |
45 |
Correct |
43 ms |
7032 KB |
Output is correct |
46 |
Correct |
44 ms |
7032 KB |
Output is correct |
47 |
Correct |
43 ms |
7032 KB |
Output is correct |
48 |
Correct |
43 ms |
7036 KB |
Output is correct |
49 |
Correct |
44 ms |
7032 KB |
Output is correct |
50 |
Correct |
43 ms |
7032 KB |
Output is correct |
51 |
Correct |
43 ms |
7032 KB |
Output is correct |
52 |
Correct |
43 ms |
7032 KB |
Output is correct |
53 |
Correct |
46 ms |
7096 KB |
Output is correct |
54 |
Correct |
43 ms |
7032 KB |
Output is correct |
55 |
Correct |
41 ms |
7032 KB |
Output is correct |
56 |
Correct |
42 ms |
7044 KB |
Output is correct |
57 |
Correct |
41 ms |
7032 KB |
Output is correct |
58 |
Correct |
39 ms |
7136 KB |
Output is correct |
59 |
Correct |
47 ms |
7032 KB |
Output is correct |
60 |
Correct |
44 ms |
7032 KB |
Output is correct |
61 |
Correct |
41 ms |
7032 KB |
Output is correct |
62 |
Correct |
43 ms |
7032 KB |
Output is correct |
63 |
Correct |
44 ms |
7032 KB |
Output is correct |
64 |
Correct |
45 ms |
7160 KB |
Output is correct |
65 |
Correct |
44 ms |
7032 KB |
Output is correct |
66 |
Correct |
46 ms |
7032 KB |
Output is correct |
67 |
Correct |
58 ms |
7032 KB |
Output is correct |
68 |
Correct |
54 ms |
7032 KB |
Output is correct |
69 |
Correct |
54 ms |
7032 KB |
Output is correct |
70 |
Correct |
51 ms |
7032 KB |
Output is correct |
71 |
Correct |
42 ms |
7032 KB |
Output is correct |
72 |
Correct |
43 ms |
7032 KB |
Output is correct |
73 |
Correct |
47 ms |
7032 KB |
Output is correct |
74 |
Correct |
44 ms |
7032 KB |
Output is correct |
75 |
Correct |
41 ms |
7032 KB |
Output is correct |
76 |
Correct |
41 ms |
7032 KB |
Output is correct |
77 |
Correct |
44 ms |
7032 KB |
Output is correct |
78 |
Correct |
40 ms |
7032 KB |
Output is correct |
79 |
Correct |
35 ms |
7032 KB |
Output is correct |
80 |
Correct |
57 ms |
7032 KB |
Output is correct |
81 |
Correct |
60 ms |
7032 KB |
Output is correct |
82 |
Correct |
45 ms |
7032 KB |
Output is correct |
83 |
Correct |
50 ms |
7032 KB |
Output is correct |
84 |
Correct |
42 ms |
7032 KB |
Output is correct |
85 |
Correct |
48 ms |
7032 KB |
Output is correct |
86 |
Correct |
41 ms |
7032 KB |
Output is correct |
87 |
Correct |
43 ms |
7032 KB |
Output is correct |
88 |
Correct |
60 ms |
7032 KB |
Output is correct |
89 |
Correct |
86 ms |
7032 KB |
Output is correct |
90 |
Correct |
81 ms |
7032 KB |
Output is correct |
91 |
Correct |
77 ms |
7032 KB |
Output is correct |
92 |
Correct |
41 ms |
7032 KB |
Output is correct |
93 |
Correct |
74 ms |
7032 KB |
Output is correct |
94 |
Correct |
190 ms |
7164 KB |
Output is correct |
95 |
Correct |
306 ms |
7052 KB |
Output is correct |
96 |
Correct |
405 ms |
7164 KB |
Output is correct |
97 |
Correct |
700 ms |
7048 KB |
Output is correct |
98 |
Correct |
739 ms |
7160 KB |
Output is correct |
99 |
Correct |
729 ms |
7160 KB |
Output is correct |
100 |
Correct |
732 ms |
7160 KB |
Output is correct |