Uva10862-connect the Cable Wires (recursive + large number of Java)

Source: Internet
Author: User

Uva10862-connect the Cable Wires (recursive + large number of Java)

Topic links

Title: give you n house in a straight line, and then just give you a cable service. Each house is required to be wired, indirectly through the direct neighbor of the House (provided that its neighbor is connected to the cable), and the second is the ability to connect directly to the cable service.

Problem-solving ideas: The back of the house is numbered 1, the front is N. If we're going to have a house number 1th, then it's three possible.


1, direct and cable service connection, then is to add f (n-1) species (front of the n-1 house of the Combination way).


2, and 2nd House connection does not and cable service connection, then again f (n-1) species.


3, and 2nd House link and Cable service connection, then this means that the 2nd House can not and cable service link, otherwise formed ring. This will allow numbers 1th and 2nd to be considered as a whole, and have an edge over the cable service. This situation can be divided into two types: (1), No and third, plus F (n-2).

(2) and number third, so that the situation goes back to 3, indicating that this is recursive.

So f (n) = 2 f (n-1) + f (n-2) + f (n-3) +: + F (1) + 1. (recursion to 1 only when there is no connection with the number 1th and 1th and then 1th and cable service both)
The same writes out F (n-1), subtracting: f (n) = 3 $*$ f (n-1)-F (n-2); (n > 2).
The boundary f (2) = 3, f (1) = 1;3 2000 will exceed long long, so use a large number.

Code:

Import Java.util.*;import Java.math.*;import java.io.*; Public classMain {StaticBigInteger f[] =Newbiginteger[2005]; Public Static voidInit () {f[1] = biginteger.valueof (1); f[2] = biginteger.valueof (3); for(inti =3; I <= -; i++) F[i] = biginteger.valueof (3). Multiply (F[i-1]). Subtract (F[i-2]); } Public Static voidMain (String args[]) {ScannerCin=NewScanner (system.in);intN Init (); while(true) {n =Cin. Nextint ();if(n = =0) Break;        System.out.println (F[n]); }    }}

Uva10862-connect the Cable Wires (recursive + large number of Java)

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.