一道超級簡單的狀壓DP題因此說狀壓是個好東西spa
看數據範圍,同時咱們發現一個格子要麼放國王or不放,所以能夠用二進制數來表示某一行的國王放置信息code
因而咱們立刻想到用\(f_{i,j}\)表示放了前\(i\)行,其中第\(i\)行的國王擺放狀況爲\(j\)時的方案數io
那麼轉移就很顯然了,每次咱們枚舉本行的國王信息以及上一行的放置位置,而後判斷是否合法便可。class
具體的操做其實就是\(<<,>>\)以後\(\&\)一下便可,這個本身看二進制
那麼這樣時限可能有點緊,咱們還能夠預處理一下每一行的合法狀況,而後每次只枚舉這些合法狀況數據
固然還有些dalao說能夠兩行一塊兒處理,這樣會更快di
反正我這麼菜確定不會,其餘的看CODE吧while
// luogu-judger-enable-o2 #include<cstdio> using namespace std; const int N=10; long long f[N][(1<<N)+5][N*N],ans; int n,m,tot,t[(1<<N)+5]; bool c[(1<<N)+5]; inline bool check(int x) { int flag=0; while (x) { if ((x&1)&flag) return 0; flag=x&1; x>>=1; } return 1; } inline int calc(int x) { int res=0; while (x) res+=x&1,x>>=1; return res; } inline bool judge(int x,int y) { return !(x&y||x&(y<<1)||(x<<1)&y); } int main() { scanf("%d%d",&n,&m); register int i,j,k,s; tot=(1<<n)-1; for (i=0;i<=tot;++i) c[i]=check(i),t[i]=calc(i); for (i=0;i<=tot;++i) if (c[i]) f[1][i][t[i]]=1; for (i=2;i<=n;++i) for (j=0;j<=tot;++j) if (c[j]) for (k=0;k<=tot;++k) if (c[k]&&judge(j,k)) for (s=m;s>=t[j];--s) f[i][j][s]+=f[i-1][k][s-t[j]]; for (i=0;i<=tot;++i) ans+=f[n][i][m]; return printf("%lld",ans),0; }