1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn = 6, mod = 123456789; 5 6 struct MAT { 7 ll a[maxn][maxn]; 8 MAT(){ memset(a,0,sizeof(a)); } 9 MAT operator*(MAT p) { 10 MAT res; 11 for (int i = 0; i < maxn; i++) 12 for (int j = 0; j < maxn; j++) 13 for(int k = 0; k < maxn; k++) 14 res.a[i][j] = (res.a[i][j]+a[i][k]*p.a[k][j])%mod; 15 return res; 16 } 17 }; 18 MAT mat_qpow(MAT A, ll b) { 19 MAT res; 20 res.a[0][0] = 1; 21 while(b) { 22 if(b&1) res = res*A; 23 A = A*A; 24 b >>= 1; 25 } 26 return res; 27 } 28 void init(MAT& A, MAT& B) { 29 A.a[0][0]=1, A.a[0][1]=2, A.a[0][2]=1, A.a[0][3]=0, A.a[0][4]=0, A.a[0][5]=0; 30 A.a[1][0]=1, A.a[1][1]=0, A.a[1][2]=0, A.a[1][3]=0, A.a[1][4]=0, A.a[1][5]=0; 31 A.a[2][0]=0, A.a[2][1]=0, A.a[2][2]=1, A.a[2][3]=3, A.a[2][4]=3, A.a[2][5]=1; 32 A.a[3][0]=0, A.a[3][1]=0, A.a[3][2]=0, A.a[3][3]=1, A.a[3][4]=2, A.a[3][5]=1; 33 A.a[4][0]=0, A.a[4][1]=0, A.a[4][2]=0, A.a[4][3]=0, A.a[4][4]=1, A.a[4][5]=1; 34 A.a[5][0]=0, A.a[5][1]=0, A.a[5][2]=0, A.a[5][3]=0, A.a[5][4]=0, A.a[5][5]=1; 35 36 B.a[0][0]=2, B.a[1][0]=1, B.a[2][0]=27, B.a[3][0]=9, B.a[4][0]=3, B.a[5][0]=1; 37 } 38 39 int main() { 40 int t; scanf("%d",&t); 41 while(t--) { 42 ll n; scanf("%lld",&n); 43 MAT A; MAT ans; 44 init(A,ans); 45 ans = mat_qpow(A,n-2)*ans; 46 printf("%lld\n",ans.a[0][0]); 47 } 48 return 0; 49 }
原文地址:https://www.cnblogs.com/wstong/p/11743114.html
时间: 2024-10-25 21:39:12