POJ - 1579 Function Run Fun
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:
1if 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相关推荐
- 记忆化搜索--poj 1597 Function Run Fun
http://poj.org/problem?id=1579 如果按平常写会超时 long long dfs(int x, int y, int z) {if(x <= 0 || y <= ...
- POJ 1597 Function Run Fun
记忆化搜索. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- POJ1579 HDU1331 HDU1579 ZOJ1168 Function Run Fun【记忆化递归】
Function Run Fun Description We all love recursion! Don't we? Consider a three-parameter recursive f ...
- POJ 1579 记忆化搜索
[问题描述] 自定义函数w(a,b,c). 如果 a ≤ 0 或b ≤ 0 或 c ≤ 0, 则返回结果: 1; 如果 a > 20 或 b > 20 或 c > 20, 则返回结 ...
- (精)【ACM刷题之路】POJ题目详细多角度分类及推荐题目
POJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094) 初期: ...
- POJ刷题列表——正在进行
转载自:http://blog.csdn.net/pyub153/article/details/50480333 初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心 ...
- POJ前面的题目算法思路【转】
1000 A+B Problem 送分题 49% 2005-5-7 1001 Exponentiation 高精度 85% 2005-5-7 1002 487-3279 n/a 90% 2005-5- ...
- poj题目详细分类及算法推荐题目
DP: 1011 NTA 简单题 1013 Great Equipment 简单题 1024 Calendar Game 简单题 ...
- ACM POJ 题目分类(完整整理版本)
DP: 1011 NTA 简单题 1013 Great Equipment 简单题 1024 Calendar Game 简单题 ...
最新文章
- zabbix监控系统客户端安装
- python123作业答案第七周-python一周练习
- 15.profile-api
- (转)windows下安装python及第三方库numpy、scipy、matplotlib终极版
- React开发(244):dva概念8router
- 两套苹果CMS付费模板:MXone自适应模板+仿电影先生2.0
- 设置 webbrowser 控件的表现形式,可以像你本机的IE浏览器一样
- 常见List面试问题
- android studio for android learning (二十五 )activity的启动模式详解与标志位简析
- Linux建立虚拟ip的方法
- 你想要的宏基因组-微生物组知识全在这(2020.8)
- 80x86汇编小站站长简介-2014年08月23日
- 阿里云盘来袭,送几个福利码!手慢无!
- 使用计算机教学的好处,谈计算机在教学中的作用
- 近几天,被这个国家的最高科技奖刷屏了~
- python -m spacy dowmload en失败
- 动态拨号主机多IP随机分配拨号自动分配
- Jpress 企业简洁模板
- 【北交所周报】继北证50指数后,北交所推出融资融券交易细则;新股慧为智能上市当日收涨22.13%,远航精密上市即破发;...
- java中使用ajax请求数据格式化,ajax请求服务器返回json数据格式化