BZOJ 1087 [SCOI 2005] 互不侵犯的king (状压DP)
2015-03-11 13:44
489 查看
题目链接:BZOJ 1087
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int N,M,cnt=0;
int a[550],s[550];
long long dp[12][550][500];
int find(int x){
int t=1,tot=0;
while(t<=x){
if(t&x)tot++;
t<<=1;
}
return tot;
}
int main(){
scanf("%d%d",&N,&M);
for(int i=0;i<(1<<N);i++){
if(!(i&(i<<1))){
a[++cnt]=i; s[cnt]=find(i);
}
}
for(int i=1;i<=cnt;i++)dp[1][i][s[i]]=1;
long long ans=0;
for(int i=2;i<=N;i++){
for(int p=0;p<=M;p++){
for(int j=1;j<=cnt;j++){
for(int k=1;k<=cnt;k++){
if(!(a[k]&a[j]) && !((a[j]<<1)&a[k]) && !((a[j]>>1)&a[k])){
if(p>=s[k]+s[j])dp[i][j][p]+=dp[i-1][k][p-s[j]];
}
}
}
}
}
for(int i=1;i<=cnt;i++)ans+=dp
[i][M];
cout<<ans<<endl;
return 0;
}
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int N,M,cnt=0;
int a[550],s[550];
long long dp[12][550][500];
int find(int x){
int t=1,tot=0;
while(t<=x){
if(t&x)tot++;
t<<=1;
}
return tot;
}
int main(){
scanf("%d%d",&N,&M);
for(int i=0;i<(1<<N);i++){
if(!(i&(i<<1))){
a[++cnt]=i; s[cnt]=find(i);
}
}
for(int i=1;i<=cnt;i++)dp[1][i][s[i]]=1;
long long ans=0;
for(int i=2;i<=N;i++){
for(int p=0;p<=M;p++){
for(int j=1;j<=cnt;j++){
for(int k=1;k<=cnt;k++){
if(!(a[k]&a[j]) && !((a[j]<<1)&a[k]) && !((a[j]>>1)&a[k])){
if(p>=s[k]+s[j])dp[i][j][p]+=dp[i-1][k][p-s[j]];
}
}
}
}
}
for(int i=1;i<=cnt;i++)ans+=dp
[i][M];
cout<<ans<<endl;
return 0;
}
相关文章推荐
- 【bzoj 1087】[SCOI2005]互不侵犯King 状压dp
- BZOJ 1087 SCOI2005 互不侵犯King 状压DP
- BZOJ 1087 [SCOI2005]互不侵犯King (状压DP)
- [BZOJ1087][SCOI2005]互不侵犯King(状压dp)
- 【BZOJ】1087: [SCOI2005]互不侵犯King(状压dp)
- 【bzoj1087】[SCOI2005]互不侵犯King 状压DP
- bzoj1087 [SCOI2005]互不侵犯King(状压dp)
- BZOJ 1087 [SCOI2005]互不侵犯King ——状压DP
- [bzoj1087]: [SCOI2005]互不侵犯King(状压dp)
- bzoj 1087: [SCOI2005]互不侵犯King 状压dp
- 【BZOJ1087】【SCOI2005】互不侵犯King(状压dp)
- bzoj 1087: [SCOI2005]互不侵犯King 状压dp
- [BZOJ1087][SCOI2005]互不侵犯King解题报告|状压DP
- 【BZOJ】1087 [SCOI2005]互不侵犯King 状压DP(轮廓线DP)
- Bzoj 1087: [SCOI2005]互不侵犯King(状压DP)
- BZOJ1087: [SCOI2005]互不侵犯King 题解【DP】【状压】
- BZOJ 1087: [SCOI2005]互不侵犯King 预处理,状压DP
- BZOJ 1087: [SCOI2005]互不侵犯King( 状压dp )
- BZOJ 1087: [SCOI2005]互不侵犯King | 状压DP
- BZOJ题目1087: [SCOI2005]互不侵犯King(状态压缩DP)