UVA10254 The Priest Mathematician大数+递推
Posted 海岛Blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10254 The Priest Mathematician大数+递推相关的知识,希望对你有一定的参考价值。
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.
F
i
g
:
T
h
e
F
o
u
r
N
e
e
d
l
e
(
P
e
g
)
T
o
w
e
r
o
f
H
a
n
o
i
Fig: The Four Needle (Peg) Tower of Hanoi
Fig:TheFourNeedle(Peg)TowerofHanoi
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, 7 minutes and 13 seconds against the more than 500000 millions years without the additional needle (as they would have to do 264 − 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. Here 0 ≤ 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
问题链接:UVA10254 The Priest Mathematician
问题简述:(略)
问题分析:递推+大数问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的Java语言程序如下:
/* UVA10254 The Priest Mathematician */
import java.math.BigInteger;
import java.util.Scanner;
public class Main
public static void main(String args[])
int N = 10000;
BigInteger f[] = new BigInteger[N + 1];
f[0] = BigInteger.ZERO;
f[1] = BigInteger.ONE;
int k = 1;
for (int i = 2; i <= N; k++)
BigInteger add = BigInteger.ONE.shiftLeft(k);
for (int j = 0; j < k + 1 && i <= N; j++)
f[i] = f[i - 1].add(add);
i++;
Scanner cin = new Scanner(System.in);
while (cin.hasNext())
int n = cin.nextInt();
System.out.println(f[n]);
cin.close();
以上是关于UVA10254 The Priest Mathematician大数+递推的主要内容,如果未能解决你的问题,请参考以下文章
UVA10254 - The Priest Mathematician(找规律)