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

Source

 
方法一:
直接递归会超时,预处理即可。实现把所有结果存到三维数组中。
 #include <iostream>
#include <cstdio> using namespace std; int main()
{
int w[][][], ans, a, b, c;
//预处理
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
for(int k = ; k < ; k++)
{
if(!i || !j || !k) w[i][j][k] = ;
else if(i < j && j < k)
w[i][j][k] = w[i][j][k-] + w[i][j-][k-] - w[i][j-][k];
//w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
else
w[i][j][k] = w[i-][j][k] + w[i-][j-][k] + w[i-][j][k-] - w[i-][j-][k-];
//w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)
}
while(scanf("%d %d %d", &a, &b, &c))
{
if(a == - && b == - && c == -) break;
if(a <= || b <= || c <= ) ans = ;
else if(a > || b > || c > ) ans = w[][][];
else ans = w[a][b][c];
printf("w(%d, %d, %d) = %d\n", a, b, c, ans);
}
return ;
}

方法二:

总之要先把之前运算出来的结果存到三维数组中,避免重复运算。

 #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int dp[][][]; int dfs(int a,int b,int 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)
dp[a][b][c] = dfs(a,b,c-)+dfs(a,b-,c-)-dfs(a,b-,c);
else
dp[a][b][c] = dfs(a-,b,c)+dfs(a-,b-,c)+dfs(a-,b,c-)-dfs(a-,b-,c-);
return dp[a][b][c];
} int main()
{
int a,b,c;
memset(dp,,sizeof(dp));
while(~scanf("%d%d%d",&a,&b,&c))
{
if(a == - && b == - && c == -)
break;
printf("w(%d, %d, %d) = %d\n",a,b,c,dfs(a,b,c));
} return ;
}
 

POJ1579:Function Run Fun的更多相关文章

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

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

  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 1331 Function Run Fun

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

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

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

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

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

  6. ACM学习历程——HDU1331 Function Run Fun(锻炼多维dp的打表)

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

  7. Function Run Fun-递归+细节处理

    We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a < ...

  8. POJ 1579 Function Run Fun

    简单动态规划,详细代码网上有!

  9. poj 1579 Function Run Fun(记忆化搜索+dp)

    题目链接:http://poj.org/problem?id=1579 思路分析:题目给出递归公式,使用动态规划的记忆搜索即可解决. 代码如下: #include <stdio.h> #i ...

随机推荐

  1. DIV+CSS布局重新学习之使用A标签和CSS制作按钮

    这里主要利用A元素的伪类来实现: a:link {color: #FF0000} /* 未访问的链接 */ a:visited {color: #00FF00} /* 已访问的链接 */ a:hove ...

  2. rabbitmq vhost

    参考 http://blog.163.com/sky20081816@126/blog/static/16476102320107173226920/ http://blog.csdn.net/kev ...

  3. bash: fork: Resource temporarily unavailable

    Last login: Wed Jul 26 09:19:11 2017 from ... -bash: fork: Resource temporarily unavailable -bash-3. ...

  4. Golang配置文件解析-oozgconf

    代码地址如下:http://www.demodashi.com/demo/14411.html 简介 oozgconf基于Golang开发,用于项目中配置文件的读取以及加载,是一个轻量级的配置文件工具 ...

  5. Android HandlerThread详解

    概述 Android HandlerThread使用,自带Looper消息循环的快捷类. 详细 代码下载:http://www.demodashi.com/demo/10628.html 原文地址: ...

  6. HDU----专题训练

    Problem A Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Sub ...

  7. HttpClient中的Timout

    connection timeout和SoTimeout A connection timeout occurs only upon starting the TCP connection. This ...

  8. JAvaScript:JS数组元素去重的方法

    在做javascript开发的时候,经常会遇到数组元素重复的问题,而javascript Array又没有直接提供方法解决此问题,还需要自己去实现. 方案一: 思路: 1.构建一个新的数组存放结果: ...

  9. Android开发之5大布局方式详解

    Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...

  10. 转: javascript动态添加、修改、删除对象的属性和方法

    在其他语言中,对象一旦生成,就不可更改了,要为一个对象添加修改成员必须要在对应的类中修改,并重新实例化,而且程序必须经过重新编译.JavaScript 中却非如此,它提供了灵活的机制来修改对象的行为, ...