UVa 10254 The Priest Mathematician:组合数学&规律发现&高精度

10254 - The Priest Mathematician

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=34&page=show_problem&problem=1195
The ancient folklore behind the "Towers of Hanoi" puzzle invented by E. Lucas in 1883 is quite well known to us. One more recent legend tells us that the Brahmin monks from Benares never believed that the world could vanish at the moment they finished to transfer the 64 discs from the needle on which they were to one of the other needles, and they decided to finish the task as soon as possible.

Fig: The Four Needle (Peg) Tower of Hanoi

One of the priests at the Benares temple (who loved the mathematics) assured their colleagues to achieve the transfer in the afternoon (the rhythm they had thought was a disc-per-second) by using an additional needle. They couldn't believe him, but he proposed them the following strategy:

-- First move the topmost discs (say the top k discs) to one of the spare needles.

-- Then use the standard three needles strategy to move the remaining n-k discs (for a general case with n discs) to their destination.

-- Finally, move the top k discs into their final destination using the four needles.

He calculated the value to k in order to minimize the number of movements and get a total of 18433 transfers, so they spent just 5 hours, 7minutes and 13 seconds against the more than 500000 millions years without the additional needle (as they would have to do 2^64-1 disc transfers. Can you believe it?)

Try to follow the clever priest's strategy and calculate the number of transfer using four needles but according with the fixed and immutable laws of Brahma, which require that the priest on duty must not move more than one disc at a time and that he must place this disc on a needle so that there is no smaller disc below it. Of course, the main goal is to calculate the k that minimize the number of transfers (even thought it is not know for sure that this is always the optimal number of movements).

Input

The input file contains several lines of input. Each line contains a single integer N, which is the number of disks to be transferred. Here0<=N<=10000. Input is terminated by end of file.

Output

For each line of input produce one line of output which indicates the number of movements required to transfer the N disks to the final needle.

Sample Input:

1

2

28

64

Sample Output:

1

3

769

18433

思路:

由题目给出的策略得:

f(1)=1, f(n)=min{2*f(k)+2^(n-k)-1} (1<=k<=n-1)

打印前面几项:1,3,5,9,13,17,25,33,41,49,65,...

计算其一阶差分序列Δf(n):2,2,4,4,4,8,8,8,8,16,...

发现2^m出现了(m+1)次,m>=0

以此为依据预处理所有解。

更多的讨论:《具体数学》P16第17题、第25题

时间: 2024-08-03 22:05:33

UVa 10254 The Priest Mathematician:组合数学&amp;规律发现&amp;高精度的相关文章

UVA 10254

题意: 汉诺塔游戏请看 百度百科:http://baike.baidu.com/view/191666.htm 正常的汉诺塔游戏是只有3个柱子,并且如果有n个圆盘,至少需要2^n-1步才能达到目标. 但是在 这题中,有4根柱子,并且按照下面规则来玩: 1. 先把圆盘顶部前k个盘子全部搬到第四根柱子上, 2. 然后把剩下的n-k个盘子在前3根柱子中按照经典的规则搬到某个柱子上(假设是a柱), 3. 最后再把那k个盘子搬到目标a柱上. 问按照这样的规则,最少需要几步? 思路: 我们先设g[n]表示按

UVa 10247 Complete Tree Labeling:组合数学&amp;amp;高精度

10247 - Complete Tree Labeling Time limit: 15.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1188 A complete k-ary tree is a k-ary tree in which all leaves have same d

UVa 10334 Ray Through Glasses (斐波那契&amp;amp;高精度)

10334 - Ray Through Glasses Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1275 Suppose we put two panes of glass back-to-back. How many ways are there for light rays to pass through or

intelligentminer数据仓库解决方案

当用户的数据积累到一定数量时,这些数据的某些潜在联系.分类.推导结果和待发现价值隐藏在其中,我们可以使用数据发掘工具帮助发现这些有价值的数据,ibm在这方面的工具就是intelligentminer.ibmintelligentminer被选为业界最佳数据采集工具,赢得了dm读者奖.除了数据仓库和数据挖掘解决方案,ibm还在此基础上开发了一系列行业解决方案及应用程序. 1.ibm数据挖掘工具 intelligentminer通过其世界领先的独有技术,例如典型数据集自动生成.关联发现.序列规律发现

利用HttpURLConnection发送post请求上传多个文件

本文要用java.net.HttpURLConnection来实现多个文件上传 1. 研究 form 表单到底封装了什么样的信息发送到servlet. 假如我参数写的内容是hello word,然后二个文件是二个简单的txt文件,form提交的信息为: [xhtml] view plaincopy -----------------------------7da2e536604c8     Content-Disposition: form-data; name="username" 

我们需要怎样的OLAP?

被狭义化的OLAP OLAP是商业智能应用中重要的组成部分,这个词从字面上理解是在线分析的意思,也就是由用户,特别是业务人员,面对数据进行各种分析操作. 但是,现在的OLAP概念被严重狭义化了.说到OLAP,基本上仅指多维分析,也就是针对一个事先建设好的数据立方体,按指定维度层次进行汇总并呈现成表格或图形,再辅以钻取.聚合.旋转.切片等操作以变换维度层次及汇总范围.多维分析的基本思路认为,直接观察大范围统计值过于粗略,无法精确定位问题,需要剥茧抽丝似地对可能有问题的大范围统计值一步步钻取到更细层

关于医疗大脑、知识图谱与智能诊断,这是最全的解读 | 硬创公开课

雷锋网按:本文整理自康夫子创始人张超在雷锋网(公众号:雷锋网)硬创公开课上的演讲,主题为"智能诊断与医疗大脑". 张超:康夫子创始人,前百度自然语言处理部资深研发工程师.文本知识挖掘方向负责人:知识图谱.实体建模方面专家:毕业于电子科技大学计算数学专业.新加坡国大多媒体搜索实验室研究助理. 以下为公开课内容: 雷锋网:简单介绍一下康夫子所做的事. 张超:让计算机去阅读医疗文献,构建知识库,赋予这些知识库一些推理能力,最后达到辅助医生.患者的目的. 在产品维度,分为面向患者和医生:医生端

量化分析机器与人类智慧

未来的智慧世界应该是机器与人类的分工,低端重复性的智能由机器承担,高端的创造性的智能由人类来承担.过分的宣扬机器智慧超越人类智慧,都会带来盲目乐观到不理智甚至沮丧的结果. 1.关于机器与人类智慧未来的分歧 2011年2月18日,超级电脑"沃森"打败了人类,站在了与人类智力竞赛的最高领奖台上.著名的未来学家库兹韦尔相信,由于信息技术正朝着"超人类智能"的奇点迈进.当这个信息奇点在2045年到来的时候,人工智能将超越人类智慧. 但也有不少科学家认为机器智慧超越人类智慧还

关于用 java 实现算法编程的困惑

问题描述 嗯,没错,我们都知道c,c++等机器执行效率很高,java在这方面没法比,但是我只喜欢java,最近在poj上做题,由于我是菜鸟,就挑简单点的做,这不,今天碰到的个非常水的题,如下:http://poj.org/problem?id=1953,这个题,想了会,找了会规律发现,根本不用去遍历,这个题就是个典型的数学题,小学生都会,如下:n=1answer=2n=2answer=3n=3answer=5n=4answer=8n=5answer=13呵呵,看到规律了吧,我还是不放心,我用遍历