Fzu_1683
The recursive relationship can be used to construct the matrix shown in, and then the binary matrix can be used to solve the problem.
Since the matrix of the recursive relationship is fixed, we may first pre-process the 2 ^ x (0 <= x <32) Power of the matrix of the recursive relationship used in the binary matrix, this saves some time.
# Include <stdio. h> # Include < String . H> # Define Maxd 4 # Define D 2009 Int N; Struct Matrix { Int A [maxd] [maxd]; matrix () {memset (, 0 , Sizeof ());} Void Init () {memset (, 0 , Sizeof (A); [ 0 ] [ 0 ] = [ 0 ] [ 1 ] = 1 ; [ 1 ] [ 1 ] = 3 , [ 1 ] [ 2 ] = 2 , [ 1 ] [ 3 ] = 7 ; [ 2 ] [ 1 ] =1 ; [ 3 ] [ 2 ] = 1 ;} Mat [ 40 ]; Matrix multiply (Matrix & X, Matrix & Y ){ Int I, J, K, ans; Matrix Z; For (K = 0 ; K < 4 ; K ++) For (I = 0 ; I < 4 ; I ++ ) If (X. A [I] [k]) { For (J = 0 ; J < 4 ; J ++ ) If (Y. A [k] [J]) Z. A [I] [J] = (Z. A [I] [J] + X. A [I] [k] * Y. A [k] [J]) %D ;} Return Z ;} Void Powmod (Matrix & unit, Int N ){ Int CNT = 0 ; While (N ){ If (N & 1 ) Unit = Multiply (MAT [CNT], Unit); n >>= 1 , ++ CNT ;}} Void Prepare (){ Int I; MAT [ 0 ]. INIT (); For (I = 1 ; I < 32 ; I ++ ) Mat [I] = Multiply (MAT [I- 1 ], Mat [I- 1 ]);} Void Solve () {matrix unit; Unit. [ 0 ] [ 0 ] = 4 , Unit. [ 1 ] [ 0 ] = 5 , Unit. [ 2 ] [ 0 ] = 3 , Unit. [ 3 ] [ 0 ] =1 ; Powmod (unit, n - 1 ); Printf ( " % D \ n " , Unit. [ 0 ] [ 0 ]);} Int Main (){ Int T, TT; prepare (); scanf ( " % D " ,& T ); For (Tt = 0 ; TT <t; TT ++ ) {Scanf ( " % D " ,& N); printf ( " Case % d: " , Tt + 1 ); If (N =0 ) Printf ( " 1 \ n " ); Else Solve ();} Return 0 ;}