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
 #include<stdio.h>
#include<iostream>
#include<string.h>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std; int book[][][];
int w(int a,int b,int c)
{
if(a<=||b<=||c<=)
return ;
else if(a>||b>||c>)
return w(,,);
else if(book[a][b][c]!=-)
return book[a][b][c];
else if(a<b&&b<c)
return book[a][b][c]=w(a,b,c-)+w(a,b-,c-)-w(a,b-,c);
else
return book[a][b][c]=w(a-,b,c)+w(a-,b-,c)+w(a-,b,c-)-w(a-,b-,c-);
// return book[a][b][c];
}
int main()
{
int a,b,c;
while(~scanf("%d %d %d",&a,&b,&c))
{
if(a==-&&b==-&&c==-)
break;
memset(book,-,sizeof(book));
printf("w(%d, %d, %d) = %d\n",a,b,c,w(a,b,c));
}
return ;
}
int w(int a,int b,int c)
{
if(a>=&&a<=&&b>=&&b<=&&c>=&&c<=&&book[a][b][c]!=-)//防止越界
return book[a][b][c];
if(a<=||b<=||c<=)
return ;
else if(a>||b>||c>)
return w(,,);
else if(a<b&&b<c)
return book[a][b][c]=w(a,b,c-)+w(a,b-,c-)-w(a,b-,c);
else
return book[a][b][c]=w(a-,b,c)+w(a-,b-,c)+w(a-,b,c-)-w(a-,b-,c-);
}

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. poj 1579 Function Run Fun 【记忆化递归】

    <题目链接> 题目大意: 给出一些递归式,直接套用这些递归式计算. 解题分析: 递归式已经由题目明确说明了,但是无脑递归铁定超时,所以此时,我们需要加上记忆化,对于那些已经算过的,就没有必 ...

  4. POJ 1579 Function Run Fun 记忆化递归

    典型的记忆化递归问题. 这类问题的记忆主要是利用数组记忆.那么已经计算过的值就能够直接返回.不须要进一步递归了. 注意:下标越界.递归顺序不能错,及时推断是否已经计算过值了,不要多递归. 或者直接使用 ...

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

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

  6. POJ1579:Function Run Fun

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

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

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

  8. hdu 1331 Function Run Fun

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

  9. ural 1353. Milliard Vasya's Function(背包/递归深搜)

    1353. Milliard Vasya's Function Time limit: 1.0 second Memory limit: 64 MB Vasya is the beginning ma ...

随机推荐

  1. struct and union example

    1. StructHandler.c: /* * StructHandler.c * *  Created on: Jul 6, 2013 *      Author: wangle */#inclu ...

  2. 关于BUG管理工具的操作总结。(禅道)

    禅道是第一款国产的优秀开源项目管理软件.先进的管理思想,合理的软件架构,简洁实效的操作,优雅的代码实现,灵活的扩展机制,强大而易用的api 调用机制,多语言支持,多风格支持,搜索功能,统计功能——这一 ...

  3. selenium之 文件上传所有方法整理总结

    本文转载“灰蓝”的原创博客.http://blog.csdn.net/huilan_same/article/details/52439546 文件上传是所有UI自动化测试都要面对的一个头疼问题,今天 ...

  4. c# api身份验证和授权

    授权 1. 全局 config.Filters.Add(new AuthorizeAttribute()); 2.控制器级别 [Authorize] public class HelloControl ...

  5. npm cnpm node yarn

    1.yarn: windows 下需要下载msi文件, 2.npm,node 安装绿色版本 3.cnpm安装:npm install -g cnpm --registry=https://regist ...

  6. source insight和vim同时使用

    https://blog.csdn.net/wangn222/article/details/72721993 1.Source Insight中,Options->Custom Command ...

  7. 【android】获取本机ip地址

    方法是利用网址:http://pv.sohu.com/cityjson?ie=utf-8,返回String类型的ip地址: public static String getNetIp() { Stri ...

  8. 1103 Integer Factorization (30)

    1103 Integer Factorization (30 分)   The K−P factorization of a positive integer N is to write N as t ...

  9. base64图片下载

    下面这种写法有些chrome不起作用 downLoadCanvas (data, filename = '活动.png') { var saveLink = document.createElemen ...

  10. python基础讲解部分&纯小白需要扎实基础

    第一章知识点 一.Python简介 ​ python的创始人为吉多·范罗苏姆(Guido van Rossum),在中国人称龟叔 ​ Python崇尚优美.清晰.简单 应用领域: ​ (1)云计算,写 ...