UVA10254-The Priest Mathematician)

Source: Internet
Author: User

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]);        }    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.