UVA 10254-the Priest mathematician (large number + recursive)

Source: Internet
Author: User

Title: In the original Hanoi game based on a pillar, mobile strategy is: To move N Hanoi, first with 4 pillars of K to a pillar, and then use the remaining 3 to move the remaining n-k to the target column, and then use 4 to move the initial K to the target column.


The key question is how much to find the K of each n, the observed rule is: with the increment of K, in fact the number of moves FN (k) increment after decrement, then F1 (k), F2 (k),... The maximum value increases with the increment of K. It seems more difficult to formalize the proof. But within the scope of the topic this can be AC. The program maintains this currently makes Fi (k) the largest k, and then recursion.


Import Java.util.*;import java.math.*;p ublic class main{public static void Main (string[] args) {Scanner input=new Scanner (system.in); int u,n;final BigInteger two=new BigInteger ("2"); Biginteger[] A=new biginteger[210]; Biginteger[] D=new biginteger[10010]; BigInteger minp;a[0]=biginteger.zero;for (int i=1;i<=200;i++) {a[i]=a[i-1].multiply (both). Add (Biginteger.one);} D[0]=biginteger.zero;d[1]=biginteger.one;d[2]=new BigInteger ("3"); u=1;for (int i=3;i<=10000;i++) {Minp=d[u]. Multiply (both). Add (A[i-u]), while ((u<i-1) && (Minp.compareto (d[u+1].multiply)))) {minp=d[u+1].multiply (n). Add (A[i-u-1]); u++;} D[I]=MINP;} while (Input.hasnext ()) {n=input.nextint (); System.out.println (D[n]);}}


UVA 10254-the Priest mathematician (large number + recursive)

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.