dp[i][j][m][n][s]表示最初选择j个i号颜色大理石。当前选择n个m号颜色大理石。剩余大理石状态(8进制数状压表示)最开始没看出状压。。sad
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #include <queue> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <climits> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long #define PI 3.1415926535897932626 using namespace std; int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);} int base_8[]={1 ,8 ,64 ,512}; int cnt[4],dp[4][4][4][4][5000]; int N; int calcu(int hc, int hl, int pc, int pl, int sta) { if (dp[hc][hl][pc][pl][sta]!=-1) return dp[hc][hl][pc][pl][sta]; dp[hc][hl][pc][pl][sta]=0; if (sta == 0) { if (hc != pc && hl != pl) return dp[hc][hl][pc][pl][sta] = 1; else return dp[hc][hl][pc][pl][sta] = 0; } for (int i = 0; i < N; i++) for (int j = 1; j <= 3 && j <= cnt[i]; j++) { if (i != pc && j != pl) { cnt[i] -= j; dp[hc][hl][pc][pl][sta] += calcu(hc, hl, i, j, sta - base_8[i] * j); cnt[i] += j; } } return dp[hc][hl][pc][pl][sta]; } int slove() { int state=0,ans=0; for (int i = N - 1; i >= 0; i--) state = state * 8 + cnt[i]; for (int i = 0; i < N; i++) for (int j = 1; j <= 3 && j <= cnt[i]; j++) ans += calcu(i, j, i, j,state - j * base_8[i]); return ans; } int main() { int T; memset(dp,-1,sizeof(dp)); scanf("%d",&T); while (T--) { scanf("%d",&N); for (int i = 0; i < N; i++) scanf("%d",&cnt[i]); if (cnt[0] == 0 && cnt[1] == 0 && cnt[2] == 0 && cnt[3] == 0) puts("1"); else printf("%d ",slove()); } return 0; }