Math in algorithmic contests (i): Fibonacci series

Source: Internet
Author: User
Tags gcd

Many of the recent topics are related to the Fabonacci sequence, and as an information group Konjac Konjac I have recently talked with the math group Lee a great God (Orz), including some of the nature of the Fabonacci series, to make a summary here.

Resources:

"Combinatorial Mathematics (5th Edition)", "Specific Mathematics (2nd edition)"

Body:

The Fibonacci sequence is shaped like 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ... of the sequence. The recursive form is defined as:

Sequence f[n]=f[n-1]+f[n-2], wherein f[0]=0,f[1]=1.

There are, of course, such Fibonacci sequences, which are shaped like:

G[n]=g[n-1]+g[n-2], but which g[0]∈z,g[1]∈z the sequence.

Widely used in the production of life, so in the informatics competition role can not be underestimated, this is some of the common Fibonacci series application problems:

Rabbit reproductive Problems: Oh, the rabbit headache;

The problem of the full-paved dominoes can also be said to be a step up.

First, a small code to find the Fibonacci sequence of Nth:

1 intFibonacciintN)2 {3     intFh=0, ft=1, fs,temp;4     if(n==0)return 0;5     if(n==1)return 1; 6      for(intI=1; i<n;++i)7     {8fs=fh+ft;9Fh=FS;Tenft=fh; One     } A     returnFS; -}

Of course, recursive or recursive algorithms can also be used, the following gives the recursive method of the code for the calculation:

1 intFibonacciintN)2 {3        if(n==0)return 0; 4        if(n==1)return 1; 5        return(Fibonacci (n1) +fibonacci (n2)); 6}

Here are some of the properties of some of the Fibonacci series that have recently been seen:

The first is the general formula:

And the derivation of it:

There is also an important property:

GCD (f (n), F (m)) =GCD (n,m);

This nature to use number theory to prove, but unfortunately this konjac Konjac has not learned number theory, can not personally give proof, but this site has a proof method, interested can go to see:

http://www.douban.com/group/topic/33566582/

So the generation of Fibonacci sequence is very simple;

Although using its general formula involves a large number of powers and irrational numbers, at least when n is large, high precision can be used to ensure that the algorithm complexity is Linear order O (n),

It's easier than recursive, recursive, and cyclic versions of the build anyway.

Then many problems can be solved.

Math in algorithmic contests (i): Fibonacci series

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.