Fzu_1692
First, I feel that l and R in the input data should be read in turn, that is, R is read first and then l is read. Otherwise, the sample cannot be calculated. I don't know if I understand the incorrect question.
It is not difficult to construct a matrix. However, this question further introduces Changshu Optimization in matrix operations:
① Since the rows of the matrix of the recursive relationship are cyclically homogeneous, and the product of the two such cyclic homogeneous matrices is still cyclically homogeneous, when using the binary matrix method for calculation, you can calculate the first line with the time of O (N ^ 2) and then use the time of O (N ^ 2) to remove the results of the following rows, in this way, the complexity of multiplication is reduced from O (N ^ 3) to O (N ^ 2 ).
② The multiply function of matrix multiplication is best to use references to pass passing parameters. If the parameter is directly transferred, the matrix is copied every time, which is less efficient.
# Include <stdio. h> # Include < String . H> # Define Maxd 110 Int N, M, L, R, D, a [maxd]; Struct Matrix { Int A [maxd] [maxd]; matrix () {memset (, 0 , Sizeof ());} Void Init (){ Int I, J; For (I = 0 ; I <n; I ++ ) A [I] [(I + N- 1 ) % N] = L, A [I] [I] = 1 , A [I] [(I + 1 ) % N] = R ;}}; matrix multiply (Matrix & X, Matrix & Y ){ Int I, J, K; Matrix Z; For (K = 0 ; K <n; k ++ ) If (X. [0 ] [K]) { For (J = 0 ; J <n; j ++ ) If (Y. A [k] [J]) Z. [ 0 ] [J] = (Z. [ 0 ] [J] + ( Long Long ) X. [ 0 ] [K] * Y. A [k] [J]) % D ;} For (I =1 ; I <n; I ++ ) {Z. A [I] [ 0 ] = Z. A [I- 1 ] [N- 1 ]; For (J = 1 ; J <n; j ++ ) Z. A [I] [J] = Z. A [I- 1 ] [J- 1 ];} Return Z;} matrix powmod (matrix unit, matrix mat, Int N ){ While (N ){ If (N & 1 ) Unit = Multiply (MAT, Unit); n >>= 1 ; MAT = Multiply (MAT, mat );} Return Unit ;} Void Solve (){ Int I, j, ans; matrix unit, mat; scanf ( " % D " , & N, & M, & R, & L ,& D ); For (I = 0 ; I <n; I ++ ) {Scanf ( " % D " ,&A [I]); Unit. A [I] [I] = 1 ;} Mat. INIT (); Unit = Powmod (unit, mat, M ); For (I = 0 ; I <n; I ++ ) {Ans = 0 ; For (J = 0 ; J <n; j ++ ) If (Unit. A [I] [J] &A [J]) ans = (ANS + ( Long Long ) Unit. A [I] [J] * A [J]) % D; If (I) printf ( " " ); Printf ( " % D " , ANS);} printf ( " \ N " );} Int Main (){ Int T; scanf ( " % D " ,& T ); While (T -- ) {Solve ();} Return 0 ;}