Function Run Fun

POJ - 1579

AYIT-2021 609暑假集训第一周下 记忆化搜索和背包

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<string.h>
int dp[500][500][500];
int w(int a,int b,int c)
{if(a>=0&&b>=0&&c>=0&&dp[a][b][c])return dp[a][b][c];//注意利用记忆数组dp时a,b,c不能为负数,利用逻辑运算符的短路运算,这些条件可以写在一个括号里if(a<=0||b<=0||c<=0)return 1;else if(a>20||b>20||c>20)return dp[a][b][c]=w(20,20,20);else if(a<b&&b<c)return dp[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);else return dp[a][b][c]=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;while(~scanf("%d%d%d",&a,&b,&c)&&(a+b+c)!=-3)printf("w(%d, %d, %d) = %d\n",a,b,c,w(a,b,c));return 0;
}

POJ - 1579 Function Run Fun相关推荐

  1. 记忆化搜索--poj 1597 Function Run Fun

    http://poj.org/problem?id=1579 如果按平常写会超时 long long dfs(int x, int y, int z) {if(x <= 0 || y <= ...

  2. POJ 1597 Function Run Fun

    记忆化搜索. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  3. POJ1579 HDU1331 HDU1579 ZOJ1168 Function Run Fun【记忆化递归】

    Function Run Fun Description We all love recursion! Don't we? Consider a three-parameter recursive f ...

  4. POJ 1579 记忆化搜索

    [问题描述] 自定义函数w(a,b,c). 如果 a ≤ 0 或b ≤ 0 或 c ≤ 0, 则返回结果: 1;  如果 a > 20 或 b > 20 或 c > 20, 则返回结 ...

  5. (精)【ACM刷题之路】POJ题目详细多角度分类及推荐题目

    POJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094) 初期: ...

  6. POJ刷题列表——正在进行

    转载自:http://blog.csdn.net/pyub153/article/details/50480333 初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心 ...

  7. POJ前面的题目算法思路【转】

    1000 A+B Problem 送分题 49% 2005-5-7 1001 Exponentiation 高精度 85% 2005-5-7 1002 487-3279 n/a 90% 2005-5- ...

  8. poj题目详细分类及算法推荐题目

    DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  1024   Calendar Game       简单题  ...

  9. ACM POJ 题目分类(完整整理版本)

    DP: 1011   NTA                 简单题  1013   Great Equipment     简单题  1024   Calendar Game       简单题   ...

最新文章

  1. zabbix监控系统客户端安装
  2. python123作业答案第七周-python一周练习
  3. 15.profile-api
  4. (转)windows下安装python及第三方库numpy、scipy、matplotlib终极版
  5. React开发(244):dva概念8router
  6. 两套苹果CMS付费模板:MXone自适应模板+仿电影先生2.0
  7. 设置 webbrowser 控件的表现形式,可以像你本机的IE浏览器一样
  8. 常见List面试问题
  9. android studio for android learning (二十五 )activity的启动模式详解与标志位简析
  10. Linux建立虚拟ip的方法
  11. 你想要的宏基因组-微生物组知识全在这(2020.8)
  12. 80x86汇编小站站长简介-2014年08月23日
  13. 阿里云盘来袭,送几个福利码!手慢无!
  14. 使用计算机教学的好处,谈计算机在教学中的作用
  15. 近几天,被这个国家的最高科技奖刷屏了~
  16. python -m spacy dowmload en失败
  17. 动态拨号主机多IP随机分配拨号自动分配
  18. Jpress 企业简洁模板
  19. 【北交所周报】继北证50指数后,北交所推出融资融券交易细则;新股慧为智能上市当日收涨22.13%,远航精密上市即破发;...
  20. java中使用ajax请求数据格式化,ajax请求服务器返回json数据格式化

热门文章

  1. 图像处理之opencv保存视频图片
  2. PyCharm中PEP8:W292警告
  3. linux 无法安全移除u盘,U盘无法安全移除?5招轻松搞定
  4. 串口芯片型号8250、16550A等是什么意思?
  5. 毕业设计 STM32空气质量检测仪 - 单片机 嵌入式
  6. 汽车毫米波雷达的规定和标准(四)
  7. 中国 邮政编码四级结构,省(省无邮政编码)、市、区、街道邮政编码,在线api获取
  8. 某游戏社区App | So层逆向分析
  9. ubuntu上编译stepmania
  10. 赞爆!全国计算机专业数据库系统工程师考试指定教程(第三版)