hdu 3326 Another kind of Fibonacci (矩阵构造)

2014-11-24 13:13:53 · 作者: · 浏览: 7

题目大意:

描述了另外一种斐波那契

F[n] = x*F[n-1] + y*F[n-2];

求segma(F[i]^2);


思路分析:

构造矩阵的详细 请戳我

构造矩阵可以得到

中间矩阵为

1 1 0 0

0 x^2 y^2 2*x*y

0 1 0 0

0 x 0 y


#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #define N 30 typedef long long LL; using namespace std; const LL mod = 10007; struct matrix { int a[4][4]; }origin; matrix multiply(matrix x,matrix y) { matrix temp; memset(temp.a,0,sizeof(temp.a)); for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { for(int k=0;k<4;k++) { temp.a[i][j]+=x.a[i][k]*y.a[k][j]; temp.a[i][j]=(temp.a[i][j])%mod; } } } return temp; } matrix matmod(matrix a,LL k) { matrix res; memset(res.a,0,sizeof res.a); for(int i=0;i<4;i++)res.a[i][i]=1; while(k) { if(k&1) res=multiply(res,a); k>>=1; a=multiply(a,a); } return res; } int main() { int n,x,y; while(scanf("%d%d%d",&n,&x,&y)!=EOF) { matrix st; x%=mod; y%=mod; st.a[0][0]=st.a[0][1]=1; st.a[0][2]=st.a[0][3]=0; st.a[1][0]=0; st.a[1][1]=(x*x)%mod; st.a[1][2]=(y*y)%mod; st.a[1][3]=(2*x*y)%mod; st.a[2][0]=st.a[2][2]=st.a[2][3]=0; st.a[2][1]=1; st.a[3][0]=st.a[3][2]=0; st.a[3][1]=x%mod; st.a[3][3]=y%mod; matrix end = matmod(st,n); int ans=0; for(int i=0;i<4;i++) { ans+=end.a[0][i]; ans%=mod; } printf("%d\n",ans%mod); } return 0; }