斐波那契数列
题目链接:https://www.luogu.org/problemnew/show/P1962
矩阵A
1,1
1,0
用A^k即可求出feb(k)。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 long long n=2,k; 7 const long long MOD=1000000007; 8 struct Matrix{ 9 long long m[3][3]; 10 }; 11 Matrix A,E,K; 12 13 Matrix cheng(Matrix X,Matrix Y) 14 { 15 Matrix C; 16 for(long long i=1;i<=n;i++) 17 for(long long j=1;j<=n;j++) 18 { 19 C.m[i][j]=0; 20 for(long long l=1;l<=n;l++) 21 C.m[i][j]=(C.m[i][j]+(X.m[i][l]*Y.m[l][j]))%MOD; 22 } 23 return C; 24 } 25 Matrix qsort(Matrix X,long long p) 26 { 27 Matrix S=E; 28 while(p) 29 { 30 if(p&1) S=cheng(S,X); 31 X=cheng(X,X); 32 p>>=1; 33 } 34 return S; 35 } 36 int main() 37 { 38 scanf("%lld",&k); 39 E.m[1][1]=1; 40 E.m[2][2]=1; 41 A.m[1][1]=1; 42 A.m[1][2]=1; 43 A.m[2][1]=1; 44 K=qsort(A,k); 45 printf("%lld ",K.m[2][1]); 46 return 0; 47 }