题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950
解题思路:
AC代码:
1 #include <cstdio> 2 #include <cstring> 3 4 using namespace std; 5 typedef long long ll; 6 const ll mod=2147493647; 7 struct Matrix{ 8 ll mat[7][7]; 9 }; 10 Matrix cst={1,2,1,4,6,4,1, 11 1,0,0,0,0,0,0, 12 0,0,1,4,6,4,1, 13 0,0,0,1,3,3,1, 14 0,0,0,0,1,2,1, 15 0,0,0,0,0,1,1, 16 0,0,0,0,0,0,1}; 17 Matrix Multiply(Matrix x,Matrix y){ 18 Matrix temp; 19 memset(temp.mat,0,sizeof(temp.mat)); 20 for(int i=0;i<7;i++){ 21 for(int j=0;j<7;j++){ 22 for(int k=0;k<7;k++){ 23 temp.mat[i][j]+=(x.mat[i][k]*y.mat[k][j]%mod); 24 temp.mat[i][j]%=mod; 25 } 26 } 27 } 28 return temp; 29 } 30 Matrix Fast_Power(Matrix a,int m){ 31 Matrix res; 32 memset(res.mat,0,sizeof(res.mat)); 33 for(int i=0;i<7;i++) res.mat[i][i]=1; 34 while(m){ 35 if(m&1) res=Multiply(res,a); 36 m>>=1; 37 a=Multiply(a,a); 38 } 39 return res; 40 } 41 int main(){ 42 int t; 43 int N,a,b; 44 scanf("%d",&t); 45 while(t--){ 46 scanf("%d%d%d",&N,&a,&b); 47 if(N==1) printf("%d ",a); 48 else if(N==2) printf("%d ",b); 49 else{ 50 Matrix ret=Fast_Power(cst,N-2); 51 ll ans=(ret.mat[0][0]*b%mod+ret.mat[0][1]*a%mod+ret.mat[0][2]*16%mod+ret.mat[0][3]*8%mod+ret.mat[0][4]*4%mod+ret.mat[0][5]*2%mod+ret.mat[0][6]%mod)%mod; 52 printf("%lld ",ans); 53 } 54 } 55 56 return 0; 57 }