ACM学习历程——HDU1331 Function Run Fun(锻炼多维dp的打表)
Description
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.
Input
Output
Sample Input
Sample Output
以及
这个条件给每一维的0这个面都赋成了1,有了这个初始化,就可以放心地递推了。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#define inf 0x3fffffff
#define eps 1e-10 using namespace std; int f[25][25][25]; void Init()
{
for (int i = 0; i <= 20; i++)
for (int j = 0; j <= 20; j++)
f[i][j][0] = f[i][0][j] = f[0][i][j] = 1;
for (int i = 1; i <= 20; i++)
for (int j = 1; j <= 20; j++)
for (int k = 1; k <= 20; k++)
{
if (i < j && j < k)
f[i][j][k] = f[i][j][k-1] + f[i][j-1][k-1] - f[i][j-1][k];
else
f[i][j][k] = f[i-1][j][k] + f[i-1][j-1][k] + f[i-1][j][k-1] - f[i-1][j-1][k-1];
}
} int w(int a, int b, int c)
{
if (a <= 0 || b <= 0 || c <= 0)
return 1;
if (a > 20 || b > 20 || c > 20)
return f[20][20][20];
return f[a][b][c];
} int main()
{
//freopen("test.txt", "r", stdin);
Init();
int a, b, c;
while (scanf("%d%d%d", &a, &b, &c) != EOF)
{
if (a == -1 && b == -1 && c == -1)
break;
printf("w(%d, %d, %d) = ", a, b, c);
printf("%d\n", w(a, b, c));
}
return 0;
}
ACM学习历程——HDU1331 Function Run Fun(锻炼多维dp的打表)的更多相关文章
- ACM学习历程—CodeForces 176B Word Cut(字符串匹配 && dp && 递推)
Description Let's consider one interesting word game. In this game you should transform one word int ...
- 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始
以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告
- ACM学习历程—HDU 5512 Pagodas(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...
- ACM学习历程—HDU5521 Meeting(图论)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是一个人从1开始走,一个人从n开始走.让最 ...
- ACM学习历程—HDU2476 String painter(动态规划)
http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意是给定一个起始串和一个目标串,然后每次可以将某一段区间染成一种字符,问从起始串到目标串最少需要染多 ...
- ACM学习历程—HDU5700 区间交(树状数组 && 前缀和 && 排序)
http://acm.hdu.edu.cn/showproblem.php?pid=5700 这是这次百度之星初赛2B的第五题.省赛回来看了一下,有这样一个思路:对于所有的区间排序,按左值排序. 然后 ...
- ACM学习历程—HDU5701 中位数计数(中位数 && 计数排序)
http://acm.hdu.edu.cn/showproblem.php?pid=5701 这是这次百度之星初赛2B的第六题.之前白山云做过类似的题,省赛完回来,我看了一下大概就有这样的思路:首先枚 ...
- ACM学习历程—HDU5696 区间的价值(分治 && RMQ && 线段树 && 动态规划)
http://acm.hdu.edu.cn/showproblem.php?pid=5696 这是这次百度之星初赛2B的第一题,但是由于正好打省赛,于是便错过了.加上2A的时候差了一题,当时有思路,但 ...
- ACM学习历程—HDU5668 Circle(数论)
http://acm.hdu.edu.cn/showproblem.php?pid=5668 这题的话,假设每次报x个,那么可以模拟一遍, 假设第i个出局的是a[i],那么从第i-1个出局的人后,重新 ...
随机推荐
- 深度解析 | 秒懂AI+智慧手机实践
阅读数:17 随着人工智能的概念越来越深入人心,智慧化生活和对应的智慧化终端体验也吸引越来越多的目光.可以想见,人工智能会深刻改变终端产业,但目前也面临各种挑战和问题.此前,在南京软件大会上,华 ...
- php 字符串内容是数组格式 转换成数组
一个简单的应用.. 例, $str = "array( 'USD'=>'1', 'GBP'=>'0.6494', 'EUR'=>'0.7668' ,'JPY'= ...
- webpy使用mysql数据库操作(web.database)
webpy_web.database模块 webpy框架中使用mysql管理数据库有两种方法,一种是使用python里面的MySQLdb模块: import MySQLdb 还有一种就是用webpy自 ...
- jquery在网页实时显示时间;
1.定义一个显示时间的位置 <div id="shijian"> </div> 2.jquery代码 function showTime() { var c ...
- WCP源码分析 与SpringMVC学习资料
1.在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找以及维护起来也不太方便. Spring2.5为我们引入了组件自动扫描机制,他可以在 ...
- java中Random(long seed)方法与rRandom()方法的使用产生随机数
Random 类作为JAVA中用于产生的随机数 ,new Random(10) :10是种子数. 注意:Random 的一个特点是:相同种子数的Random对象,对应相同次数生成的随机数字是完全相 ...
- 怎么利用jquery.form 提交form
说明:开发环境 vs2012 asp.net mvc c# 利用jQuery.form.js提交form 1.HTML前端代码 <%@ Page Language="C#" ...
- 关于dubbo的负载均衡
1 dubbo的集群 将同一个服务部署到多个机器上,然后全部注册到注册中心.这样的多个机器就是一个dubbo集群了. 2 dubbo的负载均衡是怎么回事 由于多台机器上都有同一个服务,因此consum ...
- Jeff Dean 排序时间计算
Quicksort (sometimes called partition-exchange sort) https://en.m.wikipedia.org/wiki/Quicksort
- CMake最好的学习资料
本文为转载,阅读不友好,请先查看原文:https://blog.gmem.cc/cmake-study-note 收下为原文内容================> 基础知识 CMake简介 CM ...