Http://www.codeforces.com/problemset/problem/182/E
Here is the n middle fence. Each type of fence has a length and a width, set it to a [I], B [I], and then give you a length L
How many beautiful fences can be made up of these fences?
Beautiful definition: 1. Two Adjacent fences must belong to different types.
2. The length of the last fence must be equal to the width of the former one.
Note: The two sides of a fence can be long.
Because each fence is in two states, one is to use a [I] As the length, and the other is to use B [I] As the length.
Therefore, you can set DP [I] [J] [0] to indicate the I-length fence. The last one is the J-fence, and the J-fence uses a [I] as the number of long solutions.
In the same way, DP [I] [J] [1] indicates that the j fence uses B [I] As the length.
With the status, the transfer process is not difficult. The front of J must have a fence width equal to the length of J to be transferred.
Pay attention to the situation where the two sides of a fence are of the same length during the transfer.
Update DP [I] [J] [0] and DP [I] [J] [1] each time as required.
The final answer is the sum of all DP [l] [I] [0] and DP [l] [I] [1, of course, if a [I] = B [I], add only one
View code
# Include <cstdio># Include <Cstring> Const Int MoD = 1000000007 ; Int DP [ 3005 ] [ 110 ] [ 2 ]; Int A [ 110 ], B [ 110 ]; Int Main (){ Int N, l, I, J, K; While (Scanf ( " % D " , & N, & L )! = EOF ){ For (I = 1 ; I <= N; I ++) scanf ( " % D " , & A [I], & B [I]); memset (DP, 0 , Sizeof (DP )); For (I = 1 ; I <= L; I ++ ){ For (J = 1 ; J <= N; j ++ ){ If (A [J] = I) DP [I] [J] [ 0 ] ++ ; If (B [J] = I) DP [I] [J] [ 1 ] ++;}} For (I = 1 ; I <= L; I ++ ){ For (J = 1 ; J <= N; j ++ ){ For (K = 1 ; K <= N; k ++ ){ If (J = K) Continue ; If (A [J] = A [k] & I> A [J] & DP [I-A [J] [k] [ 1 ]) DP [I] [J] [ 0 ] = (DP [I] [J] [ 0 ] + Dp [I-A [J] [k] [ 1 ]) % MOD; If (A [J] = B [k] & I> A [J] & A [k]! = B [k] & DP [I-A [J] [k] [ 0 ]) DP [I] [J] [ 0 ] = (DP [I] [J] [ 0 ] + Dp [I-A [J] [k] [ 0 ]) %MOD; If (B [J] = B [k] & I> B [J] & DP [I-B [J] [k] [ 0 ]) DP [I] [J] [ 1 ] = (DP [I] [J] [ 1 ] + Dp [I-B [J] [k] [ 0 ]) % MOD; If (B [J] = A [k] & I> B [J] & A [k]! = B [k] & DP [I-B [J] [k] [ 1 ]) DP [I] [J] [ 1 ] = (DP [I] [J] [ 1 ] + Dp [I-B [J] [k] [1 ]) % MoD ;}}_ _ int64 ans = 0 ; For (I = 1 ; I <= N; I ++ ) {Ans + = DP [l] [I] [ 0 ]; Ans % = MOD; If (A [I]! = B [I]) ans + = DP [l] [I] [ 1 ]; Ans % =MOD;} printf ( " % I64d \ n " , ANS );}}