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 ...
随机推荐
- 5V与3.3V器件电平转换
源:5V与3.3V器件电平转换 当你使用3.3V的单片机的时候,电平转换就在所难免了,经常会遇到3.3转5V或者5V转3.3V的情况,这里介绍一个简单的电路,他可以实现两个电平的相互转换(注意是相互哦 ...
- cakephp 的事件系统(Getting to grips with CakePHP’s events system), 基于观察者模式
This article was written about CakePHP 2.x and has been untested with CakePHP 3.x CakePHP seems to g ...
- android 画图之setXfermode .
setXfermode 设置两张图片相交时的模式 我们知道 在正常的情况下,在已有的图像上绘图将会在其上面添加一层新的形状. 如果新的Paint是完全不透明的,那么它将完全遮挡住下面的Paint: 而 ...
- win7 64位andriod开发环境搭建
本文转自:http://www.cfanz.cn/index.php?c=article&a=read&id=65289 最近换了新电脑,装了win7 64位系统,安装了各种开发环境, ...
- python 打印类的属性、方法
打印变量db的类(class):[root@fuel ~]# pythonPython 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)[GCC 4.4.7 2012 ...
- c# mouseenter mousemove区别?
onmousedown 当用户用任何鼠标按钮单击对象时触发. onmouseenter 当用户将鼠标指针移动到对象内时触发. onmouseleave 当用户将鼠标指针移出对象边界时触发. onmou ...
- Mysql用户相关操作
MySQL 默认有个root用户,但是这个用户权限太大,一般只在管理数据库时候才用.如果在项目中要连接 MySQL 数据库,则建议新建一个权限较小的用户来连接. 在 MySQL 命令行模式下输入如下命 ...
- JS对象引用
对象和函数都是引用的关系(改变后者会改变前者) 但是下面这个列子情况不一样了.刚开始的时候 B 引用了 A的地址,后来B又重新赋值,新占了一个地址,A B俩的关系已经脱离. 那么有的时候,我们只有 ...
- 用php自动发邮件的简单实现
如何自动发送邮件? php自带mail方法,不过只能在linux下直接使用,windows下要配置smtp服务器,有点麻烦. 可以用一些现成的类来实现,比如很有名的phpmailer,功能很强大,代码 ...
- jemalloc Mongodb Nginx 优化
下载 http://www.canonware.com/jemalloc/download.html 下载 wget http://www.canonware.com/download/jemallo ...