P1896 [SCOI2005] 互不侵犯
>>>状态压缩dp

>>>转移方程 只要 意义对, 没有的 皆是0/INF ->满足条件
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<bits/stdc++.h> #define ll long long #define ddd printf("-----------------debug\n"); using namespace std; const int maxn=1e1; ll f[20][2000][100],sit[2000],sta[200]; int k,n,cnt; ll ans; void dfs(int cur,int x,int num) { if(cur>=n+1){ sit[++cnt]=x; sta[cnt]=num; return; } dfs(cur+1,x,num); dfs(cur+2,x+(1<<(cur-1)),num+1); } int cmp(int a,int b){ if(sit[a]&sit[b]) return 0; if(sit[a]<<1&sit[b]) return 0; if(sit[a]>>1&sit[b]) return 0; return 1; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin>>n>>k; dfs(1,0,0); for(int i=1;i<=cnt;i++) f[1][i][sta[i]]=1; for(int i=2;i<=n;i++) { for(int j=1;j<=cnt;j++){ for(int x=1;x<=cnt;x++) { if(cmp(j,x)==0) continue; for(int l=sta[j];l<=k;l++) f[i][j][l]+=f[i-1][x][l-sta[j]]; } } } for(int i=1;i<=cnt;i++) ans+=f[n][i][k]; cout<<ans<<endl; return 0; }

浙公网安备 33010602011771号