UVa 10400 - Game Show Math
题目大意:给出n(n<100)个正整数和一个目标数,按照给出数的顺序,运用+、-、*、/四则运算(不考虑优先级),判断能否得出所要的结果。
首先考虑的就是暴力枚举,不过时间复杂度为O(4n),会超时。由于题目限定中间运算结果在[-32000, 32000]内,当n到一定程度后(4n远远大于64000后),就会存在大量的重复子问题,可以通过记忆化搜索的方法去重,具体到这个问题就是vis数组的运用。
#include <cstdio>
#include <cstring>
#define MAXN 100+10 long long a[MAXN], target;
char op[MAXN];
int n;
bool ok, vis[MAXN][*+]; bool judge(int cur, int n)
{
return n >= - && n <= && !vis[cur][n+];
} void dfs(int cur, long long res)
{
if (cur == n )
{
if (res == target) ok = true;
return;
}
if (!ok && judge(cur, res+a[cur]))
{
op[cur] = '+';
vis[cur][res+a[cur]+] = true;
dfs(cur+, res+a[cur]);
}
if (!ok && judge(cur, res-a[cur]))
{
op[cur] = '-';
vis[cur][res-a[cur]+] = true;
dfs(cur+, res-a[cur]);
}
if (!ok && judge(cur, res*a[cur]))
{
op[cur] = '*';
vis[cur][res*a[cur]+] = true;
dfs(cur+, res*a[cur]);
}
if (!ok && res % a[cur] == && judge(cur, res/a[cur]))
{
op[cur] = '/';
vis[cur][res/a[cur]+] = true;
dfs(cur+, res/a[cur]);
}
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int T;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
for (int i = ; i < n; i++)
scanf("%lld", &a[i]);
scanf("%lld", &target);
ok = false;
memset(vis, , sizeof(vis));
vis[][a[]+] = true;
dfs(, a[]);
if (ok)
{
for (int i = ; i < n-; i++)
printf("%lld%c", a[i], op[i+]);
printf("%lld=%lld\n", a[n-], target);
}
else printf("NO EXPRESSION\n");
}
return ;
}
也可以用递推的方法求解,不过在打印结果的时候要麻烦一些,二者的思想是一样的。
UVa 10400 - Game Show Math的更多相关文章
- UVA 10400 Game Show Math (dfs + 记忆化搜索)
Problem H Game Show Math Input: standard input Output: standard output Time Limit: 15 seconds A game ...
- UVa 10400 - Game Show Math 游戏中的数学 dfs+判重
题意:给出一些数字和一个目标数字,要求你在数字间添加+-*/,让表达式能达到目标数字,运算符号的优先级都是一样的. 由于数据量很大,本来想用map<string>判重的,结果还是超时了,然 ...
- UVa 10400 记忆化搜索
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> us ...
- UVa 1595 Symmetry (set && math)
题意:给出n个在直角坐标系上的点,问你能不能找出一条竖轴(即垂直于x的轴)使得所有的点根据这条轴对称,能则输出YES,否则输出NO 分析:首先需要找到对称轴的值,将所有n个点的x轴的值加起来然后去除以 ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- UVa10023手动开大数平方算法
题目链接:UVa 10023 import java.math.BigInteger; import java.util.Scanner; public class Main { public sta ...
- UVA 1397 - The Teacher's Side of Math(高斯消元)
UVA 1397 - The Teacher's Side of Math 题目链接 题意:给定一个x=a1/m+b1/n.求原方程组 思路:因为m*n最多20,全部最高项仅仅有20.然后能够把每一个 ...
- Math - Uva 11300 Spreading the Wealth
Spreading the Wealth Problem's Link ---------------------------------------------------------------- ...
- UVa 10773 - Back to Intermediate Math
题目:渡河问题.给你河水宽度,水流速度,求垂直渡河与最快渡河的时间差. 分析:物理题,数学题. 最快渡河情况,传垂直运动,垂直渡河,船的水平分速度和水流速度抵消. 说明:注意水流速度不能为0. #in ...
随机推荐
- Mysql命令-以NULL做where条件过滤时应该写 IS NULL;
以NULL做where条件过滤时应该写 IS NULL;SELECT * FROM pet WHERE death IS NULL; SELECT * FROM pet WHERE death IS ...
- Polygone对象
Polylgon对象是由一个或多个Ring对象的有序集合,它可以是由单个Ring 对象构成,也可以使用多个Ring组成.Polygon通常用来代表有面积的多边形矢量对象,如行政区,建筑物等. 组成Po ...
- zf-关于业务量图表没有出现统计柱形图问题
是实现类里的 objs[0] = ht.get("BUS_NAME");objs[1] = ht.get("NUM"); 这个写成小写了 改成大写即可,原来这个 ...
- How to spend you day ?
如果这是你生命中的最后的一天,你该如何去过好它呢? 不要浪费你生命中的每一分,每一秒!!!
- Fix a Tree
Fix a Tree A tree is an undirected connected graph without cycles. Let's consider a rooted undirecte ...
- 修改sqlserver的数据库排序规则语句
alter database SOETMS collate Chinese_PRC_CI_AS
- Swift中异常处理(续集.正则)
正则表达式 1.创建正则表达式的规则 let pattern = "abc" 2.创建正则表达式对象 // 方式一:try方式 do { let regex = try NSReg ...
- BZOJ 3110:[Zjoi2013]K大数查询(整体二分)
http://www.lydsy.com/JudgeOnline/problem.php?id=3110 题意:-- 思路:其实和之前POJ那道题差不多,只不过是换成区间更新,而且是第k大不是第k小, ...
- zencart里常用 SQL
1.如何替换某个字段的一些字符 如我把products表的products_image字段中包含2011的字符替换成Zencart2012update `products` set `products ...
- 第一次在手机上跑动ane
记录一下: 打包的时候先出现 error 100: descriptor cannot be parsed, 原因是命名空间少了个引号,自己粗心所致 第二次打包出现了invalid namespace ...