hdu 2855 Fibonacci Check-up (矩阵)

题目链接:hdu 2855 Fibonacci Check-up

题意&题解:

 1 #include<bits/stdc++.h>
 2 #define mst(a,b) memset(a,b,sizeof(a))
 3 #define F(i,a,b) for(int i=a;i<=b;i++)
 4 using namespace std;
 5 typedef long long ll;
 6 
 7 const int mat_N=2;
 8 int mo;
 9 struct mat{
10     int c[mat_N][mat_N];
11     void init(){mst(c,0);}
12     mat operator*(mat b){
13         mat M;int N=mat_N-1;M.init();
14         F(i,0,N)F(j,0,N)if(c[i][j])F(k,0,N)M.c[i][k]=(M.c[i][k]+c[i][j]*b.c[j][k])%mo;
15         return M;
16     }
17     mat operator^(ll k){
18         mat ans,M=(*this);int N=mat_N-1;ans.init();
19         F(i,0,N)ans.c[i][i]=1;
20         while(k){if(k&1)ans=ans*M;k>>=1,M=M*M;}
21         return ans;
22     }
23 }A,B;
24 
25 int main()
26 {
27     A=(mat){1,1,1,0};
28     int t,k;
29     scanf("%d",&t);
30     while(t--)
31     {
32         scanf("%d%d",&k,&mo);
33         if(k==0){puts("0");continue;}
34         B=A^(2*k-1);
35         printf("%d\n",B.c[0][0]);
36     }
37     return 0;
38 }
View Code

 

posted @ 2017-02-20 20:15  bin_gege  阅读(144)  评论(0编辑  收藏  举报