Fast Fibonacci sequence (matrix multiplication + fast power)

Source: Internet
Author: User
Tags mul

Fibonacci sequence

Give you a n;f (n) =f (n-1) +f (n-2)

Request out F (f (n)), because the result is large please

To the answer mod 10^9+7;

1<=n<=10^100;

Using matrix multiplication and fast power to find Fibonacci sequence is a classical application.

Matrix Formula C I j=c i k *c K J;

Construct 2*2 matrix according to recursive structure;

Primitive matrix

1 0

0 1

Matrix 2

1 1

1 0

The original matrix is multiplied by the Matrix 2 to achieve the transformation state effect;

The fast power multiplication of matrix Two and the effect of fast transformation matrix are achieved.

The large data range is difficult to solve, even if it achieves a fast conversion state;

Stripping There is a method that does not need to be high-precision;

To play a violent seeking follow-up link;

i.e. F (f (n))%mod with the remainder F (f (n%k)%p)%mod;

The violence asks K p to be able;

k=6e9+6;p=2e9+2;

The following violent procedures

#include <cstdio>typedef long long LL; LL  F[100005],k;int Main () {F[1]=1;k=1;while (1) {k++;f[2]= (f[1]+f[0])%1000000007;if (f[2]==2) {printf ("%lld\n", K );} F[0]=F[1];F[1]=F[2];}}

AC Program

#include <cstdio> #include <cmath> #include <cstring> #include <iostream> #define MOD 1000000007 #define LL Long Long int t,i,j,k;ll n;ll A[2][2],b[2][2],c[2][2],f[2][2];char ch;void Mod (ll mo) {n=0;ll q=getchar (); while (q<48| | q>57) Q=getchar (); while (q>=48&q<=57) {n= (n*10+q-48)%mo;q=getchar ();}} void Mul (ll a[2][2],ll b[2][2],ll mo) {ll c[2][2]={0};for (i=0;i<2;++i) for (j=0;j<2;++j) for (k=0;k<2;++k) c[i][ j]= (C[i][j]+a[i][k]*b[k][j]%mo)%mo;for (i=0;i<2;++i) for (J=0;J&LT;2;++J) a[i][j]=c[i][j];} int main () {//freopen ("xx.in", "R", stdin),//freopen ("Xx.out", "w", stdout), scanf ("%d\n", &t); for (; t--;) {Mod (mod*6ll+6), if (n<=2) {if (!n) printf ("0\n"), Else printf ("1\n"); continue;} N-=1;a[0][0]=a[0][1]=a[1][0]=1;a[1][1]=0;f[0][0]=f[1][1]=1;f[1][0]=f[0][1]=0;while (n) {if (n&1) Mul (f,a,mod* 2ll+2); Mul (a,a,mod*2ll+2); n>>=1;} N=f[0][0]-1;a[0][0]=a[0][1]=a[1][0]=1;a[1][1]=0;f[0][0]=f[1][1]=1;f[1][0]=f[0][1]=0;while (n) {if (n&1) Mul (F, A,MOD); Mul (a,a,mod); n>>=1;} printf ("%d\n", F[0][0]);}} 

  

Problem description "

Make?? (??) For the Fibonacci sequence?? Item, where?? (0) = 0,?? (1) = 1,?? (??) = ?? (??? 1) +?? (??? 2). So what do you want to do? Please?? (?? (??))。 "Input format" the first line an integer?? Represents the number of data groups. Next?? Line one integer per line??。 "Output format"?? Rows per line An integer represents the answer to the value of 10 9 + 7 modulo. "Sample Input" 40126 "sample Output" 01121 "Example interpretation" none. "Data size and Convention" 215 490. 70% of data, 1≤?? ≤10 5. For 100% of the data, 1≤?? ≤10 3, 1≤?? ≤10 100.

Fast Fibonacci sequence (matrix multiplication + fast power)

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.