POJ1579:Function Run Fun
2024-10-21 06:37:50
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
Source
方法一:
直接递归会超时,预处理即可。实现把所有结果存到三维数组中。
#include <iostream>
#include <cstdio> using namespace std; int main()
{
int w[][][], ans, a, b, c;
//预处理
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
for(int k = ; k < ; k++)
{
if(!i || !j || !k) w[i][j][k] = ;
else if(i < j && j < k)
w[i][j][k] = w[i][j][k-] + w[i][j-][k-] - w[i][j-][k];
//w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)
else
w[i][j][k] = w[i-][j][k] + w[i-][j-][k] + w[i-][j][k-] - w[i-][j-][k-];
//w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)
}
while(scanf("%d %d %d", &a, &b, &c))
{
if(a == - && b == - && c == -) break;
if(a <= || b <= || c <= ) ans = ;
else if(a > || b > || c > ) ans = w[][][];
else ans = w[a][b][c];
printf("w(%d, %d, %d) = %d\n", a, b, c, ans);
}
return ;
}
方法二:
总之要先把之前运算出来的结果存到三维数组中,避免重复运算。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int dp[][][]; int dfs(int a,int b,int 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)
dp[a][b][c] = dfs(a,b,c-)+dfs(a,b-,c-)-dfs(a,b-,c);
else
dp[a][b][c] = dfs(a-,b,c)+dfs(a-,b-,c)+dfs(a-,b,c-)-dfs(a-,b-,c-);
return dp[a][b][c];
} int main()
{
int a,b,c;
memset(dp,,sizeof(dp));
while(~scanf("%d%d%d",&a,&b,&c))
{
if(a == - && b == - && c == -)
break;
printf("w(%d, %d, %d) = %d\n",a,b,c,dfs(a,b,c));
} return ;
}
最新文章
- 职业规划:管理vs技术
- wangEditor ie9 表单上传图片
- 个性二维码开源专题<;替换定位点>;
- win下 golang 跨平台编译
- 第一章 01 namespace 命名空间
- ASP.NET 资料下载
- Forward reference vs. forward declaration
- C# 实现3Des加密 解密
- SpringMVC中重定向底层原理
- php_类的定义
- LIRe 源代码分析 1:整体结构
- api_response.go
- sping配置头文件
- Shiro学习
- B-Tree 和 B+Tree
- jenkins构建完成后,执行的命令行的东西也会自动结束的解决办法
- 【转】C++ map的基本操作和使用
- systemd设置静态IP
- diffutils&#39;s diff
- java内存中的对象
热门文章
- 【Linux】echo命令
- java 生成可执行jar包
- 转:【HTTP】常见错误码说明
- 使用JavaStcript对数组元素去重的方法
- php数据库操作类(转)
- Linux时间子系统(十二) periodic tick
- InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings In
- 赶集mysql军规
- Android开发7——android.database.CursorIndexOutOfBoundsException:Index -1 requested
- angular.js快速入门 hello world