UVA10254-The Priest Mathematician)
UVA10254-The Priest Mathematician)
Question Link
The tower of the four pillars.
Solution: There is a prompt in the question: First remove k with four pillars, and then remove the remaining n-k with the help of three pillars, then, move n to the column where n-k is located. Then F [n] = min (2 * F [k] + H [n-k]); H [n-k] = 2 ^ (n-k)-1; print out the previous 60 items and then print out F [n]-f [n-1]. The rule is found:
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)
However, if n reaches 10000, a large number is required.
Code:
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[n]); } }}