2013 Multi-University Training Contest 9 1011 Arc of Dream
利用递推关系构建矩阵 然后矩阵快速幂
1 #include<cstdio> 2 #include<cstring> 3 typedef long long LL; 4 const int mod=1000000007; 5 struct matrix 6 { 7 LL m[5][5]; 8 matrix(){memset(m,0,sizeof(m));} 9 matrix operator*(const matrix &a)const 10 { 11 matrix tmp; 12 for(int i=0;i<5;i++) 13 for(int j=0;j<5;j++) 14 for(int k=0;k<5;k++) 15 tmp.m[i][j]=(tmp.m[i][j]+m[i][k]*a.m[k][j]%mod)%mod; 16 return tmp; 17 } 18 }e; 19 matrix pow(matrix a,LL n) 20 { 21 if(n==1) return a; 22 if(n%2) 23 { 24 matrix tmp=pow(a,n/2); 25 return tmp*tmp*a; 26 } 27 else 28 { 29 matrix tmp=pow(a,n/2); 30 return tmp*tmp; 31 } 32 } 33 int main() 34 { 35 LL n; 36 while(scanf("%lld",&n)!=EOF) 37 { 38 LL A0,AX,AY,B0,BX,BY; 39 scanf("%lld%lld%lld%lld%lld%lld",&A0,&AX,&AY,&B0,&BX,&BY); 40 if(n==0) {printf("0\n");continue;} 41 LL input[5][5]={AX*BX%mod,AX*BY%mod,AY*BX%mod,AY*BY%mod,0,0,AX%mod,0,AY%mod,0,0,0,BX%mod,BY%mod,0,0,0,0,1,0,AX*BX%mod,AX*BY%mod,AY*BX%mod,AY*BY%mod,1}; 42 LL a[5]={A0*B0%mod,A0%mod,B0%mod,1,A0*B0%mod}; 43 for(int i=0;i<5;i++) 44 for(int j=0;j<5;j++) 45 e.m[i][j]=input[i][j]; 46 if(n==1) { printf("%lld\n",a[4]);continue;} 47 matrix ans=pow(e,n-1); 48 LL answer=0; 49 for(int i=0;i<5;i++) 50 answer=(answer+ans.m[4][i]*a[i]%mod)%mod; 51 printf("%lld\n",answer); 52 } 53 return 0; 54 }