-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
596926f
commit dbe6702
Showing
6 changed files
with
336 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
46 | ||
1 25900 199 | ||
4 22216 18921 1287 1689 2248 | ||
2 26738 7299 1812 | ||
6 11907 20741 27246 41 339 164 2372 | ||
10 25309 24890 2180 11127 14424 12280 12282 270 8133 172 363 | ||
4 17348 4672 27143 23024 861 | ||
7 11405 377 169 19245 20536 966 16968 668 | ||
7 19556 9058 1764 24220 23977 26390 11143 786 | ||
6 6698 5761 29285 2498 2723 13320 1319 | ||
10 10115 23697 1127 4305 5378 11414 22545 8935 11968 2455 2959 | ||
3 26218 815 1545 2110 | ||
1 10376 2448 | ||
3 6549 22849 16359 1917 | ||
4 1567 4927 24725 802 1339 | ||
5 18181 25567 10058 2466 979 1617 | ||
7 1685 5848 2718 12747 17127 4431 1760 2670 | ||
1 10297 1741 | ||
7 19116 17920 1874 1007 16248 24410 28785 2929 | ||
2 3952 19561 2254 | ||
8 16192 14466 21362 22063 23615 17511 27989 7439 2515 | ||
6 4560 29692 15521 10338 6786 20649 1704 | ||
3 19994 11928 21212 2202 | ||
2 21717 21166 1797 | ||
7 9188 5067 833 10705 2760 26090 26897 1384 | ||
10 22365 3269 26896 9560 26866 12546 27566 24774 14615 21641 2955 | ||
10 27201 3855 1768 15955 18574 12361 14921 7013 29353 21830 1117 | ||
3 2542 26625 26647 1474 | ||
6 15671 6192 17246 18037 17526 13588 2509 | ||
9 2375 61 1836 11512 11693 2990 28157 15372 29982 655 | ||
6 16989 22898 2004 4752 24850 22601 2300 | ||
5 8736 2514 1386 4935 23987 15 | ||
7 1793 3791 23457 8425 1160 23351 16051 2280 | ||
5 21134 9461 12220 7712 10428 1683 | ||
4 24787 28563 25513 10964 761 | ||
3 3561 29309 21708 836 | ||
4 10324 14710 4192 738 1716 | ||
2 10675 1647 1230 | ||
7 12894 29279 13489 11371 751 28554 24612 574 | ||
7 18073 29509 3432 4983 9496 24895 1111 2158 | ||
10 19859 19023 24020 5446 11100 15498 13470 6072 25047 26853 932 | ||
4 6690 13460 7143 7715 672 | ||
7 2756 1684 20846 22882 14826 22869 1105 650 | ||
10 14308 29135 29123 16568 8090 14118 19866 17894 28985 7435 433 | ||
1 5828 1133 | ||
8 8247 15251 19096 6385 8092 19110 4057 8955 2466 | ||
2 27683 19793 2559 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION | ||
NO EXPRESSION |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
|
||
using ll = long long; | ||
|
||
const ll maxN = 64000, maxNH = 32000; | ||
|
||
int main() | ||
{ | ||
ios_base::sync_with_stdio(false); | ||
std::cin.tie(NULL); | ||
string operators = "+-*/"; | ||
|
||
ll tcc; cin >> tcc; | ||
for (ll t = 1; t <= tcc; ++t) | ||
{ | ||
ll n; cin >> n; | ||
vector<ll> a(n, 0); | ||
for (ll i = 0; i <= n - 1; ++i) | ||
{ | ||
cin >> a[i]; | ||
} | ||
|
||
ll target; cin >> 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) cout << ansO[n - 1 - i]; | ||
cout << a[i]; | ||
} | ||
cout << '=' << target << '\n'; | ||
} | ||
else | ||
{ | ||
cout << "NO EXPRESSION\n"; | ||
} | ||
} | ||
|
||
cout.flush(); | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters