题解:

状压dp

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=610;
int n,m,cnt[N],c1[N],c2[N][N];
ll ans,f[10][N][N];
void pre()
{
    int s;
    for (int i=0;i<1<<n;i++)
     if ((i&(i>>1))==0)
      {
        s=0;
        for (int x=i;x;x>>=1)s+=(x&1);
        cnt[i]=s;c1[i]=1;
      }
    for (int i=0;i<1<<n;i++)
     if (c1[i])
      for (int j=0;j<1<<n;j++)
       if (c1[j])
        if (((i&j)==0)&&((i&(j>>1))==0)&&((j&(i>>1))==0))c2[i][j]=1;
}
int main()
{
    scanf("%d%d",&n,&m);
    pre();
    for (int i=0;i<1<<n;i++)
     if (c1[i])f[1][cnt[i]][i]=1;
    for (int j=1;j<n;j++)
     for (int k=0;k<1<<n;k++)
      if (c1[k])
       for (int i=0;i<1<<n;i++)
        if (c1[i])
         if (c2[k][i])
          for (int p=cnt[k];p+cnt[i]<=m;p++)f[j+1][p+cnt[i]][i]+=f[j][p][k];
    ll ans=0;
    for (int i=0;i<1<<n;i++)ans+=f[n][m][i];
    printf("%lld",ans);
    return 0;
}

 

posted on 2018-02-21 12:00  宣毅鸣  阅读(88)  评论(0编辑  收藏  举报