-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameShowMath_UVA10400.cpp
120 lines (101 loc) · 3.09 KB
/
gameShowMath_UVA10400.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <bits/stdc++.h>
using namespace std;
ifstream fin("gameShowMath_UVA10400.in");
ofstream fout("gameShowMath_UVA10400.out");
using ll = long long;
const ll maxN = 64000, maxNH = 32000;
int main()
{
string operators = "+-*/";
ll tcc; fin >> tcc;
for (ll t = 1; t <= tcc; ++t)
{
ll n; fin >> n;
vector<ll> a(n, 0);
for (ll i = 0; i <= n - 1; ++i)
{
fin >> a[i];
}
ll target; fin >> target;
vector<ll> dp0(maxN + 1, 0),
dp1(maxN + 1, 0);
vector<vector<ll>> f(n, vector<ll>(maxN + 1, 4));
dp0[a[0] + maxNH] = 1;
for (ll i = 1; i <= n - 1; ++i)
{
for (ll j = -32000; j <= 32000; ++j)
{
ll last;
// +
last = j - a[i];
if (-32000 <= last && last <= 32000)
{
if (dp0[last + maxNH] == 1)
{
dp1[j + maxNH] = 1;
f[i][j + maxNH] = 0;
}
}
// -
last = j + a[i];
if (-32000 <= last && last <= 32000)
{
if (dp0[last + maxNH] == 1)
{
dp1[j + maxNH] = 1;
f[i][j + maxNH] = 1;
}
}
// *
last = j / a[i];
if (-32000 <= last && last <= 32000 && j % a[i] == 0)
{
if (dp0[last + maxNH] == 1)
{
dp1[j + maxNH] = 1;
f[i][j + maxNH] = 2;
}
}
// /
last = j * a[i];
if (-32000 <= last && last <= 32000)
{
if (dp0[last + maxNH] == 1)
{
dp1[j + maxNH] = 1;
f[i][j + maxNH] = 3;
}
}
}
dp0 = dp1;
for (ll j = -32000; j <= 32000; ++j)
{
dp1[j + maxNH] = 0;
}
}
if (dp0[target + maxNH] == 1)
{
ll now = target;
string ansO = "";
for (int i = n - 1; i >= 1; --i)
{
ansO += operators[f[i][now + maxNH]];
if (f[i][now + maxNH] == 0) now -= a[i];
else if (f[i][now + maxNH] == 1) now += a[i];
else if (f[i][now + maxNH] == 2) now /= a[i];
else if (f[i][now + maxNH] == 3) now *= a[i];
}
for (ll i = 0; i <= n - 1; ++i)
{
if (i > 0) fout << ansO[n - 1 - i];
fout << a[i];
}
fout << '=' << target << '\n';
}
else
{
fout << "NO EXPRESSION\n";
}
}
return 0;
}