UVA 10862 Connect The Cable wires large integer class C + +

Source: Internet
Author: User

1:1

2:1+ (+)

3:1+2+ (2+3)

4:1+2+5+ (5+8)

and Fibonacci Series 1 1 2 3 5 8 ...

Hence the introduction of a[n]=a[n-1]+fib[2*i-1]+fib[2*1-2];

Java code

ImportJava.util.*;Importjava.math.*; Public classMain { Public Static voidMain (String args[]) {BigInteger [] ans=Newbiginteger[2010]; ans[1]=NewBigInteger ("1"); ans[2]=NewBigInteger ("3"); BigInteger [] fib=Newbiginteger[5020]; fib[1]=NewBigInteger ("1"); fib[2]=NewBigInteger ("1");  for(inti=3;i<5020;i++) {Fib[i]=fib[i-1].add (fib[i-2]); }         for(inti=3;i<2010;i++) {Ans[i]=ans[i-1].add (Fib[2*i-3].add (fib[2*i-2])); } Scanner Read=NewScanner (system.in); intN; N=Read.nextint ();  while(n!=0) {System.out.println (ans[n]); N=Read.nextint (); }            }}

Of course there are other methods, such as the Stainger solution, C + + code

#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespacestd;structbigint{stringA; intSign ; Bigint () {} Bigint (stringb) {(* This) =b;} intsize () {returna.size (); } Bigint Inversesign () { sign*= -1; return(* This); } Bigint Normalize (intnewsign) {         for(inti = a.size ()-1; i >0&& A[i] = ='0'; i--) A.erase (A.begin ()+i); Sign= (A.size () = =1&& a[0] =='0') ?1: newsign; return(* This); }    void operator= (stringb) {a= b[0] =='-'? B.SUBSTR (1): b;        Reverse (A.begin (), A.end ());  This->normalize (b[0] =='-'? -1:1 ); }    BOOL operator< (ConstBigint &b)Const {        if(Sign! = b.sign)returnSign <b.sign; if(A.size ()! =b.a.size ())returnSign = =1? A.size () < B.a.size (): A.size () >b.a.size ();  for(inti = a.size ()-1; I >=0; i--)if(A[i]! =B.a[i])returnSign = =1? A[i] < B.a[i]: A[i] >B.a[i]; return false; }    BOOL operator== (ConstBigint &b)Const {        returnA = = B.A && sign = =b.sign; } Bigintoperator+(Bigint b) {if(Sign! = b.sign)return(* This) -b.inversesign ();        Bigint C;  for(inti =0, carry =0; I<a.size () | | I<b.size () | | Carry i++) {Carry+ = (i<a.size ()? a[i]- -:0) + (I<b.a.size ()? b.a[i]- -:0); C.A+ = (Carry%Ten+ -); Carry/=Ten; }        returnc.normalize (sign); } Bigintoperator-(Bigint b) {if(Sign! = b.sign)return(* This) +b.inversesign (); ints = sign; Sign = B.sign =1; if( (* This) < b)return((B-(* This). Inversesign ()). Normalize (-s);        Bigint C;  for(inti =0, borrow =0; I < a.size (); i++) {Borrow= A[i]-Borrow-(I < b.size ()? B.a[i]: -); C.A+ = Borrow >=0? Borrow + -: Borrow + -; Borrow= Borrow >=0?0:1; }        returnC.normalize (s); } Bigintoperator*(Bigint b) {Bigint C ("0");  for(inti =0, k = A[i]- -; I < a.size (); i++, k = a[i]- - ) {             while(k--) c = C +b; B.a.insert (B.a.begin (),'0'); }        returnC.normalize (sign *b.sign); } Bigintoperator/(Bigint b) {if(b.size () = =1&& b.a[0] =='0') b.a[0]/= (b.a[0] - - ); Bigint C ("0"), D;  for(intj =0; J < A.size (); J + +) D.a + ="0"; intDsign = sign * b.sign; B.sign =1;  for(inti = a.size ()-1; I >=0; i--) {C.a.insert (C.a.begin (),'0'); C= C + a.substr (I,1 );  while( ! (c < b)) c = C-b, d.a[i]++; }        returnd.normalize (dsign); } Bigintoperator%(Bigint b) {if(b.size () = =1&& b.a[0] =='0') b.a[0]/= (b.a[0] - - ); Bigint C ("0"); B.sign=1;  for(inti = a.size ()-1; I >=0; i--) {C.a.insert (C.a.begin (),'0'); C= C + a.substr (I,1 );  while( ! (c < b)) c = C-b; }        returnc.normalize (sign); }    voidprint () {if(Sign = =-1) Putchar ('-');  for(inti = a.size ()-1; I >=0; i--) Putchar (A[i]); }};intMain () {Bigint ans[ .]; ans[1]="1", ans[2]="3";  for(intI=3;i<2005; i++) {Ans[i]=ans[i-1]+ans[i-1]+ans[i-1]-ans[i-2]; }    intN;  while(~SCANF ("%d", &n) &&n!=0) {ans[n].print (); cout<<Endl; }    return 0;}
View Code

UVA 10862 Connect The Cable wires large integer class C + +

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.