[bzoj1087]: [SCOI2005]互不侵犯King(状压dp)

传送门
状压dp
O(2n) 预处理出来一行的合法状态,然后构造dp转移方程:
fi,j,kijkking
则转移方程为:

fi,j,k=fi1,s,kcnt(j)(j,s)
好多细节我没写出来,自己看代码吧。
代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int read(){
    int x=0;char ch=' ';int f=1;
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')f=-1,ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int n,K;
ll f[10][1001][100];
int all;
int can[1001];
int tot[1001];
int cnt;
int main(){
    n=read();K=read();
    all=(1<<n)-1;
    for(int s=0;s<=all;s++){
        int flag=1;
        for(int i=0;i<n-1;i++){
            if(s&(s>>1)){
                flag=0;
                break;
            }
        }
        if(flag){
            can[++cnt]=s;
            int ans=0;
            for(int i=0;i<=n-1;i++){
                if((s>>i)&1)ans++;
            }
            tot[cnt]=ans;
        }
    }
    for(int s=1;s<=cnt;s++){
        f[1][can[s]][tot[s]]=1;
    }
    for(int i=1;i<n;i++){
        for(int j=1;j<=cnt;j++){
            for(int k=1;k<=cnt;k++){
                int s1=can[j];
                int s2=can[k];
                if((s1<<1)&s2)continue;
                if((s1>>1)&s2)continue;
                if(s1&s2)continue;
                for(int num=tot[j];num+tot[k]<=K;num++)
                f[i+1][s2][num+tot[k]]+=f[i][s1][num];
            }
        }
    }
    ll ans=0;
    for(int i=1;i<=cnt;i++){
        ans+=f[n][can[i]][K];
    }
    printf("%lld",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值