斐波那契 矩阵快速幂

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <queue>
 5 #include <cstdio>
 6 #include <algorithm>
 7 #include <map>
 8 #include <time.h>
 9 #include <ext/pb_ds/assoc_container.hpp>
10 #include <ext/pb_ds/tree_policy.hpp>
11 #define LL long long
12 
13 using namespace std;
14 using namespace __gnu_pbds;
15 
16 const int MOD = 19999997;
17 
18 struct Martix
19 {
20     LL martix[2][2];
21     int row,col;
22     Martix(int _row,int _col)
23     {
24         memset(martix,0,sizeof(martix));
25         row = _row;
26         col = _col;
27     }
28     Martix operator *(const Martix &A)const
29     {
30         Martix C(row, A.col);
31         for(int i = 0; i < row; i++)
32             for(int j = 0; j < A.col; j++)
33                 for(int k = 0; k < col; k++)
34                     C.martix[i][j] =  (C.martix[i][j] + martix[i][k] * A.martix[k][j])%MOD;
35         return C;
36     }
37 };
38 
39 
40 void solve()
41 {
42     Martix A(2,2), F(2,2);
43     A.martix[0][0] = A.martix[0][1] = A.martix[1][0] = 1;
44     F.martix[0][0] = F.martix[1][1] = 1;
45     int n;
46     scanf("%d",&n);
47     while(n > 0)
48     {
49         if(n & 1)
50             F = F*A;
51         A = A*A;
52         n >>= 1;
53     }
54     printf("%lld\n",F.martix[0][0]);
55 }
56 
57 int main(void)
58 {
59     solve();
60     return 0;
61 }

 

posted on 2016-08-05 14:00  鱼泪儿  阅读(151)  评论(0编辑  收藏  举报

导航