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. 使用正则表达式给网址添加a标签

    在内容中存在链接地址的时候,我们在前台显示时一定想自动的将地址添加上a标签,方便用户进入链接.使用正则表达式就能轻松实现. Jsvascript正则替换 //javascript 正则替换 var s ...

  2. 打勾显示输入的密码 --EditText与setTransformationMethod

    实现目标: 实现原理: 为CheckBox添加一个监听器事件; 实现的源码: package edu.cquptzx.showPassword; import android.app.Activity ...

  3. Java基础知识强化79:被遗忘的Java Math类

    1. Math类概述 Math类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数. 2. 成员变量 和 成员方法(常用的) (1)成员变量 public static final d ...

  4. ifconfig命令详解

    linux下网上命名规律:eth0,eth1.第一块以太网卡,第二块.lo为环回接口,它的IP地址固定为127.0.0.1,掩码8位.它代表你的机器本身. 1.ifconfig是查看网卡的信息 eth ...

  5. android系统360浏览器使用append动态添加元素出现黑色错乱背景

    去掉样式 backface-visibility:hidden;

  6. cassandra新增、更新、删除数据。

    package client; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java ...

  7. Dreamweaver管理Svn控制器内容

    一直以来很多人使用Dreamweaver来写css和xhtml.同时如果是应用于一个多人开发的项目的时候大家会使用svn或cvs来做版本控制的工作.但是可惜的是没有听说 Dreamweaver可以与版 ...

  8. linux学习笔记<命令介绍>

    主要介绍日常工作中一些常用的命令,内容均整理自慕课网 命令提示符 [root@hgs ~]# 其中: root : 当前登录用户 hgs : 主机名 ~ : 当前所在目录(家目录),对于root用户, ...

  9. 产品经理(PM)常用原型图设计工具

    本文转贴自:http://www.zhangping.name/2010/08/28/pm-wireframes-design-tools/ ,尽管都是一些商业软件,但对设计的确非常有帮助. 天天和产 ...

  10. 用CALayer实现淡入淡出的切换图片效果

    由于直接更改layer的contents显示的隐式动画切换的时候的动画持续时间没办法控制, 切换效果不尽人意,所以这里配合了CABasicAnimation实现淡入淡出的切换效果, 另外还可以使用组合 ...