//求FIB的第n项
1 #include <iostream> 2 #include <vector> 3 #include <cstdio> 4 5 using namespace std; 6 #define LL long long 7 8 typedef vector<int> vec; 9 typedef vector<vec> mat; 10 11 mat mul(mat& A, mat& B) 12 { 13 mat C ( A.size(),vec(B[0].size())); 14 15 for(int i=0;i<A.size();i++) 16 { 17 for(int k = 0;k<B.size();k++) 18 { 19 for(int j = 0;j<B[0].size();j++) 20 { 21 C[i][j] = C[i][j] + A[i][k]*B[k][j] ; 22 } 23 } 24 } 25 return C; 26 } 27 28 mat pow(mat A, LL n) 29 { 30 mat B( A.size(),vec(A.size())); 31 32 for(int i=0;i<A.size();i++) 33 { 34 B[i][i] =1; 35 } 36 while( n>0) 37 { 38 if( n&1) B = mul(B,A); 39 A = mul(A,A); 40 n>>=1; 41 } 42 return B; 43 44 45 } 46 47 int main() 48 { 49 LL n ; 50 cin>>n; 51 mat A ( 2, vec(2)); 52 A[0][0] =1; A[0][1] = 1; 53 A[1][0] = 1; A[1][1] = 0; 54 A = pow( A, n); 55 printf("%d ",A[1][0]); 56 return 0; 57 }