Problem 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

这道题其实把思路都告诉你了。。。

而题目上说数据稍微大点的话会,程序运行会用很多的时间,所以这里就用一个三维dp数组来记忆结果,使程序大大地加快速度。

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 26
int dp[N][N][N];
int dfs(int a,int b,int c)
{
//if(dp[a][b][c]!=0) return dp[a][b][c];
if(a<= || b<= || c<=)
return ;
if(a> || b> || c>)
return dfs(,,);
if(dp[a][b][c]!=) return dp[a][b][c];
if(a<b && b<c)
return dp[a][b][c]=dfs(a,b,c-)+dfs(a,b-,c-)-dfs(a,b-,c);
else
return dp[a][b][c]=dfs(a-,b,c)+dfs(a-,b-,c)+dfs(a-,b,c-)-dfs(a-,b-,c-);
}
int main()
{
int a,b,c;
while(scanf("%d%d%d",&a,&b,&c)==)
{
if(a==- && b==- && c==-)
break;
memset(dp,,sizeof(dp));
int ans=dfs(a,b,c);
printf("w(%d, %d, %d) = %d\n",a,b,c,ans);
}
return ;
}

hdu 1331 Function Run Fun的更多相关文章

  1. HDU 1331 Function Run Fun(记忆化搜索)

    Problem Description We all love recursion! Don't we? Consider a three-parameter recursive function w ...

  2. 洛谷P1464 Function  HDU P1579 Function Run Fun

    洛谷P1464 Function HDU P1579 Function Run Fun 题目描述 对于一个递归函数w(a,b,c) 如果a≤0 or b≤0 or c≤0就返回值11. 如果a> ...

  3. HDU 5608 function [杜教筛]

    HDU 5608 function 题意:数论函数满足\(N^2-3N+2=\sum_{d|N} f(d)\),求前缀和 裸题-连卷上\(1\)都告诉你了 预处理\(S(n)\)的话反演一下用枚举倍数 ...

  4. HDU 5608 - function

    HDU 5608 - function 套路题 图片来自: https://blog.csdn.net/V5ZSQ/article/details/52116285 杜教筛思想,根号递归下去. 先搞出 ...

  5. POJ 1579 Function Run Fun 【记忆化搜索入门】

    题目传送门:http://poj.org/problem?id=1579 Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Tota ...

  6. 记忆化搜索 hdu 1331

    Function Run Fun Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. HDU 6038 - Function | 2017 Multi-University Training Contest 1

    /* HDU 6038 - Function [ 置换,构图 ] 题意: 给出两组排列 a[], b[] 问 满足 f(i) = b[f(a[i])] 的 f 的数目 分析: 假设 a[] = {2, ...

  8. hdu1579 Function Run Fun(深搜+记忆化)

    版权声明:本文为博主原创文章.未经博主同意不得转载.vasttian https://blog.csdn.net/u012860063/article/details/37076755 转载请注明出处 ...

  9. HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

随机推荐

  1. IPython notebook 使用介绍

    参考资料: http://mindonmind.github.io/2013/02/08/ipython-notebook-interactive-computing-new-era/ http:// ...

  2. 比较好的总结runtime

    http://www.cocoachina.com/ios/20160128/15154.html

  3. C#总结项目《影院售票系统》编写总结完结篇

    回顾:昨天总结了影院售票系统核心部分-售票,整个项目也就完成了2/3了,需求中也要求了对销售信息的保存,今天就继续总结销售信息的保存以及加载销售信息. 分析:退出程序时将已售出票集合中的对象循环写入到 ...

  4. maven的pom.xml深入理解

    maven的pom.xml的具体使用和各个xml标签的作用.这样设计的原理是什么? maven实战的第17章-18章是架构方面的知识

  5. iOS9之后对于NSURL的编码转换方法变化说明

    在iOS9之后,官方推荐使用下面的方法对NSString进行转换 - (nullable NSString *)stringByAddingPercentEncodingWithAllowedChar ...

  6. rails 中 create, new, build, save 的用法以及误区汇总

    自己很初级,初级的不能再初级,所以初次接触rails的时候,对于里面的create,new,build等方法不是很了解,用的很混乱,导致经常出现不必要的bug,很苦恼,决定,总结一下,结合网上已有资源 ...

  7. js中的apply call 操作小结(参考自网络)

    1.方法定义 call方法:  语法:call([thisObj[,arg1[, arg2[,   [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象 说明: call ...

  8. java动态缓存技术:WEB缓存应用(转)

    可以实现不等待,线程自动更新缓存 Java动态缓存jar包请下载. 源代码: CacheData.java 存放缓存数据的Bean /** *  */package com.cari.web.cach ...

  9. dede 日期的所有格式

    [field:pubdate function=strftime('%d',@me)/] 日 [field:pubdate function=strftime('%d日',@me)/] - [fiel ...

  10. 【结构型】Composite模式

    组合模式意在将对象组合成树形结构以表示部分与整体的层次结构关系,并且用户对单个对象的操作以有对组合对象的操作都是一致的.即:组合对象 is-a 单个对象,同时又可以组合着 n 个的单个对象(甚至于其他 ...