Description

We all love recursion! Don't we?       
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

The input for your program will be a series of integer triples, one per line, until the end-of-file flag of -1 -1 -1. Using the above technique, you are to calculate w(a, b, c) efficiently and print the result.       
              

Output

Print the value for w(a,b,c) for each triple.      
              

Sample Input

1 1 1
2 2 2
10 4 6
50 50 50
-1 7 18
-1 -1 -1
              

Sample Output

w(1, 1, 1) = 2
w(2, 2, 2) = 4
w(10, 4, 6) = 523
w(50, 50, 50) = 1048576
w(-1, 7, 18) = 1
 
 
递推的式子是直接给出来的。里面最关键的两个式子是:
f[i][j][k] = f[i][j][k-1] + f[i][j-1][k-1] - f[i][j-1][k];
以及
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];
可知这个三维dp式子,前一个式子都是在i那一维,通过特征观察可知,是j一层一层递推的(或者k)。
而第二个式子又可以看出是i一层层的递推。
故递推的时候只需要三个for循环就搞定。
但是还需要注意的是
任意i,j,f[i][j][0] = f[i][0][j] = f[0][i][j] = 1;
这个条件给每一维的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的打表)的更多相关文章

  1. ACM学习历程—CodeForces 176B Word Cut(字符串匹配 && dp && 递推)

    Description Let's consider one interesting word game. In this game you should transform one word int ...

  2. 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始

    以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告

  3. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

  4. ACM学习历程—HDU5521 Meeting(图论)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是一个人从1开始走,一个人从n开始走.让最 ...

  5. ACM学习历程—HDU2476 String painter(动态规划)

    http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意是给定一个起始串和一个目标串,然后每次可以将某一段区间染成一种字符,问从起始串到目标串最少需要染多 ...

  6. ACM学习历程—HDU5700 区间交(树状数组 && 前缀和 && 排序)

    http://acm.hdu.edu.cn/showproblem.php?pid=5700 这是这次百度之星初赛2B的第五题.省赛回来看了一下,有这样一个思路:对于所有的区间排序,按左值排序. 然后 ...

  7. ACM学习历程—HDU5701 中位数计数(中位数 && 计数排序)

    http://acm.hdu.edu.cn/showproblem.php?pid=5701 这是这次百度之星初赛2B的第六题.之前白山云做过类似的题,省赛完回来,我看了一下大概就有这样的思路:首先枚 ...

  8. ACM学习历程—HDU5696 区间的价值(分治 && RMQ && 线段树 && 动态规划)

    http://acm.hdu.edu.cn/showproblem.php?pid=5696 这是这次百度之星初赛2B的第一题,但是由于正好打省赛,于是便错过了.加上2A的时候差了一题,当时有思路,但 ...

  9. ACM学习历程—HDU5668 Circle(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5668 这题的话,假设每次报x个,那么可以模拟一遍, 假设第i个出局的是a[i],那么从第i-1个出局的人后,重新 ...

随机推荐

  1. 深度解析 | 秒懂AI+智慧手机实践

    阅读数:17 ​​​随着人工智能的概念越来越深入人心,智慧化生活和对应的智慧化终端体验也吸引越来越多的目光.可以想见,人工智能会深刻改变终端产业,但目前也面临各种挑战和问题.此前,在南京软件大会上,华 ...

  2. php 字符串内容是数组格式 转换成数组

    一个简单的应用.. 例, $str    =    "array( 'USD'=>'1', 'GBP'=>'0.6494', 'EUR'=>'0.7668' ,'JPY'= ...

  3. webpy使用mysql数据库操作(web.database)

    webpy_web.database模块 webpy框架中使用mysql管理数据库有两种方法,一种是使用python里面的MySQLdb模块: import MySQLdb 还有一种就是用webpy自 ...

  4. jquery在网页实时显示时间;

    1.定义一个显示时间的位置 <div id="shijian"> </div> 2.jquery代码 function showTime() { var c ...

  5. WCP源码分析 与SpringMVC学习资料

    1.在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找以及维护起来也不太方便. Spring2.5为我们引入了组件自动扫描机制,他可以在 ...

  6. java中Random(long seed)方法与rRandom()方法的使用产生随机数

    Random 类作为JAVA中用于产生的随机数 ,new  Random(10)  :10是种子数. 注意:Random 的一个特点是:相同种子数的Random对象,对应相同次数生成的随机数字是完全相 ...

  7. 怎么利用jquery.form 提交form

    说明:开发环境 vs2012 asp.net mvc c# 利用jQuery.form.js提交form 1.HTML前端代码 <%@ Page Language="C#" ...

  8. 关于dubbo的负载均衡

    1 dubbo的集群 将同一个服务部署到多个机器上,然后全部注册到注册中心.这样的多个机器就是一个dubbo集群了. 2 dubbo的负载均衡是怎么回事 由于多台机器上都有同一个服务,因此consum ...

  9. Jeff Dean 排序时间计算

    Quicksort (sometimes called partition-exchange sort) https://en.m.wikipedia.org/wiki/Quicksort

  10. CMake最好的学习资料

    本文为转载,阅读不友好,请先查看原文:https://blog.gmem.cc/cmake-study-note 收下为原文内容================> 基础知识 CMake简介 CM ...