State compression. The value of the last slot and whether the current configuration scheme can be DP.
1 /*1438*/2#include <cstdio>3#include <cstring>4#include <cstdlib>5 6 #defineMAXN 327 8 Const intMaxs =1<<4;9 Ten__int64 dp[maxn][maxs][4][2]; One intcnt[1<<4]; A - intAbsintx) { - returnx<0? -x:x; the } - - intMain () { - intI, J, K, N; + ints; - __int64 ans; + Amemset (CNT,0,sizeof(CNT)); at for(i=0; i<maxs; ++i) - for(j=0; j<4; ++j) - if(I & (1<<j)) -++Cnt[i]; - -Memset (DP,0,sizeof(DP)); in for(i=0; i<4; ++i) -dp[1][1<<i][i][0] =1; to + for(n=2; n<maxn; ++N) { - for(i=0; i<maxs; ++i) { the for(j=0; j<4; ++j) { * for(k=0; k<4; ++k) { $s = i | (1<<j);Panax Notoginsengdp[n][s][j][1] + = dp[n-1][i][k][1]; - if(ABS (j-k) = =3) { thedp[n][s][j][1] + = dp[n-1][i][k][0]; +}Else { Adp[n][s][j][0] + = dp[n-1][i][k][0]; the } + } - } $ } $ } - - for(n=2; n<maxn; ++N) { theAns =0; - for(i=0; i<maxs; ++i) {Wuyi if(Cnt[i] >=3) theAns + = dp[n][i][0][1] + dp[n][i][1][1] + -dp[n][i][2][1] + dp[n][i][3][1]; Wu } -printf"n=%d:%i64d\n", n, ans); About } $ - return 0; -}
"Hdoj" one of the 1438 key counts