Fibonacci sequence (Fibonacci) and its extension

Source: Internet
Author: User

In fact, the Fibonacci sequence of things I have learned in elementary school, but only then did not learn so clearly, now let us take a look at the Fibonacci sequence of some algorithms (beginners can see)
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... [1]
If you set F (n) to be the nth item of the series (n∈n*), then this sentence can be written as follows: [1]
Obviously this is a linear recursive sequence. [1]
General term Formula
(This is a closed formula, which means that the formula is not very accurate for small numbers.)
1. When the n<=45, directly can be counted
2. When the n<=1000 direct high-precision, if you are not high precision can be seen in Java to write a large number of addition, the effect is pretty good
3. When n>1000, you can use this formula, but this formula to deal with what the Fibonacci number of the number of the first few can also, if the number of how to do it, rest assured that there will be explanations, to be patient to read slowly back.
Here, let's raise a chestnut.
aFor example, I ask for the top four of f (n).
We know that 1234567 can be written 1234.567*10^ (len-4), some people will ask Len how to ask, we know, for example, 99, it's Len is 2, then we can use the log10, then found less than 2, this is right, The biggest two-digit log10 after all is less than 2, then we take the whole and add a 1, have you found any laws, that is, log10 you ask for the number, and then add one after the whole, is the value of Len, then let us continue to operate, because we are asking for the top four, then we might as well set it to D, set the number is X ,
Then there will be an equation x=d.567*10^ (len-4),
Then let's take a logarithmic operation, LOG10X=LOG10 (d.567+len-4),
Then proceed to simplify log10 (X+4-len) =log10 (d.567);
Afterwards 10^ (log10 (X+4-len) =d.567;
Now someone asked, x how to ask, of course, directly set the formula Ah, but generally when n more than 45,
((1-SQRT (5))/2) ^n is already too small, so generally can be shed, does not affect.
Y=LOG10 (1/SQRT (5)) +n*log10 ((1+SQRT (5)/2) +4-(int) (LOG10 (1/SQRT (5)) +n*log10 ((1+SQRT (5))/2) (+1);
then int (POW (10,y)) is OK
Two. What if the 4 digits are counted?
Well, just follow the recursive formula and ask for 10000 more each time.
Note that if you encounter a negative number, what should you do?
Give a chestnut: -11%3=-2, at this time we should make ( -2+3)%3=1;
In addition, here are some of the following formulas:
1. (a+b)%m= (a%m+b%m)%m;
2. (a*b)%m= (a%m*b%m)%m;
3.a^n%m= (a%m) ^n;
Let's take a quick look at how fast power is coming, after reading this, we know how the short code is going to operate, let's give a chestnut
7^83%5= (7*7^82)%5;
= (2* (49^41))%5;
= (2*4^41)%5;
= (2*4*4^40)%5;
= (3*1^20)%5;
= 3;
Haha, is actually such a simple process, you understand not.
Note that the next step is to enter the most important link-matrix multiplication, learned the linear algebra of the small partner should be able to understand the rest (as long as you will be the most basic matrix multiplication in fact, do not worry)
Furthermore, it is not only the Fibonacci sequence that can be multiplied by matrices, any expression about f (n) can be multiplied by matrix. Oh, real an artifact, let's use an example directly to give you a chestnut
hit2255
http://acm.hit.edu.cn/hoj/problem/view?id=2255
Maybe acmers of HIT are always fond of Fibonacci numbers, because it is so beautiful. Don ' t you? At the same time, Fishcanfly always likes to change and this time he thinks about the following series of numbers which yo U can guess is derived from the definition of Fibonacci number.
The definition of Fibonacci number is:
F (0) = 0, f (1) = 1, and for n>=2, f (n) = f (n-1) + f (n-2)
We define the new series of numbers as below:
F (0) = A, f (1) = B, and for n>=2, f (n) = P*f (n-1) + q*f (n-2), where p and q are integers.
Just like the "Last time", we are interested in the sum of this series from the s-th element to the e-th element, which is, t o Calculate
great! Let ' s go!
Input
The ' the ' input file contains a single integer t (1 <= t <=), the number of test cases, followed by t He input the data for the each test case.
(Please click here to open the picture) http://acm.hit.edu.cn/hoj/static/img/pic/100309.bmp
Each test case contains 6 integers a,b,p,q,s,e as concerned above. We know that-1000 <= a,b <= 1000,-10-<= p,q <= and 0 <= s <= e <= 2147483647.
Output
One for each test case, containing a single interger denoting S MOD (10^7) in the range [0,10^7) and the leading zero s should not to be printed.
Sample Input
2
0 1 1-1 0 3
0 1 1 1 2 3
Sample Output
2
3

#include <iostream> #define MOD 10000000 const int max=3;
using namespace Std; typedef struct {long long m[max][max];}
Matrix;
Matrix p= {0,0,0, 1,0,0, 0,0,1,};
Matrix i= {1,0,0, 0,1,0, 0,0,1,};
    Matrix mm (Matrix A,matrix b) {matrix C;
            for (int i=0; i<max; i++) for (int j=0; j<max; J + +) {c.m[i][j]=0;
                for (int k=0; k<max; k++) {a.m[i][k]= (a.m[i][k]%mod+mod)%mod;
                b.m[k][j]= (b.m[k][j]%mod+mod)%mod;
            c.m[i][j]+= (A.m[i][k]*b.m[k][j])%mod;
        } c.m[i][j]= (C.m[i][j]%mod+mod)%mod;

return C;
    Matrix Quickpow (Long long N) {matrix x = P, y = I;
        while (n>=1) {if (n&1) y=mm (y,x);
     n=n>>1;
     X=MM (X,X);

} return y;
    int main () {int a,b,p,q,s,e,t;
    Matrix TMP,TMP1;
    cin>>t; Long Long Sum,sum1,sum2;
        while (t--) {sum=0;
        sum2=0;
        cin>>a>>b>>p>>q>>s>>e;
        P.m[0][0]=p;
        p.m[0][1]=q;
        P.m[2][0]=p;
        p.m[2][1]=q;
            if (e>=2) {Tmp=quickpow (e-1);

            Sum= (tmp.m[2][0]*b)%mod+ (tmp.m[2][1]*a)%mod+ (tmp.m[2][2]* (a+b))%mod;
        Sum= (sum+mod)%mod;
        } if (e==1) {sum= (a+b)%mod+mod)%mod;
        } if (e==0) sum= (a%mod+mod)%mod;
        if (s==0) sum1=0;
        if (s==1) sum1= (a%mod+mod)%mod;
        if (s==2) sum1= (a+b)%mod+mod)%mod;
            if (s>=3) {Tmp1=quickpow (s-2);
            sum1= (tmp1.m[2][0]*b)%mod+ (tmp1.m[2][1]*a)%mod+ (tmp1.m[2][2]* (a+b))%mod;
        sum1= (sum1+mod)%mod;
        } sum2= (Sum-sum1+mod)%mod;
    cout<<sum2<<endl;
return 0;
 }

In fact, we can also not classify the problem, the middle matrix of the last item into S (n-2), then the left side of the Matrix and the right of the matrix are changed, so that the left of the matrix of the N-power corresponding to the right matrix just can find S (n), as for the drawing of what how to push on their own to try it
Add a formula

Here's a final question about the addition of large numbers, which I've had in my blog before, but I want to put together a chestnut hdu1250
High Precision Method

#include <iostream>
#include <stdio.h>
using namespace std;
int a[8000][255]= {{0}};
int n,k,ans,i,j,m;
int main ()
{for
    (i=1; i<5; i++) a[i][1]=1;
    For (i=5, i<8000; i++) for
        (j=1; j<255; j + +)
        {
            a[i][j]+=a[i-1][j]+a[i-2][j]+a[i-3][j]+a[i-4][j];
            a[i][j+1]+=a[i][j]/100000000;
            a[i][j]=a[i][j]%100000000;
    }
    while (scanf ("%d", &n)!=eof)
    {for
        (j=254; j>0; j--)
            if (a[n][j]!=0)
            {
                k=j;
                cout<<a[n][j];
                break;
        For (m=k-1 m>0; m--)
            printf ("%.8d", A[n][m]);
            cout<<endl;


    }
    return 0;
}

Java Large number method

Import java.io.*;
Import Java.math.BigInteger;
Import java.util.*;
public class Main {public
    static void Main (string[] args)
    {
        Scanner cin=new Scanner (system.in);
        BigInteger a[]=new biginteger[10500];
        A[1]=biginteger.valueof (1);
        A[2]=biginteger.valueof (1);
        A[3]=biginteger.valueof (1);
        A[4]=biginteger.valueof (1);
        for (int i=5;i<10500;i++)
            a[i]=a[i-1].add (A[i-2]). Add (a[i-3). Add (a[i-4));
        int n;
        while (Cin.hasnext ())
        {
            n=cin.nextint ();
            System.out.println (A[n]);}}

This blog is nearing the end, but honestly, a little tired but also very happy, looking forward to the next blog trip.

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.