Fibonacci Calculation of large numbers/huge Fibonacci numbers-ACM

Source: Internet
Author: User

Huge Fibonacci numbers

Time Limit: 1 sec memory limit: 128 MB

Description

A Fibonacci sequence is calculated by adding the previous two members
Of the sequence, with the first two members being both 1.

F (1) = 1, F (2) = 1, F (n> 2) = f (n-1) + f (n-2)

Your task is to take a number as input, and print that maid number.

Input
Input one integer.

Output
Output the F (n ).

Notice: No generated Maid number in excess of 1000 digits
Will be in the test data, I. e. F (20) = 6765 has 4 digits.

Sample Input
100

Sample output
354224848179261915075

Key: Addition of large numbers. Nothing else should be done.

I wroteCode:

# Include <stdio. h> # Include < String . H> /*  A user-defined large number indicates that each int in the method struct array represents a number no more than 10, 0000, and 0000, which is smaller than the maximum value of the signed Int, that is to say, we split a large number into multiple parts for representation, just like when we add two numbers, one bit and one bit, we just useProgram9-bit 9-bit addition, replacing the manual one-bit addition  */  //  The requirement is that the number of 1000-bit Fibonacci is not exceeded. Here I will use max_big BITs, which can represent at most  //  Is max_big * 9 = 120*9 = 1080 bits.  //  Here, big_integer [0] is used to represent the second bit. # Define Max _ big 120 Typedef  Struct  {  //  + 1: it may generate the highest bit carry, but I don't care, directly discard it (otherwise vs2012 will report that the stack is damaged,  //  The stack will indeed be damaged. vs2012 is intelligent) Unsigned Int Big [max_big + 1  ];} Big_integer;  # Define Big_base 1000000000 //  Do not modify it. /*  **************************************** * ************* Function: calc @ 12 function: A + B-> C, large addition parameter: A, B-addition; C: And return: (none) **************************************** **************  */  Void Calc (big_integer * a, big_integer * B, big_integer * C) {unsigned  Int Sum = 0  ;  Int I = 0  ;  /* * ************************ An addition is calculated cyclically below: addend 1: ABCD addend 2: efgh results: (A + E + x) (B + F + x) (C + G + x) (D + H + x) Where: ABCD, efgh here each represents a 9-digit x representing the carry from the low position **************************  */  Memset (C,  0 , Sizeof  (Big_integer ));  For (I = 0 ; I <max_big; I ++ ) {C -> Big [I] = A-> big [I] + B-> big [I] + C-> big [I]; //  Perform 9-bit addition once, remember to add itself C-> big [I +1 ] = C-> big [I]/big_base; //  Add the result carry to the high position C-> big [I] = C-> big [I] % big_base; //  The current bit only retains the remainder  }}  /*  **************************************** * ******************* Function: fibonacci @ 4 function: Calculate the nth Fibonacci value parameter: N: nth digit, the minimum value is 1 ************************************* **********************  */  Void Fibonacci ( Int  N) {big_integer = { 0  }; Big_integer B = { 0  }; Big_integer C = { 0  };  Int I = 0  ;  If (N <= 1  ) {Printf (  "  % D \ n  " , N =1  );  Return  ;}  //  Fibonacci ,...  //  To guarantee the results of the first calculation (n = 2), the initial values are: a = 0, B = 1 => C = 1 A. Big [ 0 ] = 0  ; B. Big [  0 ] = 1  ;  /* * ************************ The following cyclically calculates the onacci like decimal places: F3 = F1 + F2; f1 = F2; F2 = F3 ;**************************  */      For (I = 2 ; I <= N; I ++ ) {Calc ( & A, & B ,& C); memcpy ( & A, & B, Sizeof  (Big_integer); memcpy ( & B, & C, Sizeof  (Big_integer ));}  // This for loop ignores a numerical bit (9 digits) whose value starts from the highest bit)  //  Of course, condition I> 0 is required. Do you know why?      For (I = max_big- 1 ; C. Big [I] = 0 & I> 0 ; I -- );  //  For the highest bit: The leading '0' is not required' Printf ( "  % U  " , C. Big [I -- ]); //  Then, each numeric bit is output in 9-bit decomposition. Remember to output leading 0,  //  At first, I didn't notice it.      While (I> = 0  ) {Printf (  "  % 09u  "  , C. Big [I]); I -- ;} Printf (  "  \ N  "  );} Int Main ( Void  ){  Int  N; scanf (  "  % D  " ,& N); maid (N );  Return   0  ;} 

 
The program runs at a very fast speed. In fact, it takes less than 1 second to calculate to 10000:

Girl don't cry @ 01:36:13 @ http://www.cnblogs.com/nbsofer

 

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.