#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma")
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
ll dp[10][66][66][331], pre[10][66][331], sum[66][331];
int cost[66][66][66], cost223[66][66][331], cost23[66][331], cost3[331], can[11][331];
const int s2 = 66, s3 = 241;
string mp2[s2], mp3[s3];
int _cost(string s, string t)
{
int a = -1, b = -1;
for (auto c : t)
{
if (c == 'x')
(a == -1 ? a : b) = 10;
else if (c == '/')
b = 10 - a;
else if (isdigit(c))
(a == -1 ? a : b) = c - '0';
if (b >= 0)
break;
}
a = max(0, a), b = max(0, b);
if (s[0] == 'x')
return 10 + a + b;
else if (s[1] == '/')
return 10 + a;
else
return s[0] + s[1] - 2 * '0';
}
bool match(string s, string t)
{
for (int i = 0; i < s.size(); i++)
if (s[i] != t[i] && s[i] != '?')
return 0;
return 1;
}
void solve()
{
string s;
int n, ans[13] = {};
cin >> n >> s;
for (int i = 1; i <= n; i++)
cin >> ans[i + 2];
dp[0][0][0][0] = 1;
for (int i = 1; i <= 9; i++)
for (int j = 0; j < s2; j++)
for (int k = 0; k < s2; k++)
for (int sc = 0; sc <= 330; sc++)
dp[i][j][k][sc] = 0;
for (int i = 1; i <= n - 1; i++)
for (int j = 0; j < s2; j++)
for (int sc = 0; sc <= 330; sc++)
pre[i][j][sc] = sum[j][sc] = 0;
for (int i = 0; i <= n - 2; i++)
{
string t = s.substr(2 * i, 2);
for (int l = 0; l < s2; l++)
can[i][l] = match(t, mp2[l]);
}
for (int i = 0; i <= n - 2; i++)
for (int j = 0; j < s2; j++)
for (int k = 0; k < s2; k++)
if (j == 65 && k == 65)
{
for (int sc = 0; sc <= 330; sc++)
{
if (i > 0 && can[i - 1][k])
dp[i][j][k][sc] += pre[i][j][sc];
if (dp[i][j][k][sc] != 0)
for (int l = 0; l < s2; l++)
if (can[i][l] && (sc + cost[j][k][l] == ans[i + 1] || ans[i + 1] == -1))
dp[i + 1][k][l][sc + cost[j][k][l]] += dp[i][j][k][sc];
}
}
else
{
int l, r;
if (ans[i + 1] == -1)
l = 0, r = 330 - cost[j][k][0];
else
l = max(0, ans[i + 1] - cost[j][k][0]), r = ans[i + 1] - cost[j][k][0];
for (int sc = l; sc <= r; sc++)
{
if (i > 0 && can[i - 1][k])
dp[i][j][k][sc] += pre[i][j][sc];
pre[i + 1][k][sc + cost[j][k][0]] += dp[i][j][k][sc];
}
}
ll res = 0;
for (int l = 0; l < s3; l++)
can[n - 1][l] = match(s.substr(2 * n - 2, 3), mp3[l]);
for (int j = 0; j < s2; j++)
for (int k = 0; k < s2; k++)
if (j == 65 && k == 65)
{
for (int sc = 0; sc <= 330; sc++)
{
if (can[n - 2][k])
dp[n - 1][j][k][sc] += pre[n - 1][j][sc];
if (dp[n - 1][j][k][sc] != 0)
for (int l = 0; l < s3; l++)
if (can[n - 1][l])
{
int an2 = cost223[j][k][l], an1 = an2 + cost23[k][l], an = an1 + cost3[l];
if ((sc + an2 == ans[n] || ans[n] == -1) &&
(sc + an1 == ans[n + 1] || ans[n + 1] == -1) &&
(sc + an == ans[n + 2] || ans[n + 2] == -1))
res += dp[n - 1][j][k][sc];
}
}
}
else
{
for (int sc = 0; sc <= 330 - cost223[j][k][0]; sc++)
{
if (can[n - 2][k])
dp[n - 1][j][k][sc] += pre[n - 1][j][sc];
if ((sc + cost223[j][k][0] == ans[n] || ans[n] == -1))
sum[k][sc + cost223[j][k][0]] += dp[n - 1][j][k][sc];
dp[n - 1][j][k][sc] = 0;
}
}
for (int k = 0; k < s2; k++)
for (int sc = 0; sc <= 330; sc++)
if (sum[k][sc] != 0)
for (int l = 0; l < s3; l++)
if (can[n - 1][l])
{
int an1 = cost23[k][l], an = an1 + cost3[l];
if ((sc + an1 == ans[n + 1] || ans[n + 1] == -1) &&
(sc + an == ans[n + 2] || ans[n + 2] == -1))
res += sum[k][sc];
}
cout << res << '\n';
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int idx = 0;
for (int i = 0; i <= 10; i++)
for (int j = 0; j <= 10 - i; j++)
{
string s = (i == 10 ? "x-" : (i + j == 10 ? string(1, '0' + i) + "/" : string(1, '0' + i) + string(1, '0' + j)));
mp2[idx] = s;
idx++;
}
idx = 0;
for (int i = 0; i <= 10; i++)
for (int j = 0; j <= (i == 10 ? 10 : 10 - i); j++)
for (int k = 0; k <= (i == 10 ? (j == 10 ? 10 : 10 - j) : (i + j == 10 ? 10 : 0)); k++)
{
string s = (i == 10 ? "x" : string(1, '0' + i)) +
(j == 10 ? "x" : string(1, '0' + j)) +
(k == 10 ? "x" : string(1, '0' + k));
if (i < 10 && i + j == 10)
s[1] = '/';
if (i == 10 && j < 10 && j + k == 10)
s[2] = '/';
if (i + j < 10)
s[2] = '-';
mp3[idx] = s;
idx++;
}
for (int j = 0; j < s2; j++)
for (int k = 0; k < s2; k++)
for (int l = 0; l < s2; l++)
{
cost[j][k][l] = _cost(mp2[j], mp2[k] + mp2[l]);
}
for (int j = 0; j < s2; j++)
for (int k = 0; k < s2; k++)
for (int l = 0; l < s3; l++)
cost223[j][k][l] = _cost(mp2[j], mp2[k] + mp3[l]);
for (int k = 0; k < s2; k++)
for (int l = 0; l < s3; l++)
cost23[k][l] = _cost(mp2[k], mp3[l]);
for (int l = 0; l < s3; l++)
{
int &an = cost3[l];
if (mp3[l][1] == '/')
{
an += 10;
if (mp3[l][2] != '-')
an += (mp3[l][2] == 'x' ? 10 : mp3[l][2] - '0');
}
else
{
an += (mp3[l][0] == 'x' ? 10 : mp3[l][0] - '0');
if (mp3[l][2] == '/')
an += 10;
else
{
an += (mp3[l][1] == 'x' ? 10 : mp3[l][1] - '0');
if (mp3[l][2] != '-')
an += (mp3[l][2] == 'x' ? 10 : mp3[l][2] - '0');
}
}
}
int q;
cin >> q;
while (q--)
solve();
}
Compilation message
bow.cpp: In function 'bool match(std::string, std::string)':
bow.cpp:42:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (int i = 0; i < s.size(); i++)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
440 ms |
110484 KB |
Output is correct |
2 |
Correct |
447 ms |
110312 KB |
Output is correct |
3 |
Correct |
375 ms |
110272 KB |
Output is correct |
4 |
Correct |
380 ms |
110604 KB |
Output is correct |
5 |
Correct |
386 ms |
110520 KB |
Output is correct |
6 |
Correct |
390 ms |
110544 KB |
Output is correct |
7 |
Correct |
592 ms |
110484 KB |
Output is correct |
8 |
Correct |
529 ms |
110480 KB |
Output is correct |
9 |
Correct |
504 ms |
110520 KB |
Output is correct |
10 |
Correct |
544 ms |
110476 KB |
Output is correct |
11 |
Correct |
599 ms |
110560 KB |
Output is correct |
12 |
Correct |
379 ms |
109144 KB |
Output is correct |
13 |
Correct |
136 ms |
109060 KB |
Output is correct |
14 |
Correct |
384 ms |
109104 KB |
Output is correct |
15 |
Correct |
143 ms |
108996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
345 ms |
110476 KB |
Output is correct |
2 |
Correct |
476 ms |
110456 KB |
Output is correct |
3 |
Correct |
446 ms |
110412 KB |
Output is correct |
4 |
Correct |
469 ms |
110476 KB |
Output is correct |
5 |
Correct |
481 ms |
110580 KB |
Output is correct |
6 |
Correct |
537 ms |
110480 KB |
Output is correct |
7 |
Correct |
536 ms |
110480 KB |
Output is correct |
8 |
Correct |
547 ms |
110484 KB |
Output is correct |
9 |
Correct |
508 ms |
110504 KB |
Output is correct |
10 |
Correct |
584 ms |
110488 KB |
Output is correct |
11 |
Correct |
577 ms |
110412 KB |
Output is correct |
12 |
Correct |
574 ms |
110476 KB |
Output is correct |
13 |
Correct |
584 ms |
110480 KB |
Output is correct |
14 |
Correct |
467 ms |
110480 KB |
Output is correct |
15 |
Correct |
474 ms |
110412 KB |
Output is correct |
16 |
Correct |
498 ms |
110488 KB |
Output is correct |
17 |
Correct |
482 ms |
110432 KB |
Output is correct |
18 |
Correct |
656 ms |
110480 KB |
Output is correct |
19 |
Correct |
661 ms |
110476 KB |
Output is correct |
20 |
Correct |
644 ms |
110480 KB |
Output is correct |
21 |
Correct |
657 ms |
110484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
229 ms |
110476 KB |
Output is correct |
2 |
Correct |
398 ms |
110524 KB |
Output is correct |
3 |
Correct |
404 ms |
110412 KB |
Output is correct |
4 |
Correct |
438 ms |
110476 KB |
Output is correct |
5 |
Correct |
418 ms |
110476 KB |
Output is correct |
6 |
Correct |
434 ms |
110504 KB |
Output is correct |
7 |
Correct |
438 ms |
110480 KB |
Output is correct |
8 |
Correct |
431 ms |
110368 KB |
Output is correct |
9 |
Correct |
449 ms |
110480 KB |
Output is correct |
10 |
Correct |
416 ms |
110412 KB |
Output is correct |
11 |
Correct |
412 ms |
110480 KB |
Output is correct |
12 |
Correct |
402 ms |
110552 KB |
Output is correct |
13 |
Correct |
413 ms |
110472 KB |
Output is correct |
14 |
Correct |
437 ms |
110412 KB |
Output is correct |
15 |
Correct |
434 ms |
110480 KB |
Output is correct |
16 |
Correct |
453 ms |
110496 KB |
Output is correct |
17 |
Correct |
439 ms |
110476 KB |
Output is correct |
18 |
Correct |
471 ms |
110484 KB |
Output is correct |
19 |
Correct |
520 ms |
110432 KB |
Output is correct |
20 |
Correct |
500 ms |
110540 KB |
Output is correct |
21 |
Correct |
509 ms |
110540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
389 ms |
110476 KB |
Output is correct |
2 |
Correct |
433 ms |
110524 KB |
Output is correct |
3 |
Correct |
458 ms |
110452 KB |
Output is correct |
4 |
Correct |
448 ms |
110420 KB |
Output is correct |
5 |
Correct |
431 ms |
110284 KB |
Output is correct |
6 |
Correct |
415 ms |
110484 KB |
Output is correct |
7 |
Correct |
414 ms |
110496 KB |
Output is correct |
8 |
Correct |
414 ms |
110476 KB |
Output is correct |
9 |
Correct |
419 ms |
110568 KB |
Output is correct |
10 |
Correct |
546 ms |
110540 KB |
Output is correct |
11 |
Correct |
518 ms |
110364 KB |
Output is correct |
12 |
Correct |
513 ms |
110500 KB |
Output is correct |
13 |
Correct |
514 ms |
110476 KB |
Output is correct |
14 |
Correct |
546 ms |
110480 KB |
Output is correct |
15 |
Correct |
545 ms |
110480 KB |
Output is correct |
16 |
Correct |
552 ms |
110408 KB |
Output is correct |
17 |
Correct |
526 ms |
110412 KB |
Output is correct |
18 |
Correct |
540 ms |
110480 KB |
Output is correct |
19 |
Correct |
557 ms |
110540 KB |
Output is correct |
20 |
Correct |
608 ms |
110644 KB |
Output is correct |
21 |
Correct |
604 ms |
110560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
440 ms |
110484 KB |
Output is correct |
2 |
Correct |
447 ms |
110312 KB |
Output is correct |
3 |
Correct |
375 ms |
110272 KB |
Output is correct |
4 |
Correct |
380 ms |
110604 KB |
Output is correct |
5 |
Correct |
386 ms |
110520 KB |
Output is correct |
6 |
Correct |
390 ms |
110544 KB |
Output is correct |
7 |
Correct |
592 ms |
110484 KB |
Output is correct |
8 |
Correct |
529 ms |
110480 KB |
Output is correct |
9 |
Correct |
504 ms |
110520 KB |
Output is correct |
10 |
Correct |
544 ms |
110476 KB |
Output is correct |
11 |
Correct |
599 ms |
110560 KB |
Output is correct |
12 |
Correct |
379 ms |
109144 KB |
Output is correct |
13 |
Correct |
136 ms |
109060 KB |
Output is correct |
14 |
Correct |
384 ms |
109104 KB |
Output is correct |
15 |
Correct |
143 ms |
108996 KB |
Output is correct |
16 |
Correct |
345 ms |
110476 KB |
Output is correct |
17 |
Correct |
476 ms |
110456 KB |
Output is correct |
18 |
Correct |
446 ms |
110412 KB |
Output is correct |
19 |
Correct |
469 ms |
110476 KB |
Output is correct |
20 |
Correct |
481 ms |
110580 KB |
Output is correct |
21 |
Correct |
537 ms |
110480 KB |
Output is correct |
22 |
Correct |
536 ms |
110480 KB |
Output is correct |
23 |
Correct |
547 ms |
110484 KB |
Output is correct |
24 |
Correct |
508 ms |
110504 KB |
Output is correct |
25 |
Correct |
584 ms |
110488 KB |
Output is correct |
26 |
Correct |
577 ms |
110412 KB |
Output is correct |
27 |
Correct |
574 ms |
110476 KB |
Output is correct |
28 |
Correct |
584 ms |
110480 KB |
Output is correct |
29 |
Correct |
467 ms |
110480 KB |
Output is correct |
30 |
Correct |
474 ms |
110412 KB |
Output is correct |
31 |
Correct |
498 ms |
110488 KB |
Output is correct |
32 |
Correct |
482 ms |
110432 KB |
Output is correct |
33 |
Correct |
656 ms |
110480 KB |
Output is correct |
34 |
Correct |
661 ms |
110476 KB |
Output is correct |
35 |
Correct |
644 ms |
110480 KB |
Output is correct |
36 |
Correct |
657 ms |
110484 KB |
Output is correct |
37 |
Correct |
229 ms |
110476 KB |
Output is correct |
38 |
Correct |
398 ms |
110524 KB |
Output is correct |
39 |
Correct |
404 ms |
110412 KB |
Output is correct |
40 |
Correct |
438 ms |
110476 KB |
Output is correct |
41 |
Correct |
418 ms |
110476 KB |
Output is correct |
42 |
Correct |
434 ms |
110504 KB |
Output is correct |
43 |
Correct |
438 ms |
110480 KB |
Output is correct |
44 |
Correct |
431 ms |
110368 KB |
Output is correct |
45 |
Correct |
449 ms |
110480 KB |
Output is correct |
46 |
Correct |
416 ms |
110412 KB |
Output is correct |
47 |
Correct |
412 ms |
110480 KB |
Output is correct |
48 |
Correct |
402 ms |
110552 KB |
Output is correct |
49 |
Correct |
413 ms |
110472 KB |
Output is correct |
50 |
Correct |
437 ms |
110412 KB |
Output is correct |
51 |
Correct |
434 ms |
110480 KB |
Output is correct |
52 |
Correct |
453 ms |
110496 KB |
Output is correct |
53 |
Correct |
439 ms |
110476 KB |
Output is correct |
54 |
Correct |
471 ms |
110484 KB |
Output is correct |
55 |
Correct |
520 ms |
110432 KB |
Output is correct |
56 |
Correct |
500 ms |
110540 KB |
Output is correct |
57 |
Correct |
509 ms |
110540 KB |
Output is correct |
58 |
Correct |
389 ms |
110476 KB |
Output is correct |
59 |
Correct |
433 ms |
110524 KB |
Output is correct |
60 |
Correct |
458 ms |
110452 KB |
Output is correct |
61 |
Correct |
448 ms |
110420 KB |
Output is correct |
62 |
Correct |
431 ms |
110284 KB |
Output is correct |
63 |
Correct |
415 ms |
110484 KB |
Output is correct |
64 |
Correct |
414 ms |
110496 KB |
Output is correct |
65 |
Correct |
414 ms |
110476 KB |
Output is correct |
66 |
Correct |
419 ms |
110568 KB |
Output is correct |
67 |
Correct |
546 ms |
110540 KB |
Output is correct |
68 |
Correct |
518 ms |
110364 KB |
Output is correct |
69 |
Correct |
513 ms |
110500 KB |
Output is correct |
70 |
Correct |
514 ms |
110476 KB |
Output is correct |
71 |
Correct |
546 ms |
110480 KB |
Output is correct |
72 |
Correct |
545 ms |
110480 KB |
Output is correct |
73 |
Correct |
552 ms |
110408 KB |
Output is correct |
74 |
Correct |
526 ms |
110412 KB |
Output is correct |
75 |
Correct |
540 ms |
110480 KB |
Output is correct |
76 |
Correct |
557 ms |
110540 KB |
Output is correct |
77 |
Correct |
608 ms |
110644 KB |
Output is correct |
78 |
Correct |
604 ms |
110560 KB |
Output is correct |
79 |
Correct |
291 ms |
110480 KB |
Output is correct |
80 |
Correct |
491 ms |
110484 KB |
Output is correct |
81 |
Correct |
459 ms |
110480 KB |
Output is correct |
82 |
Correct |
477 ms |
110372 KB |
Output is correct |
83 |
Correct |
448 ms |
110540 KB |
Output is correct |
84 |
Correct |
430 ms |
110476 KB |
Output is correct |
85 |
Correct |
425 ms |
110480 KB |
Output is correct |
86 |
Correct |
416 ms |
110540 KB |
Output is correct |
87 |
Correct |
411 ms |
110536 KB |
Output is correct |
88 |
Correct |
543 ms |
110484 KB |
Output is correct |
89 |
Correct |
559 ms |
110412 KB |
Output is correct |
90 |
Correct |
550 ms |
110508 KB |
Output is correct |
91 |
Correct |
566 ms |
110460 KB |
Output is correct |
92 |
Correct |
727 ms |
110444 KB |
Output is correct |
93 |
Correct |
847 ms |
110504 KB |
Output is correct |
94 |
Correct |
855 ms |
110540 KB |
Output is correct |
95 |
Correct |
862 ms |
110508 KB |
Output is correct |
96 |
Correct |
863 ms |
110384 KB |
Output is correct |
97 |
Correct |
859 ms |
110488 KB |
Output is correct |
98 |
Correct |
868 ms |
110524 KB |
Output is correct |
99 |
Correct |
843 ms |
110500 KB |
Output is correct |
100 |
Correct |
842 ms |
110484 KB |
Output is correct |