记忆化搜索 dp学习~2
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1331
Function Run Fun
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3459 Accepted Submission(s): 1707
Consider a three-parameter recursive function w(a, b, c):
if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns:
1
if a > 20 or b > 20 or c > 20, then w(a, b, c) returns:
w(20, 20, 20)
if a < b and b < c, then w(a, b, c) returns:
w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
otherwise it returns:
w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)
This is an easy function to implement. The problem is, if implemented directly, for moderate values of a, b and c (for example, a = 15, b = 15, c = 15), the program takes hours to run because of the massive recursion.
2 2 2
10 4 6
50 50 50
-1 7 18
-1 -1 -1
w(2, 2, 2) = 4
w(10, 4, 6) = 523
w(50, 50, 50) = 1048576
w(-1, 7, 18) = 1
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = ;
int dp[N][N][N]; void init()
{
memset(dp,-,sizeof(dp));
for(int i = ; i <= ; i++)
{
for(int j = ; j <=; j++)
{
dp[][i][j] = dp[i][j][] = dp[i][][j] = ;
}
}
}
int w(int a, int b, int c)
{
if(dp[a][b][c]!=-) return dp[a][b][c];
if(a<b&&b<c){
return dp[a][b][c] = w(a,b,c-)+w(a,b-,c-)-w(a,b-,c);
}
else return dp[a][b][c] = w(a-, b, c) + w(a-, b-, c) + w(a-, b, c-) - w(a-, b-, c-);
}
int main()
{
init();
int a, b, c;
while(~scanf("%d%d%d",&a,&b,&c))
{
//printf("w = %d\n",w(20,20,20));
if(a==-&&b==-&&c==-) return ;
else if(a<=||b<=||c<=) printf("w(%d, %d, %d) = 1\n",a,b,c);
else if(a>||b>||c>) printf("w(%d, %d, %d) = %d\n",a,b,c,w(,,));
else printf("w(%d, %d, %d) = %d\n",a,b,c,w(a,b,c));
}
return ;
}
记忆化搜索 dp学习~2的更多相关文章
- 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence
题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...
- HDU 1078 FatMouse and Cheese 记忆化搜索DP
直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...
- 【10.31校内测试】【组合数学】【记忆化搜索/DP】【多起点多终点二进制拆位Spfa】
Solution 注意取模!!! Code #include<bits/stdc++.h> #define mod 1000000007 #define LL long long usin ...
- hdu1331&&hdu1579记忆化搜索(DP+DFS)
这两题是一模一样的``` 题意:给了一系列递推关系,但是由于这些递推很复杂,所以递推起来要花费很长的时间,所以我要编程序在有限的时间内输出答案. w(a, b, c): 如果a,b,c中有一个值小于等 ...
- HDU - 6415 多校9 Rikka with Nash Equilibrium(纳什均衡+记忆化搜索/dp)
Rikka with Nash Equilibrium Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K ...
- hdu 4960 记忆化搜索 DP
Another OCD Patient Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Ot ...
- HNU OJ10086 挤挤更健康 记忆化搜索DP
挤挤更健康 Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 339, A ...
- BZOJ1048:[HAOI2007]分割矩阵(记忆化搜索DP)
Description 将一个a*b的数字矩阵进行如下分割:将原矩阵沿某一条直线分割成两个矩阵,再将生成的两个矩阵继续如此分割(当然也可以只分割其中的一个), 这样分割了(n-1)次后,原矩阵被分割成 ...
- [P2921][USACO08DEC]在农场万圣节Trick or Treat on the Farm (记忆化搜索/DP?,Tarjan?)
第一看还以为是水题 随便打了一个bfs只有40分…… 然后开始颓废 #include<bits/stdc++.h> #define N 100005 using namespace std ...
随机推荐
- npm的使用总结
npm常用命令 npm list 查看当前目录下已安装的包 npm root -g 查看全局安装的包的路径 npm help 查看全部命令 npm update/uninstall moduleNam ...
- Mybatis-----优化配置文件,基于注解CR
这篇主要写配置文件的优化,例如 jdbc.properties 配置文件 ,引入数据库的文件,例如driver,url,username,password 等,然后在 SqlMapConfig.x ...
- ReactNative实现图集功能
需求描述: 图片缩放.拖动.长按保存等基础图片查看的功能: 展示每张图片文本描述: 实现效果,如图: 实现步骤 使用第三方插件:react-native-image-zoom-viewer 插件Git ...
- 【转】nginx提示:500 Internal Server Error错误的解决方法
本文转自:http://www.jb51.net/article/35675.htm 现在越来越多的站点开始用 Nginx ,("engine x") 是一个高性能的 HTTP 和 ...
- Error in library(DESeq2) : 不存在叫‘DESeq2’这个名字的程辑包
Error in read.dcf(file.path(pkgname, "DESCRIPTION"), c("Package", "Type&quo ...
- Android 7.1 ActivityManagerService 屏幕旋转流程分析 (四)
四.Activity的更新(旋转) sendNewConfiguration()会调用到ActivityManagerService的updateConfiguration()来update Conf ...
- windows 运行banana
1 git clone 工程 2 安装 npm 3 执行 npm install -g bower
- How It Works: CMemThread and Debugging Them
The wait type of CMemThread shows up in outputs such as sys.dm_exec_requests. This post is intended ...
- webpack 构建简单的vue项目
---恢复内容开始--- webpack主要执行流程: 入口→loader处理→出口 // webpack.config.js 文件:const path = require('path') // 引 ...
- Git上传项目到GitHub
1.注册账户 https://github.com/ 2.创建仓库 3.需要安装 Git http://msysgit.github.com/ 4.本地创建ssh key(不是必要,不创建ssh可 ...