传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1088
简单的状压dp(话说本题的编号减1,即1087,也是一道状压dp),不解释。
#include <cstdio> const int maxn = 10005; int n, b[maxn]; long long f[maxn][8]; int main(void) { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", b + i); if (b[i] > 3 || b[i] < 0) { puts("0"); return 0; } } if (b[1] == 3 || b[n] == 3) { puts("0"); return 0; } if (b[1] == 0) { f[1][0] = 1; } else if (b[1] == 1) { f[1][0] = f[1][1] = 1; } else { f[1][1] = 1; } for (int i = 2; i <= n; ++i) { if (b[i - 1] == 0) { f[i][0] = f[i - 1][0] + f[i - 1][4]; } else if (b[i - 1] == 1) { f[i][1] = f[i - 1][0] + f[i - 1][4]; f[i][2] = f[i - 1][1] + f[i - 1][5]; f[i][4] = f[i - 1][2] + f[i - 1][6]; } else if (b[i - 1] == 2) { f[i][3] = f[i - 1][1] + f[i - 1][5]; f[i][5] = f[i - 1][2] + f[i - 1][6]; f[i][6] = f[i - 1][3] + f[i - 1][7]; } else { f[i][7] = f[i - 1][3] + f[i - 1][7]; } } if (b[n] == 0) { printf("%lld ", f[n][0] + f[n][4]); } else if (b[n] == 1) { printf("%lld ", f[n][1] + f[n][5] + f[n][2] + f[n][6]); } else if (b[n] == 2) { printf("%lld ", f[n][3] + f[n][7]); } return 0; }