Function Run Fun
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 14815 Accepted: 7659

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

解题心得:

1、这是一个很经典的记忆化搜索,算是记忆化搜索之中最简单的。其实记忆化搜索就是dp,从小到大以此往上转移。就这个题本身来说是一个很很经典的从下到上的递推。

2、题目描述也很清楚,下一个的结果要从上一个的结果中的出,如果下一个的结果未知,那么就会产生大量的递归然后复杂度爆炸,由前到后依次得到答案,上一个答案从下一个中得出,就是动态规划一个很明显的特征。

#include<cstring>
#include<stdio.h>
const int maxn = 25;
struct num
{
bool flag[maxn][maxn][maxn];
int Num[maxn][maxn][maxn];
} N;//记录出现过的结果 int w(int a,int b,int c)
{
//根据题意就好
if(a <= 0 || b <= 0 || c <= 0)
return 1;
if(a > 20 || b > 20 || c > 20)
return w(20,20,20);
if(N.flag[a][b][c])
return N.Num[a][b][c];
if(a<b && b<c)
return w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c); return w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1);
}
int main()
{
int a,b,c; //预处理一下
for(int i=1; i<=20; i++)
for(int j=1; j<=20; j++)
for(int k=1; k<=20; k++)
{
a = w(i,j,k);
N.flag[i][j][k] = true;
N.Num[i][j][k] = a;
} while(scanf("%d%d%d",&a,&b,&c))
{
if(a == -1 && b == -1 && c == -1)
break;
int ans = w(a,b,c);
printf("w(%d, %d, %d) = %d\n",a,b,c,ans);
}

记忆化搜索:POJ1579-Function Run Fun(最基础的记忆化搜索)的更多相关文章

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

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

  2. POJ1579:Function Run Fun

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

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

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

  4. 洛谷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> ...

  5. EasyUI基础searchbox&amp;progressbar(搜索框,进度条)

    easyui学习的基本组成部分(八个部分)硕果仅存searchbox和pargressbar.tooltip该,有一点兴奋.本文将偏向searchbox和pargressbar做一个探讨.鉴于双方的内 ...

  6. SaltStack 是一个服务器基础架构集中化管理平台

    SaltStack详细部署   一.基础介绍============================================================================== ...

  7. JAVA基础4---序列化和反序列化深入整理(JDK序列化)

    一.什么是序列化和反序列化? 序列化:将对象状态信息转化成可以存储或传输的形式的过程(Java中就是将对象转化成字节序列的过程) 反序列化:从存储文件中恢复对象的过程(Java中就是通过字节序列转化成 ...

  8. 搜索实时个性化模型——基于FTRL和个性化推荐的搜索排序优化

    本文来自网易云社区 作者:穆学锋 简介:传统的搜索个性化做法是定义个性化的标签,将用户和商品通过个性化标签关联起来,在搜索时进行匹配.传统做法的用户特征基本是离线计算获得,不够实时:个性化标签虽然具有 ...

  9. [Songqw.Net 基础]WPF插件化中同步Style

    原文:[Songqw.Net 基础]WPF插件化中同步Style 版权声明:本文为博主原创文章,未经博主允许可以随意转载 https://blog.csdn.net/songqingwei1988/a ...

随机推荐

  1. 创建有输出参数的存储过程并在c#中实现DataGridView分页功能

    不足之处,欢迎指正! 创建有输出参数的存储过程 if exists(select * from sysobjects where name='usp_getPage1') drop procedure ...

  2. 【踩坑】遇到 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 报错

    今天在重做 iblog 客户端时,测试接口情况,发现了 org.apache.ibatis.binding.BindingException: Invalid bound statement (not ...

  3. javascript浮点值运算舍入误差

    问题 在javascript中整数和浮点数都属于Number数据类型(简单数据类型中的一种),我们经常会发现在打印1.0这样的浮点数的结果是1而非1.0,这是由于保存浮点数的内存空间是保存整数值的两倍 ...

  4. 返回mapcontrol上的已被选择的element

     IGraphicsContainerSelect.SelectedElement

  5. EF中Entity SQL用法

    public List<policecase> GetPoliceCaseList(PoliceCaseFilter view)        {            string sq ...

  6. Linux下安装maven和nexus

    Linux下安装maven1.首先到Maven官网下载安装文件,目前最新版本为3.0.3,下载文件为apache-maven-3.0.3-bin.tar.gz,下载可以使用wget命令: 2.进入下载 ...

  7. openstack RuntimeError: Unable to create a new session key. It is likely that the cache

    [Mon Apr 15 01:02:31.654247 2019] [:error] [pid 19433:tid 139790082479872] Login successful for user ...

  8. android RadioGroup设置某一个被选中

    见码滚 mPriorityRadioGroup.clearCheck(); mStatusRadioGroup.clearCheck(); RadioButton r1 = (RadioButton) ...

  9. myeclipse引入工程后运行出错

    An internal error occurred during: Launching efax on Tomcat 7.x . 项目运行时报错 因为你项目建的时候用的是Tomcat5.x 服务器 ...

  10. 2018.1.4 UML 第三章 用例图

    第三章 用例图 (1)参与者 是指系统以外的需要使用系统或与系统交互的外部实体,吧阔人.设备.外部系统等. (2)参与者之间的关系 泛化关系的含义是参与者的共同行为提取出来表示成通用行为,并描述成超类 ...