sgu 222 - Little Rooks 简单的状态压缩DP

     每行的状态用二进制数表示~~自己好些地方没细心~导致WA了很久..


Program:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<set>
#include<algorithm>
#include<cmath>
#define oo 1000000007
#define ll long long
#define pi acos(-1.0)
#define MAXN 505
using namespace std;  
ll dp[12][1026][12];
int canuse[15],w[15],num;
bool judge(int a)
{
      bool f=false;
      int x=a;
      w[num+1]=0;
      while (x) w[num+1]+=x%2,x/=2;
      while (a)
      {
            if (a%2 && f) return false;
            if (a%2) f=true;
            a/=2;
      }
      return true;     
} 
bool ok(int a,int b)
{
      while (a && b)
      {
            if (a%2 && b%2) return false;
            a/=2,b/=2;
      }
      return true;
}
int main()
{ 
      int n,k,t,i,j,x;
      ll ans;
      while (~scanf("%d%d",&n,&k)) 
      { 
             if (k>n) 
             {
                      printf("0\n");
                      continue;
             }
             memset(dp,0,sizeof(dp));
             num=0;
             for (i=0;i<(1<<n);i++)
               if (judge(i)) canuse[++num]=i; 
             dp[0][0][0]=1;
             for (t=1;t<=n;t++)
                for (i=1;i<=num;i++)
                   for (j=0;j<(1<<n);j++)
                      if (ok(canuse[i],j))
                        for (x=0;x<=k-w[i];x++)
                          dp[t][canuse[i]+j][x+w[i]]+=dp[t-1][j][x];
             ans=0;
             for (i=0;i<(1<<n);i++) ans+=dp[n][i][k];
             printf("%I64d\n",ans);             
      }
      return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值