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. 解决SQLite中的 database is locked

    前些时候,同事在站点服务端使用SQlite存储一些临时数据,但是在多人并发的时候Sqlite会抛出异常:The database file is locked , database is locked ...

  2. Service6

    rsync同步操作 同步 : 只传输变化的数据     复制:完整的传输 • 命令用法– rsync [选项...] 源目录 目标目录 • 同步与复制的差异– 复制:完全拷贝源到目标– 同步:增量拷贝 ...

  3. CF 600E. Lomsat gelral(dsu on tree)

    解题思路 \(dsu\) \(on\) \(tree\)的模板题.暴力而优雅的算法,轻儿子的信息暴力清空,重儿子的信息保留,时间复杂度\(O(nlogn)\) 代码 #include<iostr ...

  4. linq中如何合并多个predicate条件

    最近在做一个webAPI 的时候遇到一个需要合并多个predicate条件的问题,下面就是对题的情况.为了方便交流我对case进行了简化,请先看如下代码: using System.Collectio ...

  5. thinkphp5操作redis系列教程】列表类型之lRange,lGetRange

    <?php namespace app\admin\controller; use think\cache\driver\Redis; use think\Controller; use \th ...

  6. Openstack贡献者须知 — OpenPGP/SSH/CLA贡献者协议

    目录 目录 前言 Openstack基金委员会 Openstack贡献者须知 注册Openstack In Launchpad 生成并上传OpenPGP密钥 生成并上传SSH公钥 Join The O ...

  7. Codeforces 1189C Candies!

    题目链接:http://codeforces.com/problemset/problem/1189/C 思路:前缀和. AC代码: #include<bits/stdc++.h> usi ...

  8. css3继承

    不可继承的:display.margin.border.padding.background.height.min-height.max- height.width.min-width.max-wid ...

  9. erlang在windows下和虚拟机节点通信

    版权声明:博客将逐步迁移到 http://cwqqq.com https://blog.csdn.net/cwqcwk1/article/details/24738599 在Linux下部署erlan ...

  10. 2018 最新 spring boot 整合 swagger2 (swagger2 版本 2.8.0)

    好久没上了, 看到又有人回复了. 我就来修改一下. 修改时间  2018年5月16日 这回给你上全新版本. 至发稿时间,所有的包都是新版. 注意: 高版本需要添加  jaxb-api 包, 否则会报错 ...