UVA10254 - The Priest Mathematician(找规律)
2014-11-18 21:09
405 查看
UVA10254 - The Priest Mathematician(找规律)
题目链接
题目大意:4根柱子的汉诺塔。
解题思路:题目里面有提示,先借助四个柱子移走k个,然后在借助三个柱子移走剩余的n - k个,再把n个移动到n - k个所在柱子。那么F
= min(2 * F[k] + H[n - k]);H[n - k] = 2^(n - k) - 1;把前面的60项打出来,再打印出F
- f[n - 1],会发现规律:
F[1] = 1;
F[2] = F[1] + 2^1;
F[3] = F[2] + 2^1;(2个)
f[4] = f[3] + 2^2;
f[5] = f[4] + 2^2;
f[6] = f[5] + 2^2;(3个)
F[7] = f[6] + 2^3;
... (4个)
但是n到达10000,结果要用到大数。
代码:
题目链接
题目大意:4根柱子的汉诺塔。
解题思路:题目里面有提示,先借助四个柱子移走k个,然后在借助三个柱子移走剩余的n - k个,再把n个移动到n - k个所在柱子。那么F
= min(2 * F[k] + H[n - k]);H[n - k] = 2^(n - k) - 1;把前面的60项打出来,再打印出F
- f[n - 1],会发现规律:
F[1] = 1;
F[2] = F[1] + 2^1;
F[3] = F[2] + 2^1;(2个)
f[4] = f[3] + 2^2;
f[5] = f[4] + 2^2;
f[6] = f[5] + 2^2;(3个)
F[7] = f[6] + 2^3;
... (4个)
但是n到达10000,结果要用到大数。
代码:
import java.util.*; import java.math.*; import java.io.*; public class Main { static BigInteger f[] = new BigInteger[10005]; public static void init () { f[0] = BigInteger.ZERO; f[1] = BigInteger.valueOf(1); int k = 1, j = 2; BigInteger addnum; while (j <= 10000) { addnum = BigInteger.valueOf(1).shiftLeft(k); for (int i = 0; i < k + 1 && j <= 10000; i++, j++) f[j] = f[j - 1].add(addnum); k++; } } public static void main(String args[]) { Scanner cin = new Scanner(System.in); init(); while (cin.hasNext()) { int n = cin.nextInt(); System.out.println(f ); } } }
相关文章推荐
- UVA 10254 - The Priest Mathematician (dp | 汉诺塔 | 找规律 | 大数)
- 递推+高精度+找规律 UVA 10254 The Priest Mathematician
- UVA 10254 - The Priest Mathematician (dp | 汉诺塔 | 找规律 | 大数)
- UVA 10254 - The Priest Mathematician(规律)
- uva 10254 - The Priest Mathematician
- uva 10254——The Priest Mathematician
- UVA 10254 The Priest Mathematician
- uva 10254 the priest mathematician
- uva 10254 - The Priest Mathematician(dp)
- uva 10254 The Priest Mathematician
- UVa Problem 10254 The Priest Mathematician (牧师数学家)
- UVa Problem Solution: 10254 - The Priest Mathematician
- UVA 10254 The Priest Mathematician
- UVA 10254-The Priest Mathematician(大数+递推)
- UVa 10254 - The Priest Mathematician (4柱汉诺塔)
- UVA 10254 十八 The Priest Mathematician
- UVALive 6345 The Glittering Caves of Aglarond (找规律求最多)
- UVa 11040 - Add bricks in the wall(规律)
- The Priest Mathematician
- Spreading the Wealth UVA - 11300 找规律+数学推导