Topic links: http://acm.hdu.edu.cn/showproblem.php?pid=6470
Write this question is to make myself not to forget the power of matrix how quickly launch matrix formulas.
Note that the code is a TLE! !
1 #include<stdio.h> 2 #include<string.h> 3 #define mem(a, b) memset(a, b, sizeof(a)) 4 typedef long long ll; 5 const int mod = 123456789; 6 7 ll n; 8 9 struct Matrix 10 { 11 ll a[10][10]; 12 }A, res, temp; 13 14 Matrix Multiply(Matrix a, Matrix b) 15 { 16 Matrix ans; 17 mem(ans.a, 0); 18 for(int i = 1; i <= 6; i ++) 19 for(int j = 1; j <= 6; j ++) 20 for(int k = 1; k <= 6; k ++) 21 { 22 ans.a[i][j] += a.a[i][k] * b.a[k][j] % mod; 23 ans.a[i][j] %= mod; 24 } 25 return ans; 26 } 27 28 int main () 29 { 30 mem (Aa, 0 ); // Structures Nori阵 31 Aa [ 1 ] [ 2 ] = Aa [ 2 ] [ 2 ] = Aa [ 2 ] [ 3 ] = Aa [ 3 ] [ 3 ] = Aa [ 3 ] [ 6 ] = Aa [ 4 ] [ 4 ] = Aa [ 4 ] [ 6 ] = Aa [ 5 ] [ 5 ] = Aa [ 5 ] [ 6 ] = Aa [ 6][6] = 1; 32 A.a[2][1] = A.a[4][5] = 2; 33 A.a[3][4] = A.a[3][5] = 3; 34 int T; 35 scanf("%d", &T); 36 while(T --) 37 { 38 temp = A; //初始化temp矩阵 39 mem(res.a, 0 ); // initialize the unit matrix 40 for ( int I = . 1 ; I <= . 6 ; I ++ ) 41 is res.a [I] [I] = . 1 ; 42 is Scanf ( " % LLD " , & n-) ; 43 is n-- = 2 ; 44 is the while (n-) 45 { 46 is IF (% n- 2 ) 47 RES = the Multiply (RES, TEMP); 48 TEMP = the Multiply (TEMP, TEMP); 49 n /= 2; 50 } 51 printf("%lld\n", (1 * res.a[2][1] % mod + 2 * res.a[2][2] % mod + 27 * res.a[2][3] % mod + 9 * res.a[2][4] % mod + 3 * res.a[2][5] % mod + res.a[2][6] % mod) % mod); 52 } 53 return 0; 54 }