One Hdu 1438 key count (DP _ state compression DP)

Source: Internet
Author: User

A key has N slots, with a depth of 1, 2, 3, and 4. Each key has at least three different depths and at least one pair of connected slots. The depth difference is 3. Calculate the total number of such keys.

Solution: You can use status DP to write this recursive question. It is really nice to use status DP, but programming is slightly complicated. Dp [I] [j] [k] [s] is used to indicate the I-th bad condition of the key. The total slot depth before I is j and ends with a slot depth k, s is 0, 1, 0 indicates that there is no depth difference of 3 slots, 1 indicates that there is a depth difference of 3 slots. Then enumerate various slots in various States, 1-> 1. If the difference between the front and rear slots depth is 3, 0-> 1; otherwise, 0-> 0.

Data output:
N = 2: 0
N = 3: 8
N = 4: 64
N= 5: 360
N = 6: 1776
N = 7:8216
N = 8: 36640
N = 9: 159624
N = 10: 684240
N = 11: 2898296.
N = 12: 12164608.
N = 13: 50687208.
N = 14th: 209961648
N = 15: 865509848
N = 16: 3553389280
N = 17:14538802248
N = 18: 59313382032.
N = 19th: 241378013240
N = 20:980200805824
N = 21:3973116354984
N = 22: 16078778126448.
N = 23: 64978668500120
N = 24: 262277950619296
N = 25: 1057528710767880.
N = 26:4260092054072400
N = 27: 17147133531655928.
N = 28: 68968784226289024.
N = 29:277229417298013800
N = 30: 1113741009496217136
N = 31: 4472142617535586136.

Code:
[Cpp]
# Include <stdio. h>
# Include <string. h>
 
 
_ Int64 dp [40] [40] [40] [3];
_ Int64 one [40], num [40];
 
Int abs (int x ){
 
Return x> 0? X:-x;
}
 
Int main (){
Int I, j, k, s, cur, pre;
Int ii, jj, kk, ss, n;
 
 
For (I = 0; I <= 15; ++ I)
For (j = 0; j <4; ++ j)
If (I & (1 <j) one [I] ++;
Memset (dp, 0, sizeof (dp ));
For (I = 0; I <4; ++ I)
Dp [1] [1 <I] [I] [0] = 1;
 
 
For (I = 2; I <= 31; ++ I)
For (j = 0; j <= 15; ++ j)
For (k = 0; k <4; ++ k)
For (s = 0; s <4; ++ s ){

Cur = (j | (1 <s ));
Dp [I] [cur] [s] [1] + = dp [I-1] [j] [k] [1];
If (abs (k-s) = 3)
Dp [I] [cur] [s] [1] + = dp [I-1] [j] [k] [0];
Else dp [I] [cur] [s] [0] + = dp [I-1] [j] [k] [0];
}
 

For (I = 2; I <= 31; ++ I ){
 
For (j = 0; j <= 15; ++ j)
If (one [j]> = 3) for (k = 0; k <4; ++ k)
Num [I] + = dp [I] [j] [k] [1];
Printf ("N = % d: % I64d \ n", I, num [I]);
}
 
}


Author: woshi250hua

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.