时间复杂度:O(64n t^2) #include <bits/stdc++.h> #define int long long using namespace std; int n,t,ans; int f[21][5][5][12][12]; signed main(){ //f[i][j][k][t1][t2]:前i位,第i位为j,第i-1位为k,前i-1位,t1个条件1,t2个条件2已经满足 scanf("%lld%lld",&n,&t); for (register int i=1; i<=4; ++i) for (register int j=1; j<=4; ++j) if (i!=j) f[2][i][j][0][0]=1; for (register int l=3; l<=n; ++l) for (register int t1=0; t1<=t; ++t1) for (register int t2=0; t2<t; ++t2) for (register int i=1; i<=4; ++i) for (register int j=1; j<=4; ++j) for (register int k=1; k<=4; ++k) { if (k<j && j>i && t1+1<=t) f[l][i][j][t1+1][t2]+=f[l-1][j][k][t1][t2]; else if (k>j && j<i && t2+1<=t) f[l][i][j][t1][t2+1]+=f[l-1][j][k][t1][t2]; else if (k!=j && j!=i && k!=i) f[l][i][j][t1][t2]+=f[l-1][j][k][t1][t2]; } for (register int i=1; i<=4; ++i) for (register int j=1; j<=4; ++j) if (i!=j) ans+=f[n][i][j][t][t-1]; printf("%lld\n",ans); return 0; }