洛谷P1896||bzoj1087 [SCOI2005]互不侵犯
想了很久,太久没做状压都已经不会了。。。
状压每一行就好了
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<vector> 5 using namespace std; 6 #define fi first 7 #define se second 8 #define mp make_pair 9 #define pb push_back 10 typedef long long ll; 11 typedef unsigned long long ull; 12 typedef pair<int,int> pii; 13 int n,K,s; 14 ll an[10][82][512],ans; 15 bool ok[512]; 16 int pcnt[512]; 17 #define in(a,b) (((a)&(b))==(a)) 18 vector<int> son[512]; 19 int main() 20 { 21 int i,j,k,l1,l,t; 22 scanf("%d%d",&n,&K);s=(1<<n)-1; 23 for(i=0;i<=s;++i) 24 ok[i]=!((i&(i<<1))|(i&(i>>1))); 25 for(i=0;i<=s;++i) 26 pcnt[i]=__builtin_popcount(i); 27 for(i=0;i<=s;++i) 28 if(ok[i]) 29 { 30 t=s&(~(i|(i<<1)|(i>>1))); 31 for(j=t;j;j=(j-1)&t) 32 if(ok[j]&&in(i,s&(~(j|(j<<1)|(j>>1))))) 33 { 34 //printf("1t%d %d\n",i,j); 35 son[i].pb(j); 36 } 37 son[i].pb(0); 38 } 39 for(i=0;i<=s;++i) 40 an[1][pcnt[i]][i]=1; 41 for(i=2;i<=n;++i) 42 { 43 for(j=0;j<=K;++j) 44 { 45 for(k=0;k<=s;++k) 46 if(ok[k]&&pcnt[k]<=j) 47 { 48 for(l1=0;l1<son[k].size();++l1) 49 { 50 l=son[k][l1]; 51 //printf("3t%d %d\n",j-pcnt[k],l); 52 an[i][j][k]+=an[i-1][j-pcnt[k]][l]; 53 } 54 //printf("2t%d %d %d %lld\n",i,j,k,an[i][j][k]); 55 } 56 } 57 } 58 for(i=0;i<=s;++i) 59 ans+=an[n][K][i]; 60 printf("%lld",ans); 61 return 0; 62 }