ACM程序设计之DP.ppt

  1. 1、本文档共34页,可阅读全部内容。
  2. 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
ACM程序设计之DP

ACM程序设计 东北林业大学 陈宇 Lg_chenyu@ 今天你AC 了吗? 第7讲 DP(二) 我校的ACM在线评测系统 课件下载地址: /kj/acm06.ppt Function Run Fun nefu16 We all love recursion! Dont 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 思路:递归好 long long data[21][21][21]; long long inf(int x,int y,int z) { if (x=0||y=0||z=0) return 1; if (x20||y20||z20) return inf(20,20,20); if (xyyz) { if (data[x][y][z]==-1) data[x][y][z]=inf(x,y,z-1)+inf(x,y-1,z-1)-inf(x,y-1,z); return data[x][y][z]; } else { if (data[x][y][z]==-1) {data[x][y][z]=inf(x-1,y,z)+inf(x-1,y-1,z)+inf(x-1,y,z-1)-inf(x-1,y-1,z-1); } return data[x][y][z]; } } main()函数 int main(int argc, char *argv[]) { int x,y,z; while(cinxyz) { if (x==-1y==-1z==-1) break; memset(data,-1,sizeof(data)); coutw(x, y, z) = inf(x,y,z)endl; } //system(PAUSE); return EXIT_SUCCESS; } Recaman Sequence nefu91 The Recamans sequence is defined by a0 = 0 ; for m 0, am = am?1 ? m if the rsulting am is positive and not already in

您可能关注的文档

文档评论(0)

2017meng + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档