# UVa 10400 - Game Show Math

求是否能是的計算的結果為題目給定的數字（除法要餘數為零才能進行）。

每次將當前所有可行解與下一個數字進行四種則運算，結果在範圍內即為新的可行解。

#include <cstring>
#include <cstdio>

#define formap(x) ((x)+32000)
#define revmap(x) ((x)-32000)
#define range(L, x, R) ((x) >= L && (x) <= R)

int  f[101][64004], p[101][64004], value[101];
char o[101][64004];

int save(int id, int v, int parent, char operat)
{
f[id][v] = 1;
p[id][v] = parent;
o[id][v] = operat;
}

int output(int id, int v)
{
if (id > 1) {
output(id-1, p[id][v]);
printf("%c%d",o[id][v],value[id]);
}else {
printf("%d",value[id]);
}
}

int main()
{
int n, m, ans;
while (~scanf("%d",&n))
while (n --) {
scanf("%d",&m);
for (int i = 1; i <= m; ++ i) {
scanf("%d",&value[i]);
}
scanf("%d",&ans);

memset(f, 0, sizeof(f));
f[1][formap(value[1])] = 1;
for (int i = 2; i <= m; ++ i) {
for (int j = 0; j <= 64000; ++ j) {
if (!f[i-1][j]) {
continue;
}
if (range(-32000, revmap(j)+value[i], 32000)) {
save(i, formap(revmap(j)+value[i]), j, '+');
}
if (range(-32000, revmap(j)-value[i], 32000)) {
save(i, formap(revmap(j)-value[i]), j, '-');
}
if (range(-32000, revmap(j)*value[i], 32000)) {
save(i, formap(revmap(j)*value[i]), j, '*');
}
if (revmap(j)%value[i]) {
continue;
}
if (range(-32000, revmap(j)/value[i], 32000)) {
save(i, formap(revmap(j)/value[i]), j, '/');
}
}
}
if (f[m][formap(ans)]) {
output(m, formap(ans));
printf("=%d\n",ans);
}else {
puts("NO EXPRESSION");
}
}
return 0;
}


#### uva 10400 - Game Show Math

2014-06-19 12:57:32

#### UVA 10400 Game Show Math

2014-04-17 18:28:30

#### UVa 10400 - Game Show Math

2012-08-15 00:31:33

#### UVA　10400 - Game Show Math

2013-06-13 19:02:37

#### uva 10400 Game Show Math

2013-11-18 03:34:48

#### uva 10400 Game Show Math

2013-08-30 19:12:10

#### uva 10400 - Game Show Math

2012-08-23 08:31:29

#### UVA - 10400 Game Show Math

2014-10-15 17:45:04

#### 10400 -Game Show Math

2014-06-01 13:59:48

#### 10400 game show math

2013-11-12 15:02:22