Luogp1962 Fibonacci series, P1962 Fibonacci Series
Background
As we all know, the Fibonacci series is a series that meets the following characteristics:
• F (1) = 1
• F (2) = 1
• F (n) = f (n-1) + f (n-2) (n ≥ 2 and n is an integer)
Description
Find the value of f (n) mod 1000000007.
Input/Output Format
Input Format:
· Row 1st: an integer n
Output Format:
Row 3: f (n) mod 1st Value
Input and Output sample input sample #1: Copy
5
Output example #1: Copy
5
Input example #2: Copy
10
Output sample #2: Copy
55
Description
For 60% of data: n ≤ 92
For 100% of data, n is within the range of long (INT64.
Bare matrix Rapid power
We need to determine either of the following situations:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #define LL long long 6 using namespace std; 7 const int MAXN=200000001; 8 const int mod=1000000007; 9 inline LL read()10 {11 char c=getchar();LL flag=1,x=0;12 while(c<'0'||c>'9') {if(c=='-') flag=-1;c=getchar();}13 while(c>='0'&&c<='9') x=x*10+c-48,c=getchar(); return x*flag;14 }15 LL n;16 struct Matrix17 {18 LL a[5][5];19 Matrix()20 {21 memset(a,0,sizeof(a));22 }23 };24 Matrix Matrix_Mul(Matrix a,Matrix b)25 {26 Matrix c;27 for(LL k=1;k<=2;k++) 28 for(LL i=1;i<=2;i++)29 for(LL j=1;j<=2;j++)30 c.a[i][j]+=(a.a[i][k]*b.a[k][j])%mod;31 return c;32 }33 inline void WORK()34 {35 Matrix bg,tmp;36 bg.a[1][1]=1;bg.a[1][2]=1;37 38 tmp.a[1][1]=0;tmp.a[1][2]=1;39 tmp.a[2][1]=1;tmp.a[2][2]=1;40 while(n) 41 { 42 if(n&1) bg=Matrix_Mul(bg,tmp);43 tmp=Matrix_Mul(tmp,tmp);44 n>>=1;45 } 46 printf("%lld",bg.a[1][2]%mod);47 }48 int main()49 {50 n=read();n=n-2;51 if(n==-1||n==-2) 52 {53 printf("1");54 return 0;55 }56 57 58 WORK();59 return 0;60 }