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. Android中读取图片EXIF元数据之metadata-extractor的使用

    一.引言及介绍 近期在开发中用到了metadata-extractor-xxx.jar 和 xmpcore-xxx.jar这个玩意, 索性查阅大量文章了解学习,来分享分享. 本身工作也是常常和处理大图 ...

  2. Windows 10 1703创意者更新官方ISO镜像大全

    2017年04月07日 20:00 19867 次阅读 稿源:快科技 12 条评论 Windows 10 Creators Update创意者更新正式版已经发布,目前只能通过易生.MCT工具或者ISO ...

  3. scapy windows install

    最近有点扫描网络的需求,都说scapy好,但是安装是个事(当然指的是windows安装)有个scapy3k,支持python3,可惜需要powershell,也就是说windows xp是没有戏了. ...

  4. 你要的最后一个字符就在下面这个字符串里,这个字符是下面整个字符串中第一个只出现一次的字符。(比如,串是abaccdeff,那么正确字符就是b了)

    include "stdafx.h" #include<iostream> #include<string> using namespace std; in ...

  5. XMPP协议概述

    XMPP(Extensible Messaging and Presence Protocol,前称Jabber)是一种以 XML 为基础的开放式实时通信协议,关于它的协议细节,网上已经有太多分析文章 ...

  6. mysqldump导入导出数据库总结

    mysqldump导入导出数据库总结 基本的使用方法总结: 1 导出所有库 系统命令行 mysqldump -uusername -ppassword --all-databases > all ...

  7. 【BZOJ4200】[Noi2015]小园丁与老司机 DP+最小流

    [BZOJ2839][Noi2015]小园丁与老司机 Description 小园丁 Mr. S 负责看管一片田野,田野可以看作一个二维平面.田野上有 nn 棵许愿树,编号 1,2,3,…,n1,2, ...

  8. protobuf + maven 爬坑记

    疯狂创客圈 死磕Netty 亿级流量架构系列之20 [博客园 总入口 ] 本文说明 本篇是 netty+Protobuf 整合实战的 第一篇,完成一个 基于Netty + Protobuf 实战案例. ...

  9. mysql系列之1.mysql基础

    非关系型(NOSQL)数据库 键值存储数据库: memcached  /  redis  /  memcachedb  /  Berkeley db 列存储数据库: Cassandra  /  Hba ...

  10. 20170326 ABAP调用外部webservice 问题

    1.SE80 创建企业服务: 代理生成:出现错误 库处理程序中出现例外 错误的值:未知类型参考ns1:ArrayOfMLMatnrResource 尝试: 一.测试本地文件:---无效 1. 将网址链 ...