2 1 1 3 2 3
6 196
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int mod=10007; struct matrix { long long ma[5][5]; }; matrix multi(matrix x,matrix y)//矩阵相乘 { matrix ans; memset(ans.ma,0,sizeof(ans.ma)); for(int i=1;i<=4;i++) { for(int j=1;j<=4;j++) { if(x.ma[i][j])//稀疏矩阵优化 for(int k=1;k<=4;k++) { ans.ma[i][k]=(ans.ma[i][k]+(x.ma[i][j]*y.ma[j][k])%mod)%mod; } } } return ans; } matrix pow(matrix a,long long m) { matrix ans; for(int i=1;i<=4;i++) { for(int j=1;j<=4;j++) { if(i==j) ans.ma[i][j]=1; else ans.ma[i][j]=0; } } while(m) { if(m&1) ans=multi(ans,a); a=multi(a,a); m=m>>1; } return ans; } int main() { long long x,y,n; while(~scanf("%I64d%I64d%I64d",&n,&x,&y)) { matrix a,b; memset(a.ma,0,sizeof(a.ma)); memset(b.ma,0,sizeof(b.ma)); a.ma[1][1]=1; a.ma[1][2]=1; a.ma[2][2]=(x*x)%mod; a.ma[2][3]=(y*y)%mod; a.ma[2][4]=(2*x*y)%mod; a.ma[3][2]=1; a.ma[4][2]=x; a.ma[4][4]=y; b.ma[1][1]=1; b.ma[2][1]=1; b.ma[3][1]=1; b.ma[4][1]=1; a=pow(a,n); a=multi(a,b); printf("%I64d\n",a.ma[1][1]); } return 0; }
hdu 3306 Another kind of Fibonacci(矩阵快速幂)
原文地址:http://blog.csdn.net/caduca/article/details/40680943