Zoj 3256 tour in the castle

Source: Internet
Author: User

Zoj_3256

When I first learned the plug-in DP, I began to torture me. At that time, I failed to win this question, mainly because I didn't understand it at the time: ① for the N power representing the adjacent matrix of a graph, the element at the (I, j) position indicates the number of solutions for point I to reach Point J through n steps; ② At the beginning, plug-in DP is implemented in a grid-by-grid manner. This requires column-by-column DP first, and matrix multiplication is performed after all possible column states are extended, so I don't know how to do it.

In fact, during column-by-column DP, 2 ^ n pieces can be used to list whether there is a right plug in each position in the current column, and then consider the status of the Right plug in the previous column on the left (the status here refers: whether there is a plug, and which connected plug), and then scan it from top to bottom, because the two adjacent plug (the two plug may be in the previous column, may be in the current column, there may also be one path) (whether the upper and lower plugs are determined after the Left and Right plugs are determined, so there is no need to worry about whether the upper and lower plugs are ), in this way, you can determine whether the status of the two columns can be transferred during the scan process, at the same time, you can also push out the complete status of the Right plug in the current Column Based on the plug merge situation (the previous only enumerated whether there is any, here we can calculate which connected block it belongs ), if a new connected block is created, a new number indicates the connected block. After the recurrence, in order to reduce the recurrence of the state, we can use the minimum representation of the recursive state.

In addition, I use a 4-digit plug status. during actual initialization, I will find that even if n = 7, the possible columns expanded out of the status are less than 120.

# Include <stdio. h># Include < String . H> # Define Hash 419 # Define Maxd 1010 # Define Mod4 7777777 Typedef  Long   Long  Ll;  Int N, m, d, g [ 128 ] [ 128 ], Code [ 10 ], H [ 10 ];  Struct  Matrix {  Int A [ 128 ] [ 128  ]; Matrix  Operator *( Const Matrix & T) Const  {  Int  I, J, K; ll sum; matrix ans;  For (I =0 ; I <D; I ++ )  For (J = 0 ; J <D; j ++ ) {Sum = 0  ;  For (K = 0 ; K <D; k ++) sum + = (LL) A [I] [k] * T. A [k] [J]; ans. A [I] [J] = Sum % MOD ;}  Return Ans ;}} mat, unit;  Struct  Hashmap {  Int  Head [hash], size, next [maxd], St [maxd];  Void  Init () {memset (Head, - 1 , Sizeof (Head), size = 0  ;}  Int Push ( Int  _ St ){ Int I, H = _ st % Hash;  For (I = head [H]; I! =- 1 ; I = Next [I])  If (St [I] = _ st) Break  ;  If (I =- 1  ) {I = Size; ST [size] = _ St; next [size] = Head [H], head [H] = size ++ ;}  Return  I ;}} HM;  Int Encode ( Int * Code, Int  N ){  Int I, St = 0 , CNT = 0  ; Memset (H, - 1 , Sizeof (H), H [ 0 ] = 0  ;  For (I = 0 ; I <n; I ++ ){  If (H [Code [I] =- 1 ) H [Code [I] = ++ CNT; ST = ST < 2 | H [Code [I];}  Return  St ;}  Void Decode ( Int * Code, Int N, Int  St ){  For ( Int I = N- 1 ; I> = 0 ; I --) Code [I] = sT & 3 , St> = 2  ;}  Int Trans ( Int X, Int  Y ){ For ( Int I = 0 ; I <n; I ++) If (Code [I] = x) Code [I] = Y ;}  Int Check ( Int St, Int  NST ){  Int I, J, K, flag = 0 , CNT = 0  ; Decode (Code, N, St );  Int T [ 10  ]; Memcpy (T, code,  Sizeof  (CODE ));  For (I = 0 ; I <n; I ++ ){  If (Flag = 0  ){  If (Code [I] = 0 & (NST & 1 <I) = 0 ) Return   0  ;  If (Code [I] & (NST & 1 <I )) Continue  ;  If (Code [I]) Flag = Code [I];  Else Flag =- 1  ; K = I ;}  Else {  If (Code [I] & (NST & 1 <I )) Return   0  ;  If (Code [I] = 0 & (NST & 1 <I) = 0 ) Continue  ;  If (Code [I]) {  If (Code [I] = Flag & (NST! = 0 | I! = N- 1 )) Return   0  ;  If (Flag> 0 ) Trans (code [I], code [k]), Code [I] = Code [k] = 0  ;  Else Code [k] = Code [I], code [I] = 0 ;}  Else  {  If (Flag> 0 ) Code [I] = Code [K], code [k] = 0  ;  Else Code [I] = Code [k] = N + (CNT ++ );} Flag = 0  ;}}  If (Flag! = 0 )Return   0  ;  Return   1  ;}  Void  Init (){  Int  I, J, K, St, NST; hm. INIT (); memset (code,  0 , Sizeof (Code), Code [ 0 ] = Code [n- 1 ] = 1 ; Hm. Push (  0  ), Hm. Push (encode (Code, n); memset (G,  0 , Sizeof  (G ));  For (I = 1 ; I  ) {St = Hm. St [I];  For (NST = 0 ; NST <( 1 <N); NST ++)  If  (Check (St, NST) {J = Hm. Push (encode (Code, n); G [I] [J] = 1  ;} D = Hm. size ;}  Void Powmod ( Int  N ){  While  (N ){  If (N & 1 ) MAT = mat * Unit; Unit = Unit * Unit, N> = 1  ;}}  Void  Solve (){  Int  I, J; memset (mat.,  0 , Sizeof  (Mat. ));  For (I = 0 ; I <D; I ++) mat. A [I] [I] = 1  ; Memcpy (Unit. A, G, Sizeof  (G); powmod (m );  If (Mat. [ 1 ] [ 0 ] = 0 ) Printf ( "  Impossible \ n  "  );  Else Printf ( "  % D \ n  " , Mat. [ 1 ] [0  ]);}  Int  Main (){  While (Scanf ( "  % D  " , & N, & M) = 2  ) {Init (); solve ();}  Return   0  ;} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.