The use of Fibonacci convolution formula

Source: Internet
Author: User

The Fibonacci sequence is defined as follows:
F (n) = f (n-1) + f (n-2) (n >= 3)
F (1) = 1, f (2) = 2
F (0) can be defined as 1.
The properties can be proved by inductive method:
F (n + M) = f (m-1) f (n + 1) + f (m-2) f (n) (m>= 2)
Using this property, we can convert the Fibonacci number of the larger n into a smaller number of Fibonacci, which makes the calculation more convenient.
Here's a question:

What is the length of the cyclic section of the FIBONACII series Fn (mod k)? Is there a general formula or calculation method for K?


Example

PKU Judgeonline, 3070, Fibonacci.


Problem Description

Given n, requires the result of the nth Fibonacci number mod 10000.


Input


0
9
999999999
1000000000
-1


Output

0
34
626
6875


Analysis

It can be solved recursively by the nature of Fibonacci and the nature of modulo operation. The Pigeon Nest principle lets you know that the output must be a loop. If you know the loop section, the problem is much simpler. The circulation section is also very easy to find.

This topic input data is large, the time cost is not allowed to be so large, while another idea is to use the array to record the results calculated, this practice seems to be possible, but the main topic has memory limitations, for input 1000000000 such large data, obviously to establish such an array is not allowed, based on this, The use of partial tag array (because the more the lower the number of calculations, so long as the range is appropriate, can meet the needs of this topic), and the use of Fibonacci convolution formula calculation, the effect should be better.

Fibonacci properties are many:

1.F (0) +f (1) +f (2) +...+f (n) =f (n+2)-1

2.F (1) +f (3) +f (5) +...+f (2n-1) =f (2n)-1

3.F (0) +f (2) +f (4) +...+f (2n) =f (2n+1)-1

4.[f (0)]^2+[f (1)]^2+...+[f (n)]^2=f (n) f (n+1)

5.F (0)-F (1) +f (2)-... + ( -1) ^n f (n) = ( -1) ^n [F (N+1)-f (n)]+1

6.F (m+n) =f (m-1) F (n-1) +f (M) f (N)

7.[f (n)]^2= ( -1) ^ (n-1) +f (n-1) f (n+1)

8.F (2n-1) =[f (n)]^2-[f (n-2)]^2

9.3f (n) =f (n+2) +f (n-2)

10.F (2n-2m-2) [F (2n) +f (2n+2)]=f (2m+2) +f (4n-2m) [N〉m≥-1, and N≥1]

One f (k+n) =f (k) *f (n+1) +f (k-1) *f (n)


This topic uses the variant of Equation 11 (Fibonacci convolution formula)

The relationship between odd-phase and even-numbered items can be obtained by k=n and k=n+1 respectively

F[n]=f[n/2]*f[n/2+1] + F[N/2-1]*F[N/2]; When N is an even number
F[N]=F[N/2+1]*F[N/2+1] + F[N/2]*F[N/2]; When N is an odd number

Reference Source:

/*poj3070*/#include <iostream> #define MaxSize 5000000using namespace Std;int f[maxsize];int Fibonacci (int n,int MoD) {if (n==0) return 0;if (n==1) return 1;if (n==2) return 1;if (N<maxsize&&f[n]) return F[n];int temp;/** Convolution formula using Fibonacci: *f[n]=f[n/2]*f[n/2+1] + F[N/2-1]*F[N/2]; When n is even, *f[n]=f[n/2+1]*f[n/2+1] + F[N/2]*F[N/2]; when n is odd * /if (n%2==0)/*odd*/temp= (Fibonacci (N/2,mod) *fibonacci (n/2+1,mod) +fibonacci (n/2-1,mod) *fibonacci (n/2,mod))%mod; Elsetemp= (Fibonacci (N/2+1,mod) *fibonacci (n/2+1,mod) +fibonacci (n/2,mod) *fibonacci (n/2,mod))%mod;if (n<maxSize ) F[n]=temp;return temp;} int main (int argc, char** argv) {int n; F[1]=1; F[2]=1; F[3]=2;while (cin>>n&&n!=-1) {Cout<<fibonacci (n,10000) <<endl;} return 1;}

Report

The combination proof of Fibonacci sequence character

&NBSP;&NBSP;&NBSP;&NBSP; n  , then there is:

      fn+1   F  –fn   F  = ( -1) n

See today a combinatorial mathematical proof of this theorem , and felt It's very interesting to share with you here.


The Fibonacci number has a lot of combinatorial mathematical meanings. For example, using 1x1 and 1x2 bricks to cover a 1xn chessboard, the total number of programs is exactly Fn+1 . Shows some examples of the smaller n:

The reason behind this rule is simple: given a chessboard of length n, the coverage scheme can be divided into two categories, the last side is a 1x1 block, or the last side is a 1x2 block. The number of scenarios in the former case is entirely dependent on the number of coverage schemes for the former n–1 lattice, and the scheme number in the latter case equals the number of coverage schemes for the former n–2 lattice. Therefore, if f (n) is used to represent the number of coverage schemes for a 1xn checkerboard, then there is exactly f (n) = f (n–1) + f (n–2). In addition, since f (1) = 1, f (2) = 2, thus the next number F (3), F (4), F (5), ... It just happens to form the Fibonacci series.

     in this way, then use the Bricks cover two independent 1xn chessboard, the total scheme number is Fn+1   F  . We intentionally put the two independent boards as shown on the left. Similarly, the total scheme of covering a 1x (n+1) chessboard with a brick and another 1x (n-1) Chessboard is Fn   F   Let's put the two boards as shown in the image on the right. There is a very clever one by one correspondence between the overlay scheme of the left figure and the overlay scheme on the right.

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

For any of the overlay schemes in the left-hand image, we find the "common split line" that overlaps all the positions in the upper and lower boards, select the common split line at the far right, and then swap the portion to the right of the split line. In this way, each overlay of the checkerboard on the left will turn into an overlay scheme for the right-hand chart board. According to the same method, each overlay scheme on the right-hand chart can also be changed back to the overlay scheme of the left chart board, which illustrates the Fn+1 · Fn+1 and fN · Fn+2 is quite.

Wait, then why, when N is even, Fn+1· Fn+1than FN· Fn+21, and N is an odd number, Fn+1· Fn+1Would be better than FN· Fn+2What about Little 1? God is here. This is because the one by one correspondence that you just mentioned is not really exactly one by one corresponding. When n is even, there is an extremely special coverage scheme on the left-hand chart that does not correspond to the checkerboard on the right-because there is no common split line in this scenario. When n is odd, the checkerboard on the left is no longer such a special coverage scheme, but there is another case where there is no public split line in the right-hand chart. This proves the Fn+1· Fn+1–fN· Fn+2= (-1)N。

The use of Fibonacci convolution formula

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.