`
Dev|il
  • 浏览: 126230 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

POJ1579

 
阅读更多
http://poj.org/problem?id=1579
Function Run Fun
Time Limit: 1000MS  Memory Limit: 10000K
Total Submissions: 11121  Accepted: 5815

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

从题意很容易写出代码,但是超时,想了想,用记忆性搜索可以很好的解决此题

#include <stdio.h>
#include <math.h>
#include <string.h>

int dp[21][21][21];

int w(int a, int b, int c)
{
	if(a <= 0 || b <= 0 || c <= 0)
	{
		return 1;
	}else if(a > 20 || b > 20 || c > 20)
	{
		return 1048576;
	}else if(a <= b && b <= c)
	{
		if(dp[a][b][c] == -1)
			dp[a][b][c] = (int)pow(2, a);
		return dp[a][b][c];
	}else
	{
		if(a-1 >= 0&&dp[a-1][b][c] == -1)
			dp[a - 1][b][c] = w(a-1, b, c);
		if(a - 1 >= 0 && b - 1 >= 0 &&dp[a-1][b - 1][c] == -1)
			dp[a- 1][b - 1][c] = w(a-1, b- 1, c);
		if(a- 1 >= 0 && c - 1 >= 0 && dp[a - 1][b][c - 1] == -1)
			dp[a- 1][b][c - 1] = w(a - 1, b, c - 1);
		if(a - 1 >= 0 && b - 1 >= 0 && c - 1 >= 0)
			dp[a- 1][b - 1][c - 1] = w(a - 1, b - 1, c - 1);
     	return dp[a - 1][b][c] + dp[a- 1][b - 1][c] + dp[a- 1][b][c - 1] - dp[a- 1][b - 1][c - 1];
	}
}

//此题有如下规则:
//1.当a b c中有任意一个数大于20 则返回1048576
//2.如果 a<=b<=c 则返回 pow(2, a)
//3.其他的数据可以用记忆性搜索来提高时间
int main()
{
	int a, b, c;
	memset(dp, -1, sizeof(dp));
	while(scanf("%d%d%d", &a, &b, &c) == 3)
	{
		if(a == -1 && b == -1&& c== -1)
			break;
		printf("w(%d, %d, %d) = %d\n", a, b, c, w(a, b, c));
	}
	return 0;
}
分享到:
评论

相关推荐

    acm集训题目列表

    POJ中的许多题目,如POJ1014、POJ1579等,可以使用动态规划来求解。 数据结构是程序设计的基础,它包括但不限于堆栈、队列、优先级队列、排序、哈夫曼树、二叉树、二叉搜索树、并查集、字典树、哈希表、线段树、RMQ...

    北大POJ部分题目答案(一些基础题目)

    很多的POJ题目答案!1000~1008,1011~1014,1016,1017,1019,1028,1032,1045,1046,1047,1050,1061,1067,1068,1088,1102,1159,1163,1183,1207,1218,1226,1247,1256,1258,1298,1316,1323,...

    POJ各题算法分类和题目推荐 ACM必看

    * 1579 Function Run Fun:本题目使用动态规划来计算函数的执行次数。 * 1887 Testing the CATCHER:本题目使用动态规划来计算测试结果的可能性。 * 1953 World Cup Noise:本题目使用动态规划来计算世界杯比赛的...

    poj上算法题目分类

    - 1037, 1050, 1088, 1125, 1141, 1159, 1160, 1163, 1458, 1579, 1887, 1953, 2386 **关键知识点:** - **贪心算法**:在每一步都选择局部最优解。 - **回溯算法**:采用试探性的策略来搜索所有可能的解决方案。 -...

    poj经典动态规划题目解题报告

    poj经典动态规划题目解题报告,包括经典的动态规划题目20多道,可以作为学习动态规划系统的资料,包括题目: Pku acm 1179 Polygon Pku acm 1125 Stockbroker Grapevine Pku acm 1160 post office Pku ...

    poj各种题型详细分类

    - **1579 函数运行**:可能需要使用到DFS进行状态探索。 ### 三、贪心算法类题目 这类题目的特点是每一步都选择当前看起来最优的选择,最终达到全局最优解。 #### 例题 - **1050 至最大限度**:通过贪心策略实现...

    poj题目分类...

    * 1579 Function Run Fun * 1887 Testing the CATCHER * 1953 World Cup Noise * 2386 Lake Counting 简单、模拟题 简单、模拟题是 POJ 上的基础题目,它们通常不需要复杂的算法设计和实现。以下是一些推荐的题目...

    POJ PKU 必做题+部分难题1001-2500

    1011,1012,1013,1014,1015,1017,1026,1028,1032,1035,1041,1046,1129 1149 1154 1165 1182 1185 1190 1191 1201 1251 1273 1275 1276 1286 1322 1338 1363 1364 1401 1456 1459 1564 1579 1637 1657 1658 ...

    poj100题解。具体题号见说明

    1000 1002 1003 1004 1005 1006 1007 1008 1011 1012 1013 1019 1028 1045 1046 1068 1080 1088 1163 1207 1218 1256 1298 1299 1316 1326 1401 1455 1477 1488 1503 1504 1517 1519 1547 1552 1565 1579 1607 1656 ...

    POJ ACM题目分类.

    在ACM(国际大学生程序设计竞赛)中,POJ(Problemset Online Judge)是一个常用的在线评判系统,提供了大量的编程题目供参赛者练习和比赛。这些题目涵盖了多种算法和编程技巧,帮助参赛者提升解决问题的能力。根据提供...

    acm poj 源代码

    1001 1002 1003 1004 1005 1006 1007 1008 1011 1012 1013 1014 1015 1017 ...1579 1597 1604 1609 1631 1656 1657 1658 1661 1664 1665 1666 1674 1692 1717 1731 1742 1745 1753 1789 1797 1828 1833 1836 1861 1887 ...

    POJ分类题(按照算法分类)

    22. 1579FunctionRunFun:可能涉及函数的模拟。 23. 1595PrimeCuts:关于质数的题目。 24. 1597UniformGenerator:关于均匀随机数生成器的问题。 25. 1664放苹果:可能是一个与放置问题相关的题目。 26. 1665...

    poj pku 解题报告

    1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1011 1012 1013 1014 ...1579 1651 1654 1655 1656 1658 1659 1663 1664 1699 1700 1703 1716 1730 1737 1740 1753 1771 1797 1799 1804 1833 1837 1840 1844 1861 ...

    poj135道题的代码

    1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1011 1012 1013 1014 ...1579 1658 1664 1700 1703 1730 1737 1740 1799 1833 1949 1979 1988 1989 2000 2027 2033 2084 2136 2182 2229 2236 2249 2309 2336 2353 ...

    北京大学acm题库 题目分类

    北京大学ACM题库分类是适合想做ACM题的人的题目分类,分类详细,涵盖了POJ(PKU ACM Online Judge)上的题目分类。该分类涵盖了多种算法和数据结构,包括排序、搜索、回溯、遍历、历法、枚举、数据结构的典型算法、...

Global site tag (gtag.js) - Google Analytics