Function Run Fun-递归+细节处理
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
Output
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-递归+细节处理的更多相关文章
- POJ 1579 Function Run Fun 【记忆化搜索入门】
题目传送门:http://poj.org/problem?id=1579 Function Run Fun Time Limit: 1000MS Memory Limit: 10000K Tota ...
- 洛谷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> ...
- poj 1579 Function Run Fun 【记忆化递归】
<题目链接> 题目大意: 给出一些递归式,直接套用这些递归式计算. 解题分析: 递归式已经由题目明确说明了,但是无脑递归铁定超时,所以此时,我们需要加上记忆化,对于那些已经算过的,就没有必 ...
- POJ 1579 Function Run Fun 记忆化递归
典型的记忆化递归问题. 这类问题的记忆主要是利用数组记忆.那么已经计算过的值就能够直接返回.不须要进一步递归了. 注意:下标越界.递归顺序不能错,及时推断是否已经计算过值了,不要多递归. 或者直接使用 ...
- HDU 1331 Function Run Fun(记忆化搜索)
Problem Description We all love recursion! Don't we? Consider a three-parameter recursive function w ...
- POJ1579:Function Run Fun
Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c ...
- hdu1579 Function Run Fun(深搜+记忆化)
版权声明:本文为博主原创文章.未经博主同意不得转载.vasttian https://blog.csdn.net/u012860063/article/details/37076755 转载请注明出处 ...
- hdu 1331 Function Run Fun
Problem Description We all love recursion! Don't we? Consider a three-parameter recursive function w ...
- 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 ...
随机推荐
- DesktopLoader服务程序
program DesktopLoader; //{$APPTYPE CONSOLE} uses Windows,WinSvc,ShellApi; var s:String; iDesktops,jD ...
- 合并vector里的内容,输出一个string
string merge_vector(vector<string> dp_scpe_all) { //合并vector里的内容 string new_dp_scpe; ; m < ...
- [NOIP模拟15]题解
A.建设城市(city) 这容斥题多难啊你们是怎么考场切掉的啊 首先可以想一下,如果没有k的限制,这题怎么做? 相信你们肯定能看出来是挡板法裸题:m个物品分给n个人,每个人至少一个. 就是$C_{m- ...
- (转)ubuntu下如何安装使用SSH?
转:http://os.51cto.com/art/201109/291634.htm ubuntu默认并没有安装ssh服务,如果通过ssh链接ubuntu(比如使用securecrt客户端来访问ub ...
- cgo 和 Go 语言是两码事
cgo不是Go 借用 JWZ的一句话 有些人,当他们面临一个问题时,认为“我知道,我会使用 cgo ”.那么现在,他们有了两个问题. 最近有人在 Gopher 的 Slack Channel 上使用 ...
- libcmt.lib(crt0dat.obj) : error LNK2005: _amsg_exit 已经在 MSVCRTD.lib(MSVCR110D.dll) 中定义
问题描述(VC2012): 1>MSVCRTD.lib(cinitexe.obj) : warning LNK4098: 默认库"libcmt.lib"与其他库的使用冲突:请 ...
- linux jps命令
原文链接: http://www.cnblogs.com/qlqwjy/p/7928410.html https://blog.csdn.net/u013250071/article/details/ ...
- 7、jmeter-定时器介绍与使用
jmeter-定时器介绍与使用 固定定时器 Uniform Random Timer Precise Throughput Timer Constant Throughput Timer 高斯随机定时 ...
- vbs 之 wscript
https://www.jb51.net/article/20919.htm '''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' ...
- Linux启动过程的C语言代码分析
1. main函数 参见上方http://www.cnblogs.com/long123king/p/3543872.html,代码跳转到main函数. arch/x86/boot/main.c 1: ...