Problem H
Game Show Math
Input:
 standard input
Output: standard output
Time Limit: 15 seconds

A game show in Britain has a segment where it gives its contestants a sequence of positive numbers and a target number. The contestant must make a mathematical expression using all of the numbers in the sequence and only the operators: +-*, and, /. Each number in the sequence must be used exactly once, but each operator may be used zero to many times. The expression should be read from left to right, without regard for order of operations, to calculate the target number. It is possible that no expression can generate the target number. It is possible that many expressions can generate the target number.

There are three restrictions on the composition of the mathematical expression:

o  the numbers in the expression must appear in the same order as they appear in the input file

o  since the target will always be an integer value (a positive number), you are only allowed to use / in the expression when the result will give a remainder of zero.

o  you are only allowed to use an operator in the expression, if its result after applying that operator is an integer from (-32000 ..+32000).

Input

The input file describes multiple test cases. The first line contains the number of test cases n.

Each subsequent line contains the number of positive numbers in the sequence p, followed by p positive numbers, followed by the target number. Note that 0 < p £ 100. There may be duplicate numbers in the sequence. But all the numbers are less than 32000.

Output

The output file should contain an expression, including all k numbers and (k-1) operators plus the equals sign and the target. Do not include spaces in your expression. Remember that order of operations does not apply here. If there is no expression possible output "NO EXPRESSION" (without the quotes). If more than one expression is possible, any one of them will do.

 

Sample Input

3
3 5 7 4 3
2 1 1 2000
5 12 2 5 1 2 4

Sample Output
5+7/4=3 
NO EXPRESSION
12-2/5*1*2=4

题意:给定n个数字。和一个answer。要求在n个数字中插入“+-*/“ 使得式子成立。。

题目给了几个限定条件:1、数字位置不能变,且运算符优先级与先后顺序有关(和+-*/无关)。。

2、运算过程结果必须一直保持在[-32000,32000]之间。

3、‘/’号只能在整除的情况下才能使用。。

思路:直接暴力时间复杂度O(4 ^ n)。这题最多100。。果断超时的节奏。。于是乎用记忆化搜索。

开一个二维数组vis[i][j].i表示加到第几个,j表示当前结果。。如果重复状态直接return。

这里有一个要注意的地方。就是j表示的当前结果可能为负数。但是负数最多为-32000。。所以我们可以把结果都加上32000.这样就没有问题了。

代码:

#include <stdio.h>
#include <string.h> int vis[105][66666];
int t, n, num[105], ans, judge;
char out[105];
void dfs(int start, int sum) {
if (start == n) {
if (sum == ans)
judge = 1;
return;
}
if (sum + num[start] <= 32000 && !vis[start][sum + num[start] + 32000]) {
out[start] = '+';
vis[start][sum + num[start] + 32000] = 1;
dfs(start + 1, sum + num[start]);
if (judge) return;
}
if (sum - num[start] >= -32000 && !vis[start][sum - num[start] + 32000]) {
out[start] = '-';
vis[start][sum - num[start] + 32000] = 1;
dfs(start + 1, sum - num[start]);
if (judge) return;
}
if (sum * num[start] >= -32000 && sum * num[start] <= 32000 && !vis[start][sum * num[start] + 32000]) {
out[start] = '*';
vis[start][sum * num[start] + 32000] = 1;
dfs(start + 1, sum * num[start]);
if (judge) return;
}
if (!(sum % num[start])) {
if (sum / num[start] >= -32000 && sum / num[start] <= 32000 && !vis[start][sum / num[start] + 32000]) {
out[start] = '/';
vis[start][sum / num[start] + 32000] = 1;
dfs(start + 1, sum / num[start]);
if (judge) return;
}
}
}
int main() {
scanf("%d", &t);
while (t --) {
judge = 0;
memset(vis, 0, sizeof(vis));
scanf("%d", &n);
for (int i = 0; i < n; i ++)
scanf("%d", &num[i]);
scanf("%d", &ans);
vis[0][num[0] + 32000] = 1;
dfs(1, num[0]);
if (judge) {
for (int i = 0; i < n; i ++) {
if (i != 0)
printf("%c%d", out[i], num[i]);
else
printf("%d", num[i]);
}
printf("=%d\n", ans);
}
else printf("NO EXPRESSION\n");
}
return 0;
}

UVA 10400 Game Show Math (dfs + 记忆化搜索)的更多相关文章

  1. dfs+记忆化搜索,求任意两点之间的最长路径

    C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...

  2. 不要62 hdu 2089 dfs记忆化搜索

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中 ...

  3. kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)

    pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/ ...

  5. hdu 1078(dfs记忆化搜索)

    题意:容易理解... 思路:我开始是用dfs剪枝做的,968ms险过的,后来在网上学习了记忆化搜索=深搜形式+dp思想,时间复杂度大大降低,我个人理解,就是从某一个点出发,前面的点是由后面的点求出的, ...

  6. 8636 跳格子(dfs+记忆化搜索)

    8636 跳格子 该题有题解 时间限制:2457MS  内存限制:1000K提交次数:139 通过次数:46 题型: 编程题   语言: G++;GCC Description 地上有一个n*m 的数 ...

  7. poj1088-滑雪 【dfs 记忆化搜索】

    http://poj.org/problem?id=1088 滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 79806 ...

  8. DFS——>记忆化搜索——>动态规划

    以洛谷P1802  5倍经验日 为例 https://www.luogu.org/problem/show?pid=1802 题目背景 现在乐斗有活动了!每打一个人可以获得5倍经验!absi2011却 ...

  9. POJ 1191 棋盘分割 【DFS记忆化搜索经典】

    题目传送门:http://poj.org/problem?id=1191 棋盘分割 Time Limit: 1000MS   Memory Limit: 10000K Total Submission ...

随机推荐

  1. Linux下配置免安装版Tomcat

    一.下载免安装版Tomcat 打开Tomcat主页http://tomcat.apache.org/,下载Tomcat免安装版,我这里下载的是apache-tomcat-7.0.2.tar.gz. 二 ...

  2. C++中的static成员

    C++中的static 成员永远是我心中的痛,记了好多次了,但是今天在项目中依然忘记了,今天写下来,方便以后不用再去Baidu.google搜索了. 在头文件中声明静态成员 static int i; ...

  3. MVC,jquery异步

    创建一个Ajax控制器 using System; using System.Collections.Generic; using System.Linq; using System.Web; usi ...

  4. js生成随机数的方法小结

    js生成随机数主要用到了内置的Math对象的random()方法.用法如:Math.random().它返回的是一个 0 ~ 1 之间的随机数.有了这么一个方法,那生成任意随机数就好理解了.比如实际中 ...

  5. 浅谈MDX处理空值NULL及格式化结果

    MDX查询结果中往往会含有"NULL"值,这是某维度下对应的的量值不存在导致的,为了让报表呈现更好的效果,在有些情况下,需要将"NULL"的切片值置换成0,这些 ...

  6. xode 中文乱码处理

    find *.* -exec sh -c "iconv -f GB18030 -t UTF-8 {} > {}.txt" \;

  7. php 之 分页查询的使用方法及其类的封装

    一.分页的使用: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  8. PHP程序员衰老后的下场

    长期从事编程活动的程序员都期望在50多岁时能爬到一个足够高的职位,或者能顺利的退休. 但我在这里讨论的可能是一个你还没有想过的问题:如果到那时你失业了呢? 50多岁时你的职业仕途会成为一个问题.如果你 ...

  9. magent——memcached缓存代理服务器

    memcached分布式缓存 我们使用PHP连接多台memcached服务器,做分布式缓存,实现如下: $memcache = new Memcache; $memcache->addServe ...

  10. for_each 用法!

    class MapTest:public CapTest{ private: set <string> MyTestContain; typedef pair <string,int ...