状压DP,数组范围没注意,搞了半天QAQ
感觉状压DP挺难调的
1 #include<bits/stdc++.h> 2 #define inc(i,l,r) for(int i=l;i<=r;i++) 3 #define dec(i,l,r) for(int i=l;i>=r;i--) 4 #define link(x) for(edge *j=h[x];j;j=j->next) 5 #define mem(a) memset(a,0,sizeof(a)) 6 #define inf 1e9 7 #define ll long long 8 #define succ(x) (1<<x) 9 #define NM 10 10 using namespace std; 11 int read(){ 12 int x=0,f=1;char ch=getchar(); 13 while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} 14 while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); 15 return x*f; 16 } 17 ll d[NM][succ(NM)][NM*NM],ans; 18 int k,n,m,c[succ(NM)],b[succ(NM)]; 19 int main(){ 20 n=read();k=read(); 21 inc(i,0,succ(n)-1) 22 if(!(i&(i<<1)))b[++m]=i,c[m]=__builtin_popcount(i),d[1][m][c[m]]=1; 23 inc(i,2,n) 24 inc(t,1,m) 25 inc(v,0,k) 26 if(c[t]<=v){ 27 int _t=0; 28 inc(j,0,n-1) 29 if((b[t]&succ(j-1))||(b[t]&succ(j))||(b[t]&succ(j+1)))_t+=succ(j); 30 inc(j,1,m) 31 if(!(b[j]&_t)&&c[t]<=v) 32 d[i][t][v]+=d[i-1][j][v-c[t]]; 33 } 34 // inc(i,1,n){inc(t,1,m){ 35 // inc(j,0,k)printf("%d ",d[i][t][j]); 36 // printf("\n");} 37 // printf("\n");} 38 inc(t,1,m)ans+=d[n][t][k]; 39 printf("%lld\n",ans); 40 return 0; 41 }