Matrix multiplication Fast Power Codevs 1732 Fibonacci sequence 2

Source: Internet
Author: User

1732 Fibonacci Series 2

time limit: 1 sspace limit: 128000 KBtitle level: Diamonds Diamond SolvingView Run ResultsTitle Description Description

In the "1250 Fibonacci series", we find the value of the nth Fibonacci series. But in 1250, n<=109. Now, your task is still to find the value of the nth Fibonacci sequence, but note that n is an integer and 1 <= n <= 100000000000000

Enter a description Input Description

Enter multiple sets of data, one row for each group of data, and an integer n (1 <= n <= 100000000000000)

Output description Output Description

Outputs several rows. Output per line (corresponding input) n Fibonacci number (considering the number will be very large, mod 1000000007)

Sample input Sample Input

3
4
5

Sample output Sample Output

2
3
5

Data range and Tips Data Size & Hint

1 <= N <= 100000000000000

Although the data is large, but the matrix fast power can still be solved, and fast, as long as the attention to prevent longlong multiplication overflow can

1 #defineN 32 #definell Long Long3#include <iostream>4 using namespacestd;5#include <cstdio>6#include <cstring>7 structjz{8     intCal,line;9     Long LongJz[n][n];Ten }; One Long Longq=1000000007; A intRead () - { -     Chars; the     intans=0, ff=1; -s=GetChar (); -      while(s<'0'|| S>'9') -     { +         if(s=='-') ff=-1; -s=GetChar (); +     } A      while('0'<=s&&s<='9') at     { -ans=ans*Ten+s-'0'; -s=GetChar (); -     } -     returnans*ff; - } in Long LongQuick_mod (ll A,ll b)/*slow multiplication prevents overflow*/ - { toa%=q;b%=Q; +ll ans=0; -      while(b) the     { *         if(b&1) $         {Panax Notoginsengans+=A; -ans%=q;// the         } +b>>=1; Aa<<=1; thea%=Q; +     } -     returnans; $ } $ Jz martax (Jz x,jz y) - { - Jz ans; theAns.line=X.line; -Ans.cal=y.cal;Wuyi      for(intI=1; i<=ans.line;++i) the        for(intj=1; j<=ans.cal;++j) -       { Wuans.jz[i][j]=0; -            for(intk=1; k<=x.cal;++k) AboutAns.jz[i][j]= (Ans.jz[i][j]+quick_mod (x.jz[i][k],y.jz[k][j]))%Q; $       } -     returnans; - } - Long LongFast_martax (Long LongN) A { +     if(n==1|| n==2)return 1; then-=2; - Jz ans,a; $A.cal=a.line=2; thea.jz[1][1]=0; a.jz[1][2]=1; thea.jz[2][1]=1; a.jz[2][2]=1; theAns.line=2; ans.cal=1; theans.jz[1][1]=1; ans.jz[2][1]=1; -      while(n) in     { the         if(n&1) the         { Aboutans=Martax (A,ans); the         } then>>=1; theA=Martax (a,a); +     } -     returnans.jz[2][1]%Q; the }Bayi intMain () the { the     Long LongN; -      while(SCANF ("%lld", &n) = =1) -     { theprintf"%lld\n", Fast_martax (n)); the     } the     return 0; the}

Matrix multiplication Fast Power Codevs 1732 Fibonacci sequence 2

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.