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. 关于 while(1)和for(;;)效率问题的一点想法

    这几天看到好友的文章关于while(1)和for(;;)效率的讨论,手痒说了两句.回头一寻思,自己也仅仅是判断.没有做不论什么实验,我们就看看这两种写法究竟有什么差别: 实验环境:IAR EWARM ...

  2. 【laravel5.4】中jquery的post Ajax提交

    1.post的ajax需要提交csrf_token字段,进行安全过滤 <meta name="csrf-token" content="{{ csrf_token( ...

  3. Java设计模式中的单例模式

    有时候在实际项目的开发中,我们会碰到这样一种情况,该类只允许存在一个实例化的对象,不允许存在一个以上的实例化对象,我们将这种情况称为Java设计模式中的单例模式.设计单例模式主要采用了Java的pri ...

  4. maven 错误处理

    如果是方法找不到或者返回参数变了,那么肯定是包被升级了,那么到仓库下把对应的包删掉,然后maven自动下载最新的. 如果是包找不到,或者类找不到,那么把maven ->update maven可 ...

  5. get请求乱码情况

    编写一个RegistServlet处理用户的Get请求数据 public void doGet(HttpServletRequest request, HttpServletResponse resp ...

  6. DP较为完整的知识

    数位DP 这类题,才刚刚接触,记得去年网络赛,就有道这样的题,我完全不会, 对于这类题基本方法是,是利用数的位数来构造转移方程. 下面给出两篇论文的链接: <数位计数问题解法研究> < ...

  7. Chrome浏览器桌面通知提示设置

    版本 24.0.1312.56 m     老版本23.* 桌面通知,也可以由用户在Chrome浏览器中自定义:板手 -> 选项  -> 高级选项 –> 通知 (管理例外情况…).

  8. android适配器Adapter

    一.什么是适配器,适配器有什么用? 适配器是AdapterView视图(如ListView - 列表视图控件.Gallery - 缩略图浏览器控件.GridView - 网格控件.Spinner - ...

  9. Linux时间子系统(五) POSIX Clock

    一.前言 clock是timer的基础,任何一个timer都需要运作在一个指定的clock上来.内核中维护了若干的clock,本文第二章描述了clock的基本概念和一些静态定义的posix clock ...

  10. lcx.exe内网转发命令教程 + LCX免杀下载

    作者: 小健 本机: lcx -listen 2222 3333 2222为转发端口,3333为本机任意未被占用的端口 肉鸡:lcx -slave 119.75.217.56 2222 127.0.0 ...